picture in picture roi crash assertion failed [closed]

asked 2016-06-23 00:26:23 -0600

atv gravatar image

So i finally got this picture in picture to work after i looked through Shervin's Chapter 8 code of mastering CV. I was missing the conversion lines below:

 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::Mat subview = srcBGR(roi);
 subview.copyTo(original(roi));

However, i think i am going out of bounds somewhere and i'm not sure how to check this or to stay within it.

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

Alef

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-08 03:09:21.958973

Comments

how large is your image ?

berak gravatar imageberak ( 2016-06-23 01:05:34 -0600 )edit

I put some couts in there. The face_resized returns 200x200. Interestingly enough just before the crash face_resized is 0. I guess it doesn't always get a face.

[200 x 200]
[1280 x 960]
[0 x 0]
[1280 x 960]
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

Abort trap:

atv gravatar imageatv ( 2016-06-23 01:45:35 -0600 )edit

Sorry not sure how to markup properly in a comment. So if face_resized gets to be 0 sometimes (is that normal/expected?) how could i best check against that? Many thanks.

atv gravatar imageatv ( 2016-06-23 01:46:58 -0600 )edit

"Sorry not sure how to markup properly in a comment" - yea, that's tricky. but we're here to help ;)

berak gravatar imageberak ( 2016-06-23 01:52:18 -0600 )edit

indeed, the problem seems to be at the point, where you acquire your Rects.

if that was e.g. from face detection, you have to make sure, it actually found (at least) one, else skip the roi-drawing

berak gravatar imageberak ( 2016-06-23 01:55:38 -0600 )edit

I'm checking for .empty() now, that seems to do the trick. Sometimes when i put my hand in front of my face, or it gets a false positive, it would crash because it wouldn't get a face. Not sure if that's expected behaviour or not (shouldn't i always get a image?).

atv gravatar imageatv ( 2016-06-23 01:59:47 -0600 )edit

"shouldn't i always get a image?" -- don't know, if i understand this one.

maybe it's the wrong question ? your vector<Rect> will be empty, if it did not find any faces, that's expected.

then, (no idea why, btw), there might be empty [0x0] Rects, too. maybe you need another check like faces[i].area() > 100

berak gravatar imageberak ( 2016-06-23 02:10:34 -0600 )edit

also, you could try to increase the minNeighbours param in the face detection, to get less false positives.

berak gravatar imageberak ( 2016-06-23 02:12:24 -0600 )edit

I might do that. And yes i meant if i did not find any faces, the vector<rect> will be empty. Thanks berak, appreciate it.

atv gravatar imageatv ( 2016-06-23 04:31:08 -0600 )edit