David Doria
David Doria

Reputation: 10273

Does cv::Mat_ maintain its ROI?

Say I have a 100x100 cv::Mat1b called image. Then I do:

cv::Mat1b subImage = image(cv::Rect(0,0,49,49));

To get the upper left corner of image into subImage. Then say I pass subImage to a function, say cv::findContours. Will the resulting contours be relative to image or subImage? That is, does findContours() know that subImage is actually a sub-image? Or do all OpenCV functions just treat a subImage extracted like this as a "full image" and then it's the callers responsibility to add the corner of the extracted region to each of the coordinates of the contour pixels (in this example case) to get the contour as interpreted in the original image?

Upvotes: 0

Views: 46

Answers (2)

Saransh Kejriwal
Saransh Kejriwal

Reputation: 2533

If you declare subImage as:

Mat1b subImage = image(cv::Rect(0,0,49,49));

then subImage will be taken as a complete Mat object by any OpenCV function. However, any change made in subImage will be reflected in the corresponding portion of image as well.

If you want to exclude the changes made in subImage from image, you should use:

Mat1b subImage = image(cv::Rect(0,0,49,49)).clone();

Now you have a subImage that is not pointing to image

Upvotes: 1

Martin Beckett
Martin Beckett

Reputation: 96147

The sub-image is treated as a complete image, functions called on the sub-image don't know it isn't a complete image.

The findcontour function however does take an optional parameter of the coordinates of the ROI so that contour coordinates are returned relative to the full image Alternatively you can simply add an offset to each contour point

Upvotes: 1

Related Questions