Possible solutions for OpenCv Assertion Error problem
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 -
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.
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).
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.
I am checking the ROI creation line.