20
votes

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.

9
Your syntax is actually correct for OpenCV 3.0-beta or later, but you are probably using a stable version 2.8 or something. - Dimitris
The behaviour is different across different versions of OpenCV (OpenCV2, 3, 4). I have answered it in detail below. - Vishal

9 Answers

39
votes

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)
4
votes

It now returns three values:

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

return image, contours, hierarchy

3
votes

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

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

As of the year 2019, we have three versions of OpenCV (OpenCV2, OpenCV3, and OpenCV4).

OpenCV4 and OpenCV2 have similar behavoiur (of returning two values from cv2.findContours). Whereas OpenCV3 returns three values.

if cv2.getVersionMajor() in [2, 4]:
    # OpenCV 2, OpenCV 4 case
    contour, hier = cv2.findContours(
                    thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
else:
    # OpenCV 3 case
    image, contour, hier = cv2.findContours(
                    thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
2
votes
  • findContours returns only two values. so use just,

So use

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

This should help:

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

Depending on the OpenCV version, cv2.findContours() has varying return signatures. In OpenCV 3.4.X, cv2.findContours() returns 3 items. In OpenCV 2.X and 4.1.X, cv2.findContours() returns 2 items

You can easily obtain the contours regardless of the version like this:

cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
0
votes

Python version 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]

NumPy version: 1.16.1

argparse version: 1.1

CV2 version: 4.0.0

Traceback (most recent call last):

  File "omr.py", line 254, in <module>

    main()

  File "omr.py", line 237, in main

    answers, im = get_answers(args.input)

  File "omr.py", line 188, in get_answers

    contours = get_contours(im)

  File "omr.py", line 26, in get_contours

    im2, contours, hierarchy =cv2.findContours(image_gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

ValueError: need more than 2 values to unpack

This is resolved by removing 'im2 ,' from line 26.. as in OpenCv version 3.0 or higher, the function 'findContours' returns only 2 values.. so the statement should be

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

and also upgrade your OpenCv version

0
votes

As per the Current Opencv Versions cv2.findContours returns 2 Values and that is Contours and heirachy. Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition.