Ask Your Question
0

draw rectangle change image property unexpected!

asked 2017-10-13 13:18:06 -0600

mimi gravatar image

updated 2017-10-14 17:08:08 -0600

Hi, I have a question regarding the drawing function "rectangle" from opencv 3.2. I think there is a mistake.

The following methods suppose not to change the input "img", since it is a copy of "img" pass to the method. But the "rectangle" method change the property of the input "img" even no reference as input pass to the method.

See my example:

// input is a copy of image
void drawPatch (cv::Mat img){
       rectangle (.....);
}
void drawCorner(cv::Mat img){
   circle(....);
}

int main(){

cv::Mat img = imread('1.jpg');

imshow("1", img);
drawCorner(img);       
imshow("2", img;)         // it shows the original img, without drawn corners. no reference as input.
drawPatch(img);         
imshow("3", img);        // it shows the drawn rectangle even no reference as input.  


return 0;
}

I hope that i have make my point clear. Please let me know if there is a mistake. Thx.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-10-13 15:09:07 -0600

LBerger gravatar image

updated 2017-10-14 02:22:25 -0600

drawPatch (cv::Mat img) or drawCorner(cv::Mat img) it's not a copy of an image, it is a copy of all Mat member : member data is copied too then pixels are at same location in memory.

If you want a copy you must clone image before calling function :

Mat img2=img.clone(),img3=img.clone();
imshow("1", img);
drawCorner(img2);       
imshow("2", img2);         // it shows the original img, with drawn corners. no reference as input.
drawPatch(img3);         
imshow("3", img3);        // it shows the drawn rectangle even no reference as input.
edit flag offensive delete link more

Comments

Sorry, I think I wrote something wrong in the original post. It should be on the Line

imshow("2", img2); // after drawCorner() , It shows the original image without drawn corner. Since the drawing function only affect the object inside of the method without return or reference input "&". But on the line

imshow ("3", img2); // it shows the unexpected drawn rectangle on the image.

A second try i did in the method was add "cv::Mat dst = img; rectangle(dst, ....)" but it shows the same result with unexpected rectangle in the main function.

mimi gravatar imagemimi ( 2017-10-14 17:07:48 -0600 )edit

please post the whole code

sturkmen gravatar imagesturkmen ( 2017-10-14 17:12:54 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-13 13:18:06 -0600

Seen: 362 times

Last updated: Oct 14 '17