Reputation: 1518
I'm encountering a odd behaviour and while I can work around it I would like to know why this is.
When I use cv2.findContours it modifies the original image, even if I haven't passed it to the function. Here is a minimal example where the picture can be found here.
import matplotlib.pyplot as plt
import cv2
img =cv2.imread('a.jpg',0)
a1=plt.subplot(121)
plt.imshow(img, cmap='Greys')
ret, thresh = cv2.threshold(img,57,255,cv2.THRESH_BINARY)
a1=plt.subplot(122)
plt.imshow(thresh, cmap='Greys')
plt.show()
temp=thresh
del thresh
contours, hierarchy = cv2.findContours(temp,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
When I comment out the line with cv2.findContours it works fine. Why is that?
Upvotes: 0
Views: 1989
Reputation: 4295
It happens because temp
is thresh
.
In python when you make an assignment like that you are not coping the object, you are just making a new reference.
Take a look at copy
module to achieve your goal.
Upvotes: 2