OpenCV轮廓 - 需要2个以上的值才能打开包装

时间:2013-12-31 03:41:33

标签: opencv python-2.7

我正在尝试使用以下代码实现轮廓..

im = cv2.imread('C:\Users\Prashant\Desktop\T.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContour(im, contours, -1, (0,255,0), 3)
cv2.imshow('Image1',img)

但我不断收到以下错误。

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "C:/Users/Prashant/.spyder2/.temp.py", line 17, in <module>
    image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ValueError: need more than 2 values to unpack

函数findContours需要更多的论证吗? 我可以做些什么来纠正它。

初学者

9 个答案:

答案 0 :(得分:31)

在OpenCV 2中,findContours只返回两个值,contourshierarchy。当python尝试将这两个值分配给此语句左侧给出的三个名称时,会发生错误:

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

答案 1 :(得分:3)

findContours 只返回 opencv3

中的三个值图像,轮廓和层次结构
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

答案 2 :(得分:3)

It now returns three values:

findContours(image, mode, method[, contours[, hierarchy[, offset]]])

return image, contours, hierarchy

答案 3 :(得分:2)

-findContours只返回两个值。所以请使用,

所以使用

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

答案 4 :(得分:0)

这应该有帮助:

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

答案 5 :(得分:0)

Python版本2.7.14(v2.7.14:84471935ed,2017年9月16日,20:25:58)[MSC v.1500 64位(AMD64)]

NumPy版本:1.16.1

argparse版本:1.1

CV2版本:4.0.0

回溯(最近通话最近一次):

文件

中的文件“ omr.py”,第254行
main()

文件“ omr.py”,第237行,位于主文件中

answers, im = get_answers(args.input)

get_answers中的文件“ omr.py”,第188行

contours = get_contours(im)

get_contours中的文件“ omr.py”,第26行

im2, contours, hierarchy =cv2.findContours(image_gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

ValueError:需要两个以上的值才能解包

这是通过从第26行中删除“ im2”来解决的。如在OpenCv 3.0版或更高版本中,函数“ findContours”仅返回2个值..因此该语句应为

轮廓,层次结构= cv2.findContours(image_gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

并升级您的OpenCv版本

答案 6 :(得分:0)

截至2019年,我们提供了三个版本的OpenCV(OpenCV2,OpenCV3和OpenCV4)。

OpenCV4和OpenCV2具有类似的行为(从cv2.findContours返回两个值)。而OpenCV3返回三个值。

if cv2.getVersionMajor() in [2, 4]:
    # OpenCV 2, OpenCV 4 case
    contour, hier = cv2.findContours(
                    thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
else:
    # OpenCV 3 case
    image, contour, hier = cv2.findContours(
                    thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)

答案 7 :(得分:0)

取决于OpenCV版本,cv2.findContours()具有不同的返回签名。在OpenCV 3.4.X中,cv2.findContours()返回3个项目。在OpenCV 2.X和4.1.X中,cv2.findContours()返回2个项目

不管这样的版本,您都可以轻松获得轮廓:

cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

答案 8 :(得分:0)

根据当前的Opencv版本, cv2.findContours 返回2个值,即Contours和heirachy。轮廓可以简单地解释为连接具有相同颜色或强度的所有连续点(沿边界)的曲线。轮廓是进行形状分析以及物体检测和识别的有用工具。