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)
ValueError: not enough values to unpack (expected 3, got 2) (OpenCV)
findContours
method returns two values: contours
and hierarchy
but you're expecting three: _,cnts,_
. Change to this:
cnts, _ = cv2.findContours(thresh_delta.copy(),cv2.RETR_EXTERNAL,cv2.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
on Google Colab with pyhton: ValueError: not enough values to unpack (expected 3, got 2)
To make it easier to answer your question it would be very useful to have the full stack trace so that we can see clearly in which line the code fails.
But, I assume that the error is raised in (new, cnts, _) = cv2.findContours(...)
because cv2.findContours()
only returns a tuple of two (contours and hierarchy). You can see this in the documentation and a use case in their guide.
ValueError in python OpenCV - not enough values to unpack (expected 3, got 2)
As you can see here findContours
only returns the contours and the hierarchy, but not the image itself, as in
contour, hierarchy = cv2.findContours(imthres, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
That's what this error message means.
Python ValueError: not enough values to unpack (expected 3, got 2)
According to cv2.findContours
documentation, the function return only 2 values. You try to unpack 3.
Remove the first _
(unwanted value) from the second line to match the signature from the documentation.
_, threshold = cv2.threshold(gray_roi, 3, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
Generally speaking, when you get this Python error message:
ValueError: not enough values to unpack (expected x got y)
Search where you try to unpack y
elements, and try to fix it by unpacking x
elements.
ValueError: not enough values to unpack (expected 3, got 2) using Contour in OpenCV
In openCV4, cv2.findContours()
returns 2 values contours, hierarchy
, so it should be:
contours, hierarchy = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
How to fix ValueError: not enough values to unpack (expected 3, got 2) in python
(_,cnts,_) = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
findContours
returns a tuple of two objects (contours and hierachy). You are trying to unpack three values (even if you discard the first and third).
I imagine from the name you've used, you want
cnts, _ = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
Related Topics
How to Dynamically Compose an or Query Filter in Django
Could Not Find a Version That Satisfies the Requirement Tensorflow
Want to Find Contours -> Valueerror: Not Enough Values to Unpack (Expected 3, Got 2), This Appears
Plotting a Decision Boundary Separating 2 Classes Using Matplotlib's Pyplot
Print a String as Hexadecimal Bytes
How to Pipe a Subprocess Call to a Text File
How to Get an Event Callback When a Tkinter Entry Widget Is Modified
Scikit-Learn & Statsmodels - Which R-Squared Is Correct
Django Db Settings 'Improperly Configured' Error
Character Reading from File in Python
How to Clamp an Integer to Some Range
Python Subprocess.Call a Bash Alias
Why Do Some Functions Have Underscores "_" Before and After the Function Name
How to Do Multiple Arguments to Map Function Where One Remains the Same