Pra*_*ava 18 python opencv python-2.7
我正在尝试使用以下代码实现轮廓..
im = cv2.imread('C:\Users\Prashant\Desktop\T.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContour(im, contours, -1, (0,255,0), 3)
cv2.imshow('Image1',img)
Run Code Online (Sandbox Code Playgroud)
但我不断收到以下错误.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/Prashant/.spyder2/.temp.py", line 17, in <module>
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ValueError: need more than 2 values to unpack
Run Code Online (Sandbox Code Playgroud)
做函数findContours需要更多的论证?我可以做些什么来纠正它.
War*_*ser 35
在OpenCV 2中,findContours只返回两个值,contours和hierarchy.当python尝试将这两个值分配给此语句左侧给出的三个名称时,会发生错误:
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)
小智 5
它现在返回三个值:
findContours(image, mode, method[, contours[, hierarchy[, offset]]])
Run Code Online (Sandbox Code Playgroud)
返回图像、轮廓、层次结构