OpenCV Contours - need more than 2 values to unpack

匿名 (未验证) 提交于 2019-12-03 02:15:02

问题:

I am trying to implement contours using the following code..

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) 

but i am continously getting the following error.

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 

do the function findContours need more arguements?? wht could i do to correct it.

beginner

回答1:

In OpenCV 2, findContours returns just two values, contours and hierarchy. The error occurs when python tries to assign those two values to the three names given on left in this statement:

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


回答2:

It now returns three values:

findContours(image, mode, method[, contours[, hierarchy[, offset]]]) 

return image, contours, hierarchy



回答3:

findContours returns just three values image, contours and hierarchy in opencv3

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


回答4:

-findContours returns only two values. so use just,

So use

contours, hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!