Ask Your Question
3

ROI out of bounds issue

asked 2015-09-16 02:44:20 -0600

Nbb gravatar image

updated 2017-01-30 22:12:38 -0600

Hello forum,

I would like to crop out a part of my image using

Mat crop = frame(_rectangle);

The problem is, I get an error when _rectangle is out of bounds of my 1920 x 1080 frame. Is there a simple function that I could use to just crop out the section where the Rect and the Mat intersects ? I could write my own function for it but it would be simpler if opencv has a built-in function to resolve the out of bounds issue.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
6

answered 2015-09-16 03:39:40 -0600

thdrksdfthmn gravatar image

updated 2015-09-16 03:49:15 -0600

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);

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...

edit flag offensive delete link more

Comments

1

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

getRectSubPix might be an alternative then.

berak gravatar imageberak ( 2015-09-16 05:28:54 -0600 )edit
1

Thank you that did it :) I didn't google about the & operator for opencv

Nbb gravatar imageNbb ( 2015-09-16 07:59:19 -0600 )edit

It would be nice if you approve my answer...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-09-17 06:59:37 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2015-09-16 02:44:20 -0600

Seen: 8,681 times

Last updated: Jan 30 '17