1

I tried to draw contour in the image, got error to solve.

import cv2
img = cv2.imread('image7.jpg')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('grey',imgray)
ret, thresh = cv2.threshold(imgray, 50, 255, 0)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print("Number of contours = {}".format(str(len(contours))))
print('contours {}'.format(contours[0]))

cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.drawContours(imgray, contours, -1, (0, 255, 0), 3)
cv2.imshow('Image', img)
cv2.imshow('Image GRAY', imgray)
cv2.waitKey(0)

This is the error message:

Error:
    ValueError                                Traceback (most recent call last)
<ipython-input-1-e0ee08be0330> in <module>
      6 ret, thresh = cv2.threshold(imgray, 50, 255, 0)
      7 
----> 8 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
      9 print("Number of contours = {}".format(str(len(contours))))
     10 print('contours {}'.format(contours[0]))

ValueError: too many values to unpack (expected 2)
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
zma
  • 59
  • 7
  • 2
    Does this answer your question? [ValueError: too many values to unpack python 2.7](https://stackoverflow.com/questions/30924665/valueerror-too-many-values-to-unpack-python-2-7) – Cris Luengo Dec 12 '20 at 05:52
  • 2
    This question has been asked hundreds of times, please search before asking. See also here for the reverse error: https://stackoverflow.com/q/40278444/7328782 (`findContours` changed from 3 to 2 outputs, it depends on which version of OpenCV you use, how many outputs it generates). – Cris Luengo Dec 12 '20 at 05:53

0 Answers0