我遇到了一个奇怪的行为,虽然我可以解决它,但我想知道为什么会这样。
当我使用cv2.findContours时,它会修改原始图像,即使我没有将它传递给函数。这是一个可以找到图片的最小例子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)
当我用cv2.findContours注释掉这一行时,它运行正常。那是为什么?
答案 0 :(得分:2)
这是因为temp
是 thresh
。
在python中,当你进行类似的任务时,你没有处理对象,你只是在做一个新的引用。
查看copy
模块以实现目标。