Ask Your Question
1

Possible solutions for OpenCv Assertion Error problem

asked 2013-09-25 23:41:27 -0600

utkarshmankad gravatar image

updated 2013-09-26 01:37:03 -0600

berak gravatar image

I am trying to write a simple module for Image Cropping - here's my code

cv::Mat img = input_frame;
cv::Mat finalImg;

// Set Region of Interest to the area defined by the box
int h = input_frame.rows;
int w = input_frame.cols;

cv::Rect roi(skeletonBound[0].x,skeletonBound[0].y,abs(skeletonBound[3].x - skeletonBound[0].x),abs(skeletonBound[3].y - skeletonBound[0].y));

if(!(roi.height <= h) && !(roi.width <= w))
{

    return -1;
}
else
{
    cv::Mat crop = input_frame(roi);
    cv::imshow("crop", crop);
    cv::imwrite("cropped.png", crop);
    cv::Mat finalImg(crop);

    finalImageToBeusedForDetection = finalImg; 
    cout << "Success : "; 
    return 1;
}

I am getting this error -

image description

I checked and rechecked the dimensions, they are correct. i am successfully able to run this code for about early 10-15 frames, later at certain point of time, the code crashes abruptly with above error log. There seems to be a very small problem which i am not able to figure out.

Previously, I was also getting the error where ROI was greater than the size of input image. In that scenario also, the above code worked fine for first 10-15 frames, and later it crashed. I couldn't figure out the problem and hence discarded the erroneous result frame.

I am using latest versions of OpenCv for C++ in Visual studio 2012.

Please help me out on this. I tried all web resources, and have spent pretty lot of time on the same.

edit retag flag offensive close merge delete

Comments

This assertion error somehow indicates that size of your matrix is corrupted. Also, you did not post the whole code snippet for cropping, right? Using Mat frame_copy = frame; can be sometimes a little tricky simply because you create a new frame_copy header that points to the same image as frame header. So if you make changes on frame_copy that means that frame will be affected too. So if your aim is to copy the data you need to use frame.copyTo(frame_copy).

Vladimir Cernega gravatar imageVladimir Cernega ( 2013-09-26 01:35:39 -0600 )edit

Thanks Vladimir, I made the changes in my code as you mentioned. I got a slight improvement in performance, but the problem still persists. The cropping is just a small part of the whole functionality. Hence i didn't paste the code involving the dependency.

utkarshmankad gravatar imageutkarshmankad ( 2013-09-26 01:54:45 -0600 )edit

I am checking the ROI creation line.

utkarshmankad gravatar imageutkarshmankad ( 2013-09-26 01:55:16 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-09-26 02:24:58 -0600

utkarshmankad gravatar image

Thank you very very much. I was successfully able to solve the problem. As per suggestion given by Vladislav Vinogradov and Moster, i modified my earlier condition to check the ROI. The new condition is as follows -

if(roi.x >= 0 && roi.y >= 0 && roi.width + roi.x < input_frame.cols && roi.height + roi.y < input_frame.rows)
{
    // your code

}
else
    return -1;

The assert problem is successfully solved here. Thanks a lot Moster and Vladislav Vinogradov.

edit flag offensive delete link more
0

answered 2013-09-26 01:41:49 -0600

Vladislav Vinogradov gravatar image

Check that roi is correct:

if (roi.x >= 0 && row.y >= 0 && roi.width < input_frame.cols && roi.height < input_frame.rows)
{
    // your code
}
else
{
    return -1;
}
edit flag offensive delete link more

Comments

This will probably still not be successful. It could happy that roi.width < img.cols, but it will still fail. This happens when roi.x + roi.width > img.cols. So the check should be roi.x + roi.width < img.cols and the same for the height. Thats also what the assert says.

Moster gravatar imageMoster ( 2013-09-26 01:46:10 -0600 )edit

Question Tools

Stats

Asked: 2013-09-25 23:41:27 -0600

Seen: 15,452 times

Last updated: Sep 26 '13