-1

How can I fix it?

import numpy as np
import cv2 as cv
im = cv.imread('good.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

Error

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

Kalyan Mohanty
  • 95
  • 1
  • 10
  • Does this answer your question? [too many values to unpack calling cv2.findContours](https://stackoverflow.com/questions/43960257/too-many-values-to-unpack-calling-cv2-findcontours) – Dan Mašek Jun 12 '20 at 22:53

1 Answers1

1

The problem is in your last line. To better understand it, here's a quote taken from the documentation:

See, there are three arguments in cv2.findContours() function, first one is source image, second is contour retrieval mode, third is contour approximation method. And it outputs the contours and hierarchy. Contours is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x,y) coordinates of boundary points of the object.

So clearly cv2.findContours() only return two arguments (not 3). To fix it just change the last line to:

contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
Amin Guermazi
  • 1,632
  • 9
  • 19