Error using cv2.findContours () using python

I recently started learning OpenCV in Python.

I am referring to this tutorial here to get some help in getting the image outlines.

My code is

import cv2
import numpy as np

img = cv2.imread('shapes.jpg', 0)
img = cv2.medianBlur(img, 5)
thresh =     cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)

cv2.imshow('Thresh', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

image, contours, hierarchy =   cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(image, countours, -1, (0,255,0), 3)
cv2.imshow('Contours', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The first threshold image appears, but after that I get an error

Traceback (most recent call last):
  File "contours.py", line 21, in <module>
    image, contours, hierarchy =     cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
ValueError: need more than 2 values to unpack

We will be grateful for any help in solving this problem.

+9
source share
2 answers

Take a look at this example.

cv2.findContours(...)

returns only two objects, you are trying to unpack it into three.

change this line to this:

contours, hierarchy =   cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

and that should work.

+14
source

, , OpenCV version 3. cv2.findContours 3 .

opencv, @will.

+8

Source: https://habr.com/ru/post/1659025/


All Articles