Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: too many values to unpack in cv2.findContours #5

Open
TheMatt2 opened this issue Jun 15, 2017 · 13 comments
Open

ValueError: too many values to unpack in cv2.findContours #5

TheMatt2 opened this issue Jun 15, 2017 · 13 comments

Comments

@TheMatt2
Copy link

For whatever reason, cv2.findContours on line 211 returns three arguments instead of the expected 2. From playing with it, I found that by eliminating the first value would solve the problem.
I am not knowledgeable about cv2 so if this is something obvious someone please say so. (MacOS, cv2 '3.2.0').
Here is the fix that worked for me.
result = cv2.findContours(edges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, hierarchy = result if len(result) == 2 else result[1:3]

@ossiach
Copy link

ossiach commented Aug 14, 2017

Adding a third value in front of the other two also solved the problem for me.
For example:
_, contours, hierarchy = cv2.findContours(edges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

Apparently findContours got changed some time ago:
https://stackoverflow.com/questions/25504964/opencv-python-valueerror-too-many-values-to-unpack

@bhosalems
Copy link

Yes above solution worked for me too.

thubtenrigzin added a commit to thubtenrigzin/namsel2Mac that referenced this issue Feb 23, 2018
thubtenrigzin added a commit to thubtenrigzin/namsel2Mac that referenced this issue Feb 23, 2018
thubtenrigzin added a commit to thubtenrigzin/namsel-ocr that referenced this issue Mar 2, 2018
@amar74
Copy link

amar74 commented Mar 29, 2018

conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
ValueError: too many values to unpack (expected 2)

sir please tell me where I'm wrong

@pallavipundir
Copy link

Just initialize with one more variable in front of other two on L.H.S.For example:-
_,conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

I've used _(Underscore) here.You can give any name or simply use it as underscore only.

@plusminuschirag
Copy link

Is it safe to say that find Contours needs three variables to store the returned value?

@pallavipundir
Copy link

Ofcourse it is.Don't worry, it is safe to say this.

@vrajchauhan0
Copy link

vrajchauhan0 commented Jul 6, 2018

(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Not able to understand. Please help me. giving error too many values to unpack.

@pallavipundir
Copy link

Use like this:-
(_, cnts, h)= cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

@pallavipundir
Copy link

Because you cannot unpack 3 values from the tuple and place them in a tuple of two,which you are doing.

@lmiller1990
Copy link

lmiller1990 commented Jul 28, 2018

What is the third value, though? According to the docs, only two should be returned. https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours

Edit: I am using opencv3, cannot find the docs for findContours in 3.0 though, only 2.4.

@bharath5673
Copy link

bharath5673 commented Jan 17, 2019

EASY FIX ;)

( _ , cnts , _ ) = cv2.findContours(threshFrame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)

try this

( cnts , _ )

python is right.
you cannot unpack 3 values from the turple and place them in a turple of two
###

happy coding ;)

@rutujar
Copy link

rutujar commented Apr 22, 2019

@bharath5673 this works ,thank u

@Sanjeeth8733
Copy link

I'm getting the same error unable to resolve it.

in extract_bv(image)
22 ret,f6 = cv2.threshold(f5,15,255,cv2.THRESH_BINARY)
23 mask = np.ones(f5.shape[:2], dtype="uint8") * 255
---> 24 image, contours, hierarchy = cv2.findContours(f6.copy() ,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE)
25 for cnt in contours:
26 if cv2.contourArea(cnt) <= 200:

ValueError: not enough values to unpack (expected 3, got 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests