Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Although your example is not complete I think the problem is that you use m1 instead of m2 in the line

Mat image_roi = m1(roi);

OpenCv does not reallocate matrices for efficiency reasons, only when it has to. As m1 is not initialized in your code snippet it needs to be allocated independently of m2. If you use the following code

Mat image_roi = m2(roi);

then image_roi would use the same memory area as m2 but with a different matrix header so modifications of the roi would alter the original image.

See the Mat tutorial for details.