Python / 轮廓 OpenCV returns "too many values to unpack"
Python / Contour OpenCV returns "too many values to unpack"
我在学习本教程时遇到问题:https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html
确实,当我尝试执行此代码时遇到此错误:
import numpy as np
import cv2
img = cv2.imread("onepoint.png", 0)
canny_img = cv2.Canny(img,150,200)
ret,thresh = cv2.threshold(canny_img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv2.moments(cnt)
print M
这里是错误:
File "contours.py", line 13, in <module>
contours,hierarchy = cv2.findContours(thresh, 1, 2)
ValueError: too many values to unpack
我不明白为什么会出现这个错误。我明白 cv2.findContours (thresh, 1, 2)
returns 对我来说 3 "arrays",但为什么呢?如果有人好心向我解释我是索取者:(
我正在尝试构架此 picture 的文字。
我是 OpenCV 的新手,我想你会明白的,
提前致谢
您需要将其更改为
im, contours,hierarchy = cv2.findContours(thresh, 1, 2)
或者,如果您只关心轮廓:
(_, contours, _) = cv2.findContours(thresh, 1, 2)
功能已在 OpenCV 3
中更改。
我在学习本教程时遇到问题:https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html 确实,当我尝试执行此代码时遇到此错误:
import numpy as np
import cv2
img = cv2.imread("onepoint.png", 0)
canny_img = cv2.Canny(img,150,200)
ret,thresh = cv2.threshold(canny_img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv2.moments(cnt)
print M
这里是错误:
File "contours.py", line 13, in <module>
contours,hierarchy = cv2.findContours(thresh, 1, 2)
ValueError: too many values to unpack
我不明白为什么会出现这个错误。我明白 cv2.findContours (thresh, 1, 2)
returns 对我来说 3 "arrays",但为什么呢?如果有人好心向我解释我是索取者:(
我正在尝试构架此 picture 的文字。
我是 OpenCV 的新手,我想你会明白的,
提前致谢
您需要将其更改为
im, contours,hierarchy = cv2.findContours(thresh, 1, 2)
或者,如果您只关心轮廓:
(_, contours, _) = cv2.findContours(thresh, 1, 2)
功能已在 OpenCV 3
中更改。