我目前正在阅读OpenCV网站上的示例代码,该代码试图在图像中找到轮廓。
我首先读取图像并转换为灰度:
img = cv2.imread('/.../.../four.png')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
然后,我通过应用阈值将图像转换为二进制图像:
thresh = cv2.threshold(imgray, 127, 255, 0, cv2.THRESH_BINARY)
根据教程..然后,我应该能够在阈值图像上调用
findContours()
:contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
尝试执行此代码时,由于某种原因,我遇到类型错误:
contours = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) TypeError: image is not a numerical tuple
不确定为什么?
以下是完整的代码,以提高可读性:
img = cv2.imread('/Users/samtozer/Desktop/four.png')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(imgray, 127, 255, 0, cv2.THRESH_BINARY)
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0,255,0), 3)
想知道以前是否有人遇到过这个问题?如果是这样,xD出了什么问题
提前致谢
最佳答案
如评论中所述,您必须注意两个问题:
cv2.findContours()
的返回类型cv2.findContours()
中有两个返回值:
cv2.threshold()
的返回类型cv2.threshold()
中有两个返回值:float
值,该值用于对像素值进行分类)关于python - 从阈值图像中找到轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137362/