赞
踩
opencv有一个绘制轮廓的函数drawContours,很久没用了,现在把使用方法记录下来
- mask = cv2.imread(path,cv2.IMREAD_UNCHANGED)
- '''
- use threshold, second param is th and the third is number put in when pixel intensity higher than second param
- fourth param is constant defined by threshold method
- '''
- import numpy as np
- #ret,th = cv2.threshold(mask,100,255,0)
- img,contours,hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
- img_s = np.zeros(img.shape)
- img_s = cv2.drawContours(img_s,contours,-1,(125,25,0),5)
- import matplotlib.pyplot as plt
- plt.imshow(img_s)
- plt.show()
这里由于输入图像本身就是二值化图像所以不需要使用threshold来产生二值化图像。
首先使用findContours来找到边界,然后使用numpy 产生一张新图,如果在原图基础上绘制很容易发现不了绘制的边界。
然后使用drawContours进行绘制,第一个参数是需要将边界绘制到上面的图像,第二个参数是边界坐标,第三个参数是绘制边界的序号(可能有多条边界,-1表示绘制所有),第四个参数表示绘制的边界颜色,第五个参数表示绘制的边界粗细程度。
绘制效果如下所示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。