0

I'm having a problem with this tutorial: https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html Indeed, I encounter this error when I try to execute this code:

import numpy as np
import cv2

img = cv2.imread("onepoint.png", 0)

canny_img = cv2.Canny(img,150,200)
ret,thresh = cv2.threshold(canny_img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]
M = cv2.moments(cnt)
print M

Here is the error:

  File "contours.py", line 13, in <module>
    contours,hierarchy = cv2.findContours(thresh, 1, 2)
ValueError: too many values to unpack

I do not understand why this error occurs. I understand that cv2.findContours (thresh, 1, 2) returns to me 3 "arrays", but why? If a person at the kindness to explain to me I am a taker :(

I'm trying to frame the words of this image:

picture

I'm totally new to OpenCV, I guess you'll understand,

Thank in advance

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Corentin Mar
  • 125
  • 1
  • 7
  • Possible duplicate of [OpenCV python: ValueError: too many values to unpack](https://stackoverflow.com/questions/25504964/opencv-python-valueerror-too-many-values-to-unpack) – alkasm Feb 05 '18 at 18:03
  • I looked at this link before posting my question, but I did not find a clear explanation.. – Corentin Mar Feb 05 '18 at 18:05

1 Answers1

0

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.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • I understand thank you :) But why it returns me 3 arrays and not 2 as on the tutorial? – Corentin Mar Feb 05 '18 at 18:07
  • @CorentinMar because the tutorial was written for an older version of OpenCV which only returns two values (as Jan mentioned, the function has changed in OpenCV 3). Note in the [3.0 documentation](https://docs.opencv.org/3.0-beta/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours) you can see that there are three return values and not two, [as there was in 2.4](https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours). – alkasm Feb 05 '18 at 22:00