0
votes

I am using this code to load/show/write images (opencv_python-3.3.0-cp36-cp36m-win32):

import cv2
img0 = cv2.imread('original.jpg',1)
img1=img0
for i in range(img0.shape[0]):
    for j in range(img0.shape[1]):
        img1[i,j]=[0,0,255]
cv2.imshow('original',img0)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note that line 7 is supposed to show the original image img0, but it shows the modified image img1 instead (i.e. a red rectangle). Line 3 is supposed to create a temporary copy of img0, not to modify img0. What is wrong here?

1

1 Answers

0
votes

When you are using assignment operator (=) between mat variables, you are not actually copying the data but sharing the reference. Hence change in one is getting reflected in another. Please checkout this : http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat-operator

You need to use clone() or copyTo() to achieve what you want. Check them out here : http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat-clone