Ask Your Question

# Revision history [back]

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

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