copyMakeBorder returns un-solid border
Goal: Adding a solid white line to a black'n'white image.
I'm trying to extract text from an image by cropping it and then adding a solid border with copyMakeBorder()
. My problem is that I can't make the border solid. It seems like the copyMakeBorder()
uses information from my initial image instead.
This is my code:
void OcvCardReader::cropToAreaWithSolidBorder(Mat image, const Rect2f area) {
//Crop the image.
image = image(area);
//Add the border.
int borderSize = 10;
copyMakeBorder(image, image,
borderSize, borderSize, borderSize, borderSize,
BORDER_CONSTANT, Scalar(255));
}
My input image looks like this:
This is after the crop:
And finally, after adding the border:
Note the black pixels att the bottom left. I had expected a border to be white all around. I also tried to use Scalar(0)
to see if it makes any difference. It doesn't.
Curiously, it works well if I convert the image to a 3-channel Mat
and then use Scalar(255, 255, 255)
, but that seems like a detour to me.
So, why does copyMakeBorder()
return a border that isn't solid?
it's only part of the problem, but please use
cropToAreaWithSolidBorder(Mat &image, ...)
(pass a reference) else your Mat header is not updated after calling the functionOh! I thought a
Mat
was big enough to be passed by reference automagically. Thanks! I will try that! :-Dit's not about the size, but about manipulating a copy (the pixels will be changed, but not the Mat header (rows,cols and such)
From the doc:
Is it what you got?
Also, be careful with in-place parameters (
src
anddst
objects are the same). Some OpenCV functions do not operate with in-place parameters.