OpenCV python: ValueError: too many values to unpack

匿名 (未验证) 提交于 2019-12-03 01:17:01

问题:

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

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

Traceback (most recent call last):     File "skinimagecontour.py", line 13, in      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!

回答1:

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

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.



回答2:

You have to change this line;

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


回答3:

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)

use

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!