Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

python - 在Python中使用cv2.findContours()时出错(Error using cv2.findContours(), with python)

I've recently started learning OpenCV on Python.

(我最近开始学习Python上的OpenCV。)

I'm referring to this tutorial here, to get some help on getting the contours of an image.

(我在这里指的是教程,以获取有关获取图像轮廓的帮助。)

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 thresholded image is appearing, but after that I get an error 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

Any help to resolve this issue will be appreciated.

(解决此问题的任何帮助将不胜感激。)

  ask by Souvik Saha translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Look at this example.

(看这个例子。)

cv2.findContours(...)

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

(只返回两个对象,您正试图将其拆成三个对象。)

change that line to this:

(将该行更改为此:)

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

and it should work.

(它应该工作。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...