OpenCV python: ValueError:要解包的值太多

时间:2021-04-16 20:21:09

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

我正在编写一个opencv程序,我发现了另一个*问题的脚本:计算机视觉:掩蔽人手

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!

任何帮助都是赞赏!

4 个解决方案

#1


105  

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

我从OpenCV堆栈交换网站得到了答案。回答

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.

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

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的当前稳定版本2.4.9。

#2


12  

You have to change this line;

你必须改变这条线;

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

#3


1  

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)

python是正确的。不能从turple中解压缩3个值,并将它们放在两个等高线的turple中,_ = 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,等高线,_ = cv2。findContours(skin_ycrcb cv2。RETR_EXTERNAL cv2.CHAIN_APPROX_SIMPLE)

#4


0  

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,轮廓,层次结构,= cv2. find轮廓(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

to

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

= 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

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

#1


105  

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

我从OpenCV堆栈交换网站得到了答案。回答

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.

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

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的当前稳定版本2.4.9。

#2


12  

You have to change this line;

你必须改变这条线;

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

#3


1  

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)

python是正确的。不能从turple中解压缩3个值,并将它们放在两个等高线的turple中,_ = 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,等高线,_ = cv2。findContours(skin_ycrcb cv2。RETR_EXTERNAL cv2.CHAIN_APPROX_SIMPLE)

#4


0  

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,轮廓,层次结构,= cv2. find轮廓(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

to

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

= 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

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