OpenCV python:ValueError:解压的值太多

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

OpenCV python: ValueError: too many values to unpack

pythonopencvimage-processing

提问by ahmadux

I'm writing an opencv program and I found a script on another stackoverflow question: Computer Vision: Masking a human hand

我正在编写一个 opencv 程序,我在另一个 stackoverflow 问题上找到了一个脚本:计算机视觉:屏蔽人手

When I run the scripted answer, I get the following error:

当我运行脚本化答案时,出现以下错误:

Traceback (most recent call last):
    File "skinimagecontour.py", line 13, in <module>
    contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack

The code:

编码:

import sys
import numpy
import cv2

im = cv2.imread('Photos/test.jpg')
im_ycrcb = cv2.cvtColor(im, cv2.COLOR_BGR2YCR_CB)

skin_ycrcb_mint = numpy.array((0, 133, 77))
skin_ycrcb_maxt = numpy.array((255, 173, 127))
skin_ycrcb = cv2.inRange(im_ycrcb, skin_ycrcb_mint, skin_ycrcb_maxt)
cv2.imwrite('Photos/output2.jpg', skin_ycrcb) # Second image

contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i, c in enumerate(contours):
    area = cv2.contourArea(c)
    if area > 1000:
        cv2.drawContours(im, contours, i, (255, 0, 0), 3)
cv2.imwrite('Photos/output3.jpg', im)

Any help is appreciated!

任何帮助表示赞赏!

采纳答案by ahmadux

I got the answer from the OpenCV Stack Exchange site. Answer

我从 OpenCV Stack Exchange 站点得到了答案。回答

THE ANSWER:

答案:

I bet you are using the current OpenCV's master branch: here the return statements have changed, see http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours.

Thus, change the corresponding line to read:

_, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Or: since the current trunk is still not stable and you probably will run in some more problems, you may want to use OpenCV's current stable version 2.4.9.

我敢打赌您正在使用当前 OpenCV 的主分支:这里的返回语句已更改,请参阅http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours

因此,将相应的行更改为:

_, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

或者:由于目前的主干还不稳定,你可能会遇到更多的问题,你可能想使用OpenCV目前的稳定版本2.4.9。

回答by Volkan Alt?nta?

You have to change this line;

你必须改变这一行;

image, contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

回答by kishea

python is right.
you cannot unpack 3 values from the turple and place them in a turple of two contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

蟒蛇是对的。
你不能从元组中解压 3 个值并将它们放在两个的元组中 contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

use

img, contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

img, contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

回答by Edwardabk

The thing you need to do is just add '_' where you are not using the required var , originally given by:

您需要做的就是在不使用所需 var 的地方添加 '_' ,最初由以下给出:

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

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

to

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

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

Here the original doc is given: https://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html

这里给出了原始文档:https: //docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html

回答by Safwan

This works in all cv2versions:

这适用于所有cv2版本:

contours, hierarchy = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]

contours, hierarchy = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]

Explanation: By using [-2:], we are basically taking the last two values from the tuplereturned by cv2.findContours. Since in some versions, it returns (image, contours, hierarchy)and in other versions it returns (contours, hierarchy), contours, hierarchyare always the last two values.

说明:通过 using [-2:],我们基本上是从tuple返回的 by 中获取最后两个值cv2.findContours。因为在某些版本中,它返回,(image, contours, hierarchy)而在其他版本中它返回(contours, hierarchy)contours, hierarchy总是最后两个值。

回答by Priyansh gupta

I'm using python3.x and opencv 4.1.0 i was getting error in the following code :

我正在使用 python3.x 和 opencv 4.1.0 我在以下代码中收到错误:

cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

ERROR : too many values to Unpack

then i came to know that above code is used in python2.x SO i just replaced above code with below one(IN python3.x) by adding one more '_' in the left most side have a look

然后我开始知道上面的代码是在 python2.x 中使用的,所以我只是用下面的代码(IN python3.x)替换了上面的代码,在最左边再添加一个“_”看看

_,cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)