2

I am trying to create an ROI above the face detected to place a hat like shown in the image: Plz click here : ROI created above face to place a hat

I have made sure that the ROI created is withing the bounds of the image. It looks like this: // Create an ROI // where face is the detected face ROI

    if (0<=face.x && 0<=face.x-face.width*0.08<=image.cols && 0<=face.x+face.width+face.width*0.08<=image.cols 
         && 0<=face.y && 0<=face.y-face.height*0.28<=image.rows)
    {
      Mat ROI_hat = image(Rect(abs(face.x-face.width*0.08),abs(face.y-face.height*0.28),abs(face.x+face.width+face.width*0.08),abs(face.y)));
      rectangle(image,Point(abs(face.x-face.width*0.08),abs(face.y-face.height*0.28)),Point(abs(face.x+face.width+face.width*0.08),abs(face.y)),Scalar(255, 0, 0), 1, 4);

      cout<<"Within the bounds of Image"<<endl;
    }
    else{
     cout<<" Out of bounds of Image "<<endl;
        }

There are no negative values and for every frame it says ROI is withing the bounds. But I still get assertion error:

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 /home/user/OpenCV_Installed/opencv-3.2.0/modules/core/src/ma‌​trix.cpp, line 522 terminate called after throwing an instance of 'cv::Exception' what(): /home/user/OpenCV_Installed/opencv-3.2.0/modules/core/src/ma‌​trix.cpp:522: 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 Aborted (core dumped)

Can someone please tell me where I'm going wrong?

5
  • 1
    Whats the error's message? Commented Mar 31, 2017 at 13:19
  • 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 /home/user/OpenCV_Installed/opencv-3.2.0/modules/core/src/matrix.cpp, line 522 terminate called after throwing an instance of 'cv::Exception' what(): /home/user/OpenCV_Installed/opencv-3.2.0/modules/core/src/matrix.cpp:522: 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 Aborted (core dumped) Commented Mar 31, 2017 at 13:25
  • Then your ROI is outside of the image... check again your conditions Commented Mar 31, 2017 at 13:26
  • Can u plz elaborate ?? I 'm not able to get what you are trying to say Commented Mar 31, 2017 at 13:29
  • A portion of your ROI falls outside of the image boundaries Commented Mar 31, 2017 at 13:32

1 Answer 1

2

The error means that your ROI is outside of the image, so your conditions are wrong.

Since it's pretty easy to get confused, I usually apply this small trick that is based on the intersection of the roi with a dummy roi roiImg that contains all the image:

Rect roiImg(0, 0, image.cols, image.rows);
Rect roi = ... // Very complex way of setting up the ROI

if( (roi.area() > 0) && ((roiImg & roi).area() == roi.area()) ) {
    // roi is inside the image, and is non-empty
    // VALID roi
} else {
    // roi is at least partially outside of the image, or it's empty
    // INVALID roi
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hy I used the condition above but I get same error. I didnt understand how ((roiImg & roi).area() == roi.area()) helps to evaluate if its inside the image bounds. Also plz take a look at the image I have attached, when I plot the ROI its always inside the image only. After some time it crashes
I started debugging the code line by line and found one point was residing outside the image. So it crashed. I fixed it. Thanks anyway Miki

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.