Sou*_*aha 9 python opencv image-processing
我最近开始在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()
Run Code Online (Sandbox Code Playgroud)
第一个阈值图像出现,但之后我得到一条错误信息
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
Run Code Online (Sandbox Code Playgroud)
任何帮助解决此问题将不胜感激.
wil*_*ill 16
看看这个例子.
cv2.findContours(...)
Run Code Online (Sandbox Code Playgroud)
只返回两个对象,你试图将它解压缩为三个.
将该行更改为:
contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)
它应该工作.