Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Your rectangle is getting out of the image, maybe with one pixel, and that is the problem.

Your rectangle is getting out of the image, maybe with one pixel, and that is the problem. I had the same case many times. I have solved it by adding a line like this:

cv::Rect roi = _rectangle & cv::Rect(0, 0, image.width, image.height);

Your rectangle is getting out of the image, maybe with one pixel, and that is the problem. I had the same case many times. I have solved it by adding a line like this:

cv::Rect roi = _rectangle & cv::Rect(0, 0, image.width, image.height);

and then add your line:

Mat crop = frame(roi);

If you would like not to create another rect, then just update the existing one:

_rectangle = _rectangle & cv::Rect(0, 0, image.width, image.height);

Your rectangle is getting out of the image, maybe with one pixel, and that is the problem. I had the same case many times. I have solved it by adding a line like this:

cv::Rect roi = _rectangle & cv::Rect(0, 0, image.width, image.height);

and then add your line:

Mat crop = frame(roi);

If you would like not to create another rect, Rect, then just update the existing one:

_rectangle = _rectangle & cv::Rect(0, 0, image.width, image.height);

I also wonder if this should be an implicit thing in the ROI function or not...

Imagine that your algorithm of detection of the Rect has a bug and it is computing it in a wrong case (like 2 times bigger, or shifted, or something else), and then you are getting always wrong results and you are concluding that the approach is bad or that OpenCV is not correct, or I don't know. I find this as a plus. Even if in the case of some problems because the approximations, you are getting a pixel more, that is getting out of the image...