3
import cv2
import numpy as np 

img = cv2.imread("img.jpg")
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255,0)
contours,hierarchy = cv2.findContours(thresh,2,1)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 7, in <module>
    contours,hierarchy = cv2.findContours(thresh,2,1)

When test went there, an error occuredValueError: too many values to unpack, can anyone tell me why it happened since my opencv is 3.0.0 and the findContours return two values as said in Documents and how to solve this problem

After print the result of threshold function, it returns this

(127.0, array([[255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255], ..., [255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255]], dtype=uint8))

2
  • Please print out and report back what cv2.threshold really returns, without your unpacking into (ret, thresh) Commented Mar 17, 2015 at 11:36
  • Can you also post your full traceback? Commented Mar 17, 2015 at 11:38

1 Answer 1

5

"since my opencv is 3.0.0 and the findContours return two values" - you're wrong about this:

>>> help(cv2.findContours)
Help on built-in function findContours:

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

see, it returns 3 values, an additional image (that you should discard)

Sign up to request clarification or add additional context in comments.

2 Comments

oh, yes, thank you. After I add a useless image, the test run well~~
yes, indeed, the image is useless in this case. (but note, that a lot of 3.0 python functions (like all the drawing stuff) differ in the same way, and return an additional image [for chaining, i guess])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.