# inconsistent behavior about ROI calculation

As we know:

Mat img(7, 8, CV_8UC1, Scalar(0));
Mat com = (Mat_<uchar>(4, 3) << 255, 0, 0, 255, 255, 0, 255, 255, 0, 255, 255, 255);
img(Rect(1, 2, 3, 4)) += com;


We will get a img like this:

This is out understandable behavior because we do a calculation in the same ROI. But why this code:

Mat img(7, 8, CV_8UC1, Scalar(0));
Mat com = (Mat_<uchar>(4, 3) << 255, 0, 0, 255, 255, 0, 255, 255, 0, 255, 255, 255);
img(Rect(1, 2, 3, 4)) = com;


cannot get a same result? The img is empty image still:

I'm confusion about it. Or do I have missed something?

edit retag close merge delete

What result you get with the latter?

( 2019-05-27 04:18:33 -0500 )edit

@HYPEREGO I have edited the question.

( 2019-05-27 04:46:11 -0500 )edit

Sort by ยป oldest newest most voted

Ok got it. In first case you do a sum + operation img(Rect(1, 2, 3, 4)) += com;, so a sum is performed and the result is wrote back using the existing data (i.e. in the origianl img). In that case the data is wrote back and this is the expected behaviour because it is basically (img1 = img1 + img2) so it's a sum of two matrices.

In the second case I think that the assignment is not performed afterall, just should use copyTo. In fact, what you do is extract the rectangle from img but then it cannot be referenced to the com matrix since the rectangle gonna be destroyed after the call. Basically, when you use the = operator with Mat it is just a "soft copy", you create a reference to the Mat container without copying data, so sometimes things can be different from what you expect.

more

2

this will do the trick:

com.copyTo(img(Rect(1, 2, 3, 4)));

( 2019-05-27 06:08:33 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2019-05-27 01:31:45 -0500

Seen: 57 times

Last updated: May 27