Python OpenCV 轮廓 - 需要 2 个以上的值才能解包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20851365/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:23:35  来源:igfitidea点击:

OpenCV Contours - need more than 2 values to unpack

pythonpython-2.7opencv

提问by Prashant Shrivastava

I am trying to implement contours using the following code..

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

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)

but i am continously getting the following error.

但我不断收到以下错误。

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

do the function findContours need more arguments? wht could i do to correct it.

函数 findContours 需要更多参数吗?我该怎么做才能纠正它。

采纳答案by Warren Weckesser

In OpenCV 2, findContoursreturns just two values, contoursand hierarchy. The error occurs when python tries to assign those two values to the three names given on left in this statement:

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

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

回答by rohan goli

findContoursreturns just three values image, contours and hierarchy in opencv3

findContoursopencv3 中只返回三个值 image、contours 和hierarchy

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

回答by Yiming Zhou

It now returns three values:

它现在返回三个值:

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

return image, contours, hierarchy

返回图像、轮廓、层次结构

回答by sharat kanthi

  • findContours returns only two values. so use just,
  • findContours 仅返回两个值。所以只用,

So use

所以用

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

回答by Pinaki Saha

This should help:

这应该有帮助:

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

回答by srbh rthr

Python version 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]

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

NumPy version: 1.16.1

NumPy 版本:1.16.1

argparse version: 1.1

argparse 版本:1.1

CV2 version: 4.0.0

CV2 版本:4.0.0

Traceback (most recent call last):

  File "omr.py", line 254, in <module>

    main()

  File "omr.py", line 237, in main

    answers, im = get_answers(args.input)

  File "omr.py", line 188, in get_answers

    contours = get_contours(im)

  File "omr.py", line 26, in get_contours

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

ValueError: need more than 2 values to unpack

This is resolved by removing 'im2 ,' from line 26.. as in OpenCv version 3.0 or higher, the function 'findContours' returns only 2 values.. so the statement should be

这是通过从第 26 行中删除 'im2' 来解决的。在 OpenCv 3.0 或更高版本中,函数 'findContours' 只返回 2 个值.. 所以语句应该是

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

and also upgrade your OpenCv version

并升级您的 OpenCv 版本

回答by Vishal

As of the year 2019, we have three versions of OpenCV (OpenCV2, OpenCV3, and OpenCV4).

截至 2019 年,我们拥有三个版本的 OpenCV(OpenCV2、OpenCV3 和 OpenCV4)。

OpenCV4 and OpenCV2 have similar behavoiur (of returning two values from cv2.findContours). Whereas OpenCV3 returns three values.

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)

回答by nathancy

Depending on the OpenCV version, cv2.findContours()has varying return signatures. In OpenCV 3.4.X, cv2.findContours()returns 3 items. In OpenCV 2.X and 4.1.X, cv2.findContours()returns 2 items

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

You can easily obtain the contours regardless of the version like this:

无论版本如何,您都可以轻松获得轮廓:

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

回答by nikhil rana

As per the Current Opencv Versions cv2.findContoursreturns 2 Values and that is Contours and heirachy. Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition.

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