我编写了以下代码来检测和绘制轮廓:
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);
这是我收到的错误:
Traceback (most recent call last): File "C:/Users/R.K.singh/Desktop/Image processing/intro-to-contours.py", line 10, in contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE); ValueError: too many values to unpack
出了什么问题?我正在使用 Python 2.7 和 OpenCV 3.1.0
最佳答案
为了强调 Selchuk 的观点,涉及 OpenCV 3.x 的语法发生了一些变化。对于cv2.findContours
,它有不同的返回值。它返回以下图像、轮廓、层次结构
。
但是,以前版本的 OpenCV 仅返回轮廓、层次结构
。他们不返回图像。
关于python - 如何在Python中使用OpenCV检测和绘制轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41486770/