best way to check Mat empty/size and ROI size

asked 2016-06-25 12:45:48 -0500

atv gravatar image

Is below if a good way to check if i am going out of bounds when copying Mats into a ROI ? Below solved a lot of problems but it still crashes occasionally with:

 Camera dropped frame!
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


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

            }
edit retag flag offensive close merge delete

Comments

could you explain what do you want to do with this code snippet?

sturkmen gravatar imagesturkmen ( 2016-06-25 18:05:58 -0500 )edit

This piece of code puts a small stamp sized picture of the detected face in the rectangle. I use pos_x and pos_y so the small picture moves with the rectangle.

It works fine, but after maybe 10 -20 minutes it crashes with the above error. Without this piece, it doesn't so i can only assume (also because of the ROI assert error that i am doing something or forgot to check something that is causing this) it is this part.

atv gravatar imageatv ( 2016-06-26 02:39:52 -0500 )edit