小编典典

OpenCV python:ValueError:太多值无法解包

python

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

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

编码:

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)

任何帮助表示赞赏!


阅读 215

收藏
2020-12-20

共1个答案

小编典典

我从OpenCV StackExchange网站获得了答案。回答

答案:

我敢打赌,您正在使用当前的OpenCV的master分支:此处的return语句已更改,请参阅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。

2020-12-20