Python ValueError: 没有足够的值来解压 (预期 3, 得到 2)

问题描述 投票:0回答:2

我得到python的。

编码

_, threshold = cv2.threshold(gray_roi, 3, 255, cv2.THRESH_BINARY_INV)
_, contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)

错误(第二行)

Traceback (most recent call last):
  File "/Users/hissain/PycharmProjects/hello/hello.py", line 17, in <module>
    _, contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)

如何解决这个问题?

python pycharm opencover
2个回答
1
投票

根据 cv2.findContours 文件,函数只返回两个值。你试着解压3个。

移除第一个 _ (不需要的值) 从第二行开始匹配文档中的签名。

_, threshold = cv2.threshold(gray_roi, 3, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)

一般来说,当你得到这样的Python错误信息时。

ValueError: 没有足够的值来解包 (预期的) x 得到 y)

搜索您试图解压的地方 y 元素,并尝试通过解压修复 x 元素。


1
投票

请参考OpenCV的参考文献。

https:/docs.opencv.org2.4modulesimgprocdocstructural_analysis_and_shape_descriptors.html?highlight=findcontours#cv2.findContours。

findCounters返回(轮廓,层次结构),所以你的第二行应该是。

contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
© www.soinside.com 2019 - 2024. All rights reserved.