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.
Python / Contour OpenCV returns too many values to unpack
You'll need to change it to
im, contours,hierarchy = cv2.findContours(thresh, 1, 2)
Or, if you only care about the contours:
(_, contours, _) = cv2.findContours(thresh, 1, 2)
The function has changed in OpenCV 3
.
ValueError: too many values to unpack in OpenCV project
Depending on the cv2 version, this function may return a different number of elements.
The problem is that you are using cv2 version 3, and not version 2.
First of all, what is your version?
Use:
import cv2
cv2.__version__
To solve the error try first this:
(_, cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Documentation 3.4.1
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)
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
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)
Error ValueError: too many values to unpack (expected 4) on official structured edge detection and edgeboxes example
The method returns (boxes, scores)
, not just the boxes. That seems to have been changed between 2017 and 2019.
You should use this code:
(boxes, scores) = edge_boxes.getBoundingBoxes(edges, orimap)
Then the loop will work.
You encountered this issue because you're using an outdated link to that example. The link you have shows a version from 2017. That old code would work with a 2017/2018 release of OpenCV.
The current version of the example, from 2019, corresponds to the current API. The current example shows the proper usage for the current API.
Here is the change: https://github.com/opencv/opencv_contrib/commit/6ae9809b2e464b72810298b10c6cf4935886a0f1
Related Topics
Pycharm Import External Library
How to Force/Ensure Class Attributes Are a Specific Type
When Is Not a Good Time to Use Python Generators
What Exactly Is the Point of Memoryview in Python
Removing Control Characters from a String in Python
How to Check If One Dictionary Is a Subset of Another Larger Dictionary
Mixing Cdef and Regular Python Attributes in Cdef Class
How to Install the Yaml Package for Python
Building Lxml for Python 2.7 on Windows
Do You Use the "Global" Statement in Python
Importerror: No Module Named 'Django.Core.Urlresolvers'
Django - How to Make a Variable Available to All Templates
How to Resize an Image with Opencv2.0 and Python2.6
"Private" (Implementation) Class in Python
How to Convert Escaped Characters
How to Efficiently Handle European Decimal Separators Using the Pandas Read_CSV Function