Ask Your Question
1

inconsistent behavior about ROI calculation

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

yode gravatar image

updated 2019-05-27 04:45:31 -0500

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 flag offensive close merge delete

Comments

What result you get with the latter?

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

@HYPEREGO I have edited the question.

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

1 answer

Sort by ยป oldest newest most voted
3

answered 2019-05-27 05:15:57 -0500

HYPEREGO gravatar image

updated 2019-05-27 05:28:59 -0500

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.

edit flag offensive delete link more

Comments

2

this will do the trick:

com.copyTo(img(Rect(1, 2, 3, 4)));
Witek gravatar imageWitek ( 2019-05-27 06:08:33 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 52 times

Last updated: May 27