cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None)
第一个参数是指明在哪幅图像上绘制轮廓;image为三通道才能显示轮廓
第二个参数是轮廓本身,在Python中是一个list;
第三个参数指定绘制轮廓list中的哪条轮廓,如果是-1,则绘制其中的所有轮廓。后面的参数很简单。其中thickness表明轮廓线的宽度,如果是-1(cv2.FILLED),则为填充模式。
import cv2
im = cv2.imread('./img2.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 244, 255, 0)
# image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print('轮廓1的有{}个点组成'.format(len(contours[0])))
print('len contours', len(contours))
contours2=[cnt for cnt in contours if cv2.contourArea(cnt)>200]#过滤太小的contour
print('过滤太小的contour', len(contours2))
img=cv2.drawContours(im, contours=contours2, contourIdx=-1, color=(0, 0, 255), thickness=3)
cv2.imshow('drawContours', im)
cv2.waitKey(0)