ocr-text-extraction icon indicating copy to clipboard operation
ocr-text-extraction copied to clipboard

ValueError: too many values to unpack in cv2.findContours

Open TheMatt2 opened this issue 7 years ago • 13 comments

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]

TheMatt2 avatar Jun 15 '17 03:06 TheMatt2

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

ossiach avatar Aug 14 '17 14:08 ossiach

Yes above solution worked for me too.

bhosalems avatar Dec 23 '17 06:12 bhosalems

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

amar74 avatar Mar 29 '18 18:03 amar74

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.

pallavipundir avatar Apr 09 '18 08:04 pallavipundir

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

plusminuschirag avatar May 16 '18 16:05 plusminuschirag

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

pallavipundir avatar May 16 '18 16:05 pallavipundir

(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.

vrajchauhan0 avatar Jul 06 '18 07:07 vrajchauhan0

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

pallavipundir avatar Jul 10 '18 11:07 pallavipundir

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

pallavipundir avatar Jul 10 '18 11:07 pallavipundir

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.

lmiller1990 avatar Jul 28 '18 02:07 lmiller1990

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 ;)

bharath5673 avatar Jan 17 '19 13:01 bharath5673

@bharath5673 this works ,thank u

rutujar avatar Apr 22 '19 09:04 rutujar

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)

Sanjeeth8733 avatar Nov 02 '19 07:11 Sanjeeth8733