Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I've as of late began learning OpenCV on Python. 

I'm alluding to this instructional exercise here, to get some assistance on getting the forms of a picture. 

My code:

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 thresholded picture is showing up, yet after that, I get a mistake message as

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

Anyone, please me to resolve this issue.

1 Answer

0 votes
by (26.4k points)

You can use below code:

cv2.findContours(...)

just returns two objects, you're attempting to unload it into three. 

change that line to this:

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

It will work.

Are you interested to learn the concepts of Python? Join the python training course fast!

To know more about this you can have a look at the following video tutorial:-

Browse Categories

...