Ask Your Question

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

asked 2013-05-27 12:33:59 -0500

Jawaher Khemakhem gravatar image

updated 2015-10-04 07:52:25 -0500

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 
     imshow("image",m1);//there is no change in m1
edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted

answered 2013-05-27 13:04:51 -0500

yes123 gravatar image


Mat image_roi(m1,roi);

But your solution should already work too

edit flag offensive delete link more

answered 2013-05-27 13:19:54 -0500

rics gravatar image

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.

edit flag offensive delete link more

answered 2014-10-05 18:01:05 -0500

Sebita gravatar image

Thanks, it worked for me

Mat m1=image;
Rect roi(r.x,r.y, r.width,r.height);
Mat image_roi(m1,roi);
edit flag offensive delete link more

answered 2014-10-06 01:28:12 -0500

govardhana padavala gravatar image

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

edit flag offensive delete link more

Question Tools


Asked: 2013-05-27 12:33:59 -0500

Seen: 2,251 times

Last updated: Oct 06 '14