在 python 中使用 cv2.findContours() 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40278444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error using cv2.findContours(), with python
提问by Souvik Saha
I've recently started learning OpenCV on Python.
我最近开始在 Python 上学习 OpenCV。
I'm referring to thistutorial 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.
任何解决此问题的帮助将不胜感激。
回答by will
回答by ham
The tutorial you have linked is for OpenCV version 3
. cv2.findContours
does return 3 objects in that version.
您链接的教程适用于OpenCV version 3
. cv2.findContours
确实返回该版本中的 3 个对象。
So either update opencv or use the solution by @will .
所以要么更新 opencv 要么使用 @will 的解决方案。
回答by A.Sha
In version 4.1.2-dev
it returns only two values.
You have to unpack it with two values and then use cv2.drawContours()
to see them.
This is the link to the documentation: https://docs.opencv.org/master/d4/d73/tutorial_py_contours_begin.html
在版本中4.1.2-dev
它只返回两个值。你必须用两个值解压它,然后用它cv2.drawContours()
来查看它们。这是文档的链接:https: //docs.opencv.org/master/d4/d73/tutorial_py_contours_begin.html