我得到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)
如何解决这个问题?
根据 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
元素。
请参考OpenCV的参考文献。
findCounters返回(轮廓,层次结构),所以你的第二行应该是。
contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)