Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Contours - need more than 2 values to unpack

Tags:

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 arguments? wht could i do to correct it.

like image 519
Prashant Shrivastava Avatar asked Dec 31 '13 03:12

Prashant Shrivastava


People also ask

How do I combine two contours in OpenCV?

go through the first contour as it is listed until you hit the closest point. Then switch to the other list, starting from the closest point you go clockwise through the other contour until it is used up. switch back to the first contour and append the rest of their points. force them into cv2 contour format.

How does OpenCV find contours work?

To put in simple words findContours detects change in the image color and marks it as contour. As an example, the image of number written on paper the number would be detected as contour. The part that you want to detect should be white like above numbers in 1st image.


2 Answers

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)
like image 62
Warren Weckesser Avatar answered Oct 14 '22 20:10

Warren Weckesser


It now returns three values:

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

return image, contours, hierarchy

like image 21
Yiming Zhou Avatar answered Oct 14 '22 19:10

Yiming Zhou