copyTo with () operator is not working on IOS

asked 2019-07-23 07:26:29 -0600

updated 2019-07-23 12:25:31 -0600

berak gravatar image

I am getting Thread 1: Heap buffer overflow. when i run following code. Actually I want to convert rectangle image to bigger square image and image should be centred.

It generates error on copyto function when there is range provided.

   cv::Mat croppedImage;
   cv::Mat(orig_image, roiF).copyTo(croppedImage);
   cv::Mat(orig_image,roiF).clone();

    int size = roiF.height > roiF.width ? roiF.height : roiF.width;

    int offset_height = ((size) / 2 - (roiF.height) / 2);
    int offset_width = ((size ) / 2 - (roiF.width) / 2);

    cv::Mat  resizeImage = cv::Mat(size, size , croppedImage.type(),new cv::Scalar(0,0,0,255));
    if (offset_width > 0) {
        cv::Range colSrcRange = cv::Range(0, roiF.width);
        cv::Range rowSrcRange = cv::Range(0, roiF.height); //select maximum allowed cols
        cv::Range colDestRange = cv::Range(offset_width, offset_width + roiF.width);
        cv::Range rowDestRange = cv::Range(0, roiF.height + 0);
        croppedImage(rowSrcRange, colSrcRange).copyTo(resizeImage(rowDestRange,  colDestRange));
    } else if (offset_height > 0) {
        cv::Range colSrcRange =cv::Range(0, roiF.width); //select maximum allowed cols(*roiF).height
        cv::Range rowSrcRange =cv::Range(0, roiF.height);
        cv::Range colDestRange = cv::Range(0, roiF.width + 0);
        cv::Range rowDestRange = cv::Range(offset_height, offset_height + roiF.height);
        croppedImage(rowSrcRange, colSrcRange).copyTo(resizeImage(rowDestRange,  colDestRange));
    }
    return [OpenCVWrapper UIImageFromCVMat:resizeImage];
edit retag flag offensive close merge delete

Comments

Can you try to reproduce on a easier example ?

{
    Mat img =(Mat_<uchar>(3, 4) << 0 , 1 , 2 , 3 , 10 , 11 , 12 , 13 , 20 , 21 , 22 , 23);
    Mat a(3, 4, CV_8UC1, Scalar(0));
    Mat b(3, 4, CV_8UC1, Scalar(0));

    img.copyTo(a);
    cout << "a = " << a << endl;
    img(Range(0,2),Range(1,3)).copyTo(b(Range(0, 2), Range(1, 3)));
    cout << "b = " << b << endl;


}
LBerger gravatar imageLBerger ( 2019-07-23 09:13:32 -0600 )edit

This code worked but in my code i am getting error,

govi2010 gravatar imagegovi2010 ( 2019-07-23 09:36:54 -0600 )edit

I am also getting Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) sometimes.

govi2010 gravatar imagegovi2010 ( 2019-07-23 09:42:00 -0600 )edit