Ask Your Question
0

Mat out of bounds

asked 2018-11-14 20:54:25 -0500

updated 2018-11-15 01:01:06 -0500

berak gravatar image

I have suceesfully drew a rectanlge over the face in Android. Inorder to crop the selected face I used the following code:

Mat cropped_face = image;
Rect cropped= new Rect(facesArray[i].tl(), facesArray[i].br());
cropped_face = new Mat(image,cropped);

The 'image' variable holds the original image from the camera. I encountered following error on the last line of code.

AndroidRuntime: FATAL EXCEPTION: Thread-6
    Process: com.example.kiran.opencv, PID: 5573
    CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.1) /build/master_pack-android/opencv/modules/core/src/matrix.cpp:418: error: (-215) 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows in function cv::Mat::Mat(const cv::Mat&, const cv::Range&, const cv::Range&)
    ]
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-11-15 01:10:14 -0500

berak gravatar image

CascadeDetector detections can be partly out of the original image, see docs

it's not a problem, if you only want to draw something, but if you want to crop image content, you carefully have to check (or even adjust !) the Rect:

Rect r = facesArray[i];
r.x = Math.max(r.x,0);
r.y = Math.max(r.y,0);
r.width = Math.min(image.cols()-1-r.x, r.width);
r.height = Math.min(image.rows()-1-r.y, r.height);
Mat cropped_face = new Mat(image,r); // now you can crop it safely !
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-11-14 20:54:25 -0500

Seen: 19 times

Last updated: Nov 15