Too Many Values to Unpack Calling Cv2.Findcontours

OpenCV Python: cv2.findContours - ValueError: too many values to unpack

I got the answer from the OpenCV Stack Exchange site. Answer

THE ANSWER:

I bet you are using the current OpenCV's master branch: here the return statements have changed, see http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours.

Thus, change the corresponding line to read:

_, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Or: since the current trunk is still not stable and you probably will run in some more problems, you may want to use OpenCV's current stable version 2.4.9.

too many values to unpack calling cv2.findContours

It appears that you're using OpenCV version 3.x, while writing code intended for the 2.x branch. There were some API changes between those two branches. Since you're using Python, you have a handy help available -- make sure to use it, along with the documentation.

OpenCV 2.x:

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

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

OpenCV 3.x:

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

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

This means that in your script the correct way to call findContours when using OpenCV 3.x would be something like

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

UPDATE (Dec 2018)

In OpenCV 4.x, findContours returns 2 values only.

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

findContours(...)
findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
. @brief Finds contours in a binary image.

cv.findContours() - ValueError: not enough values to unpack (expected 3, got 2)

"findContours" returns a tuple of two values. The correct usage is:

contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

usage of findContours causes ValueError: not enough values to unpack (expected 3, got 2)

This is a version change. Prior to OpenCV 4.4, findCounters did return three things. Newer versions return 2: contours and hierarchy.

https://docs.opencv.org/4.4.0/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0

ValueError: too many values to unpack (expected 2) in openCV while trying to detect nameplates of cars

There was a change in cv 3.0
Has to do with .findContours returns 3 values now.

https://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html

import cv2 
image = cv2.imread("path")
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(image, 10, 250)

#old way
#(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# 3.0 way
_, cnts, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

idx = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
if w>50 and h>50:
idx+=1
new_img=image[y:y+h,x:x+w]
cv2.imwrite(str(idx) + '.png', new_img)
cv2.imshow("im",image)
cv2.waitKey(0)

OpenCV Contours - need more than 2 values to unpack

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)

Want to find contours - ValueError: not enough values to unpack (expected 3, got 2), this appears

the function cv2.findContours() has been changed to return only the contours and the hierarchy and not ret

you should change it to:

contours,hierachy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)


Related Topics



Leave a reply



Submit