Ask Your Question

Revision history [back]

So, I've tried a few different solutions based on the comments I got.

First I tried to change the call to call by reference, as @berak suggested. It might have changed something in the overhead but it did no visible change to the image.

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));
}

Then I changed to use BORDER_ISOLATED instead of BORDER_CONSTANT, as @Eduardo suggested. It worked like a charm! :-D

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_ISOLATED, Scalar(255));
}

I also managed to find a book article suggesting the use of another crop-method, and that worked as well.

void OcvCardReader::cropToAreaWithSolidBorder(Mat& image, const Rect2f area) {
    //Crop the image.
    float centerX = area.x + area.size().width / 2;
    float centerY = area.y + area.size().height / 2;
    Point2f center(centerX, centerY);
    getRectSubPix(image, area.size(), center, image);
    //Add the border.
    int borderSize = 10;
    copyMakeBorder(image, image,
        borderSize, borderSize, borderSize, borderSize,
        BORDER_CONSTANT, Scalar(255));
}

Which solution one goes for is a matter of choice, I guess. I will be reading up a bit more based on the given comments to gain a better understanding of how the methods works. My code will probably be a combination of the mentioned solutions in the end.

So, I've tried a few different solutions based on the comments I got.

First I tried to change the call to call by reference, as @berak suggested. It might have changed something in the overhead but it did no visible change to the image.

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));
}

Then I changed to use BORDER_ISOLATED instead of BORDER_CONSTANT, as @Eduardo suggested. It worked like a charm! :-Dcharm!

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_ISOLATED, Scalar(255));
}

I also managed to find a book article suggesting the use of another crop-method, and that worked as well.

void OcvCardReader::cropToAreaWithSolidBorder(Mat& image, const Rect2f area) {
    //Crop the image.
    float centerX = area.x + area.size().width / 2;
    float centerY = area.y + area.size().height / 2;
    Point2f center(centerX, centerY);
    getRectSubPix(image, area.size(), center, image);
    //Add the border.
    int borderSize = 10;
    copyMakeBorder(image, image,
        borderSize, borderSize, borderSize, borderSize,
        BORDER_CONSTANT, Scalar(255));
}

Which solution one goes for is a matter of choice, I guess. I will be reading up a bit more based on the given comments to gain a better understanding of how the methods works. My code will probably be a combination of the mentioned solutions in the end.