我编写了以下代码来检测和绘制轮廓:
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
为了强调Selchuk的观点,涉及OpenCV 3.x的语法有所改变。它对cv2.findContours
有不同的回报价值。它返回以下image, contours, hierarchy
。
但是,早期版本的OpenCV仅返回contours, hierarchy
。他们不会返回图像。
更改以下行。您正在使用OpenCV 3.1.0,但您已使用OpenCV 2.7.x编码。
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
这个link也会帮助你。