# How to perform changes in both sub-image and original image ?

I have limited a region of interest in a binary image with a rectangle . I have convert this rectangle to a new matrix . now whenever I perform an operation in this matrix there is no change in the original image , but what I want is to have a change in the original image .

     // region of interest
Rect roi(maxloc.x,maxloc.y, m2.cols,m2.rows);
//new matrix
Mat image_roi = m1(roi);
//perform operations
bitwise_not(image_roi,image_roi);
image_roi=choice(image_roi);
imshow("image",m1);//there is no change in m1

edit retag close merge delete

Sort by » oldest newest most voted

Try

Mat image_roi(m1,roi);


more

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.

more

Thanks, it worked for me

Mat m1=image;
Rect roi(r.x,r.y, r.width,r.height);
Mat image_roi(m1,roi);
imshow("image",image_roi);

more

Hi jawahar, your intesion was clear but problem in you code. That is you need to declare the matrix(mat) object coordinates then your will get expected output, here you missed out the mat object coordinates

Thank you, its gov...

more

Official site

GitHub

Wiki

Documentation