[英]How to detect and draw contours using OpenCV in Python?
我编写了以下代码来检测和绘制轮廓:
img = cv2.imread('test2.tif');
if not img is None:
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY);
ret,thresh = cv2.threshold(imgray,127,255,0);
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE);
#draw a three pixel wide outline
cv2.drawContours(img,contours,-1,(0,255,0),3);
这是我收到的错误:
追溯(最近一次通话):文件“ C:/ Users / RKsingh / Desktop / Image processing / intro-to-contours.py”,第10行,轮廓,层次结构= cv2.findContours(thresh,cv2.RETR_TREE,cv2 .CHAIN_APPROX_SIMPLE); ValueError:太多值无法解包
怎么了? 我正在使用Python 2.7和OpenCV 3.1.0
更改以下行。 您正在使用OpenCV 3.1.0,但已使用OpenCV 2.7.x进行了编码。
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
此外,此链接将为您提供帮助。
为了强调Selchuk的观点,涉及OpenCV 3.x的语法有些变化。 当涉及cv2.findContours
时,它具有不同的返回值。 它返回以下image, contours, hierarchy
。
但是,OpenCV的早期版本仅返回contours, hierarchy
。 他们不返回图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.