在 python 中使用 cv2.findContours() 时出错

新手上路,请多包涵

我最近开始在 Python 上学习 OpenCV。

我在这里指的是 教程,以获取有关获取图像轮廓的一些帮助。

我的代码是 -

 import cv2
import numpy as np

img = cv2.imread('shapes.jpg', 0)
img = cv2.medianBlur(img, 5)
thresh =     cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)

cv2.imshow('Thresh', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

image, contours, hierarchy =   cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(image, countours, -1, (0,255,0), 3)
cv2.imshow('Contours', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

第一个阈值图像出现,但之后我收到一条错误消息

Traceback (most recent call last):
  File "contours.py", line 21, in <module>
    image, contours, hierarchy =     cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
ValueError: need more than 2 values to unpack

解决此问题的任何帮助将不胜感激。

原文由 Souvik Saha 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 635
2 个回答

看看 这个 例子。

 cv2.findContours(...)

仅返回两个对象,您正试图将其解压缩为三个。

将该行更改为:

 contours, hierarchy =   cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

它应该工作。

原文由 will 发布,翻译遵循 CC BY-SA 3.0 许可协议

您链接的教程适用于 OpenCV version 3cv2.findContours 在该版本中确实返回 3 个对象。

所以要么更新 opencv 要么使用@will 的解决方案。

原文由 ham 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题