ahm*_*dux 68 python opencv image-processing
我正在编写一个opencv程序,我在另一个stackoverflow问题上找到了一个脚本:计算机视觉:屏蔽人手
当我运行脚本的答案时,我收到以下错误:
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
Run Code Online (Sandbox Code Playgroud)
代码:
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)
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!
ahm*_*dux 127
我从OpenCV Stack Exchange站点得到了答案.回答
答案:
我敢打赌你正在使用当前的OpenCV主分支:这里的返回语句已经改变,请参阅http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours.
因此,将相应的行更改为:
Run Code Online (Sandbox Code Playgroud)_, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)或者:由于当前主干仍然不稳定,您可能会遇到更多问题,您可能需要使用OpenCV当前的稳定版本2.4.9.
小智 16
你必须改变这条线;
image, contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)
我正在使用 python3.x 和 opencv 4.1.0 我在以下代码中收到错误:
cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
ERROR : too many values to Unpack
Run Code Online (Sandbox Code Playgroud)
然后我开始知道上面的代码是在 python2.x 中使用的,所以我只是用下面的代码(IN python3.x)替换了上面的代码,在最左边再添加一个“_”看看
_,cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)
小智 5
你需要做的就是在你没有使用所需的 var 的地方添加 '_' ,最初由:
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
到
_ , contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
这里给出了原始文档:https : //docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html
这适用于所有cv2版本:
contours, hierarchy = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]