我最近开始在 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 许可协议
看看 这个 例子。
仅返回两个对象,您正试图将其解压缩为三个。
将该行更改为:
它应该工作。