why am i getting so many roi crashes

asked 2016-07-04 04:41:42 -0500

atv gravatar image

I don't think i understand copyTo/roi very well because the moment i fiddle with the size of a picture i copy onto another Mat (like picture in picture) i get opencv crashing on me with assertion statements.

What am i doing wrong, or how do i protect against this ? What are the rules?

I understand you can't copy a mat that is bigger then another mat into it. I think i also get crashes when work is done on a empty mat.

I can provide some example code.

edit retag flag offensive close merge delete


Hello there, could you give us the exact assertion statements ? :)

Major Squirrel gravatar imageMajor Squirrel ( 2016-07-04 05:48:41 -0500 )edit

Stuff like this essentially: http://answers.opencv.org/question/97...

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /tmp/opencv-20160502-20452-13c59z2/opencv-2.4.12/modules/core/src/matrix.cpp, line 323 libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv-20160502-20452-13c59z2/opencv-2.4.12/modules/core/src/matrix.cpp:323: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat

atv gravatar imageatv ( 2016-07-04 06:22:45 -0500 )edit

@atv In OpenCV, it seems that the size of a picture has to be inverted according to the matrix initialization. For example, a picture with a size of 640x480 has to be initialized as a matrix like so :

cv::Mat(480, 640, CV_YOUR_DATATYPE);

Are you sure the matrix is well initialized according to the picture size ?

EDIT: It should be helpful if you code give us some example code to help your further. :)

Major Squirrel gravatar imageMajor Squirrel ( 2016-07-04 07:19:28 -0500 )edit

I didn't know that.I'm using something like this: https://github.com/Itseez/opencv_cont...

So i don't think it's ever specifically initialized, i think it uses the webcam's resolution/capability. That is basically the code i'm using. I then use :

int pos_x = std::max(face_i.tl().x, 0); // Used to be x-10 int pos_y = std::max(face_i.tl().y, 0); // Used to be y-10

    // Do our subviews first:
    if(!face_resized.empty() && pos_x>0 && pos_y>0) { // Check for face
        Mat srcBGR = Mat(face_resized.size(), CV_8UC3);
        cvtColor(face_resized, srcBGR, CV_GRAY2BGR);

        cv::Rect roi = cv::Rect(50,50, srcBGR.cols/2, srcBGR.rows/2);
        cv::Rect roi_dst = cv::Rect(pos_x,pos_y,srcBGR.cols/2,srcBGR.rows
atv gravatar imageatv ( 2016-07-04 07:51:06 -0500 )edit

cv::Mat subview = srcBGR(roi); // subview.copyTo(original(roi_dst));

To copy a small picture into the rectangle and i use the if checks to hopefully safeguard me against roi crashes. But if i put a picture somewhere else (outside of the rectangle), or if i enlarge this picture, say 0,0 then it crashes very quickly.

atv gravatar imageatv ( 2016-07-04 07:52:43 -0500 )edit

@atv Try to insert some debug messages to verify some values, especially srcBGR.cols and srcBGR.rows, what do they tell you ?

Major Squirrel gravatar imageMajor Squirrel ( 2016-07-04 07:59:16 -0500 )edit