当前位置:   article > 正文

Opencv(Python)绘制轮廓函数_python drawcontours如何插入坐标绘制

python drawcontours如何插入坐标绘制

opencv有一个绘制轮廓的函数drawContours,很久没用了,现在把使用方法记录下来

  1. mask = cv2.imread(path,cv2.IMREAD_UNCHANGED)
  2. '''
  3. use threshold, second param is th and the third is number put in when pixel intensity higher than second param
  4. fourth param is constant defined by threshold method
  5. '''
  6. import numpy as np
  7. #ret,th = cv2.threshold(mask,100,255,0)
  8. img,contours,hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  9. img_s = np.zeros(img.shape)
  10. img_s = cv2.drawContours(img_s,contours,-1,(125,25,0),5)
  11. import matplotlib.pyplot as plt
  12. plt.imshow(img_s)
  13. plt.show()

这里由于输入图像本身就是二值化图像所以不需要使用threshold来产生二值化图像。

首先使用findContours来找到边界,然后使用numpy 产生一张新图,如果在原图基础上绘制很容易发现不了绘制的边界。

然后使用drawContours进行绘制,第一个参数是需要将边界绘制到上面的图像,第二个参数是边界坐标,第三个参数是绘制边界的序号(可能有多条边界,-1表示绘制所有),第四个参数表示绘制的边界颜色,第五个参数表示绘制的边界粗细程度。

绘制效果如下所示:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/856733
推荐阅读
相关标签
  

闽ICP备14008679号