picture in picture roi crash assertion failed [closed]
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
how large is your image ?
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.
Abort trap:
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.
"Sorry not sure how to markup properly in a comment" - yea, that's tricky. but we're here to help ;)
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
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?).
"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
also, you could try to increase the
minNeighbours
param in the face detection, to get less false positives.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.