---------------------------------------------------------------------------
ValueError跟踪(最近一次通话)
在
1 im = image_list[4]
2 ret,thresh = cv2.threshold(im,127,255,0)
----> 3 image, contours, hierarchy = cv2.findContours(thresh , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)
4 image = cv2.drawContours(image, contours, -1, (0,255,0), 3)
5
ValueError:没有足够的值可解包(预期3,得到2)
最佳答案
不同版本的OpenCV从cv2.findContours返回不同数量的项目。
OpenCV 4和OpenCV 2具有相似的行为,返回两个项目,而OpenCV 3返回三个项目。
您的版本显然只需要2个项目。所以尝试
contours, hierarchy = cv2.findContours(thresh , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)
或者如果您想要某种版本无关的东西,那么如果您需要层次结构使用
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = contours[1] if len(contours) == 2 else contours[2]
contours = contours[0] if len(contours) == 2 else contours[1]
或者如果您只是想要轮廓
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
关于python - findContours是否有理由给我一个错误,尽管它不应该这样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62272551/