Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My simple Python code is this

import cv2  img=cv2.imread('Materials/shapes.png')  blur=cv2.GaussianBlur(img,(3,3),0) gray=cv2.cvtColor(blur,cv2.COLOR_BGR2GRAY) returns,thresh=cv2.threshold(gray,80,255,cv2.THRESH_BINARY)  ret,contours,hierachy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)  for cnt in contours:      area=cv2.contourArea(cnt) #contour area          if (area>1220):         cv2.drawContours(img,[cnt],-1,(0,255,0),2)         cv2.imshow('RGB',img)         cv2.waitKey(1000)         print(len(cnt))  import numpy as np  contours=np.array(contours)  print(contours) 

This worked fine. But recently without me even making any changes. This was throwed to me

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

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

like image 201
Indunil Aravinda Avatar asked Jan 12 '19 22:01

Indunil Aravinda


People also ask

How do I fix ValueError is not enough values to unpack expected 3 got 2?

The “ValueError: not enough values to unpack” error is raised when you try to unpack more values from an iterable object than those that exist. To fix this error, make sure the number of values you unpack from an iterable is equal to the number of values in that iterable.

What is find contours in OpenCV?

Contours are defined as the line joining all the points along the boundary of an image that are having the same intensity. Contours come handy in shape analysis, finding the size of the object of interest, and object detection. OpenCV has findContour() function that helps in extracting the contours from the image.


1 Answers

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) 
like image 88
Rami Isam Avatar answered Sep 19 '22 05:09

Rami Isam