图
今天我们主要介绍如何使用 OpenCV 来在图片上绘制几何图形, 线, 矩形, 圆以及添加文字.
在图片上绘制直线
line 方法提供在图片绘制线, line 第一个参数为要绘制的图片, 接下来我们需要指定直线的起点和终点, 接下通过 BGR 给直线颜色, 最后参数是线的宽度.
- import cv2
- img = cv2.imread('lena.jpg',1)
- img = cv2.line(img,(0,0),(255,255),(0,0,255),5)
- cv2.imshow('image',img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- line(图像, 起始点, 终止点, 颜色, 宽度)
图
arrowedLine 绘制箭头
img = cv2.arrowedLine(img,(0,0),(255,255),(232,187,111),5)
图
绘制矩形
rectangle(图像, 矩形左上点, 矩形右下角点, 颜色, 线条宽度 (如果是 -1 表示颜色)) img = cv2.rectangle(img,(384,0),(510,128),(0,0,255),5)
图
img = cv2.rectangle(img,(384,0),(510,128),(0,0,255),-1)
绘制圆形
circle(图像, 圆心位置, 直径, 颜色,-1 表示填充) img = cv2.circle(img,(420,65),65,(0,255,0),-1)
图
添加文字
putText(图像, 文字内容, 坐标 , 字体, 大小, 颜色, 字体线条宽度), 这里字体 font 可以使用 cv2.FONT_HERSHEY_SIMPLEX
font = cv2.FONT_HERSHEY_SIMPLEX img = cv2.putText(img,'Welcome to zidea zone',(10,500),font,1,(232,187,111),2,cv2.LINE_AA)
图
来源: http://www.jianshu.com/p/0fb7a1077a68