《OpenCV 轻松入门 面向Python》 学习笔记
OpenCV绘图及交互
绘画基础
OpenCV提供了绘制直线的函数 cv2.line()、绘制矩形的函数 cv2.rectangle()、绘制圆的函数 cv2.circle()、绘制椭圆的函数 cv2.ellipse()、绘制多边形的函数 cv2.polylines()、在图像内添加文字的函数 cv2.putText()等多种绘图函数。
这些绘图函数有一些共有参数,主要用于设置原图像,颜色,线条属性等。
- img:在其上面绘制图像的载体图像(绘图的容器载体,也成为画布,画板)
- color:绘制颜色的形状,通常用BGR 模型表示颜色;对于灰度图只能传入灰度值;需要注意,颜色通道是BGR, 不是RGB
- thickness:线条粗细。默认值是1;如果设置为-1,表示填充图形(绘制的图形是实心的)
- lineType:线条类型。默认 8连接类型
绘制直线数 cv2.line()
img = cv2.line( img, pt1, pt2, color[, thickness[, lineType ]])
- pt1 对应线段的起点
- pt2 对应线段的终点
import cv2
import numpy as np
n = 300
img = np.zeros((n+1, n+1, 3), dtype=np.uint8)
img = cv2.line(img, (0, 0), (n, n), (255, 0, 0), 1)
img = cv2.line(img, (0, 100), (n, 100), (0, 255, 0), 3)
img = cv2.line(img, (100, 0), (100, n), (0, 0, 255), 6)
cv2.imshow('draw_lines', img)
cv2.waitKey()
cv2.destroyAllWindows()
绘制矩形 cv2.rectangle()
img = cv2.rectangle( img, pt1, pt2, color[, thickness[, lineType]] )
- pt1: 矩阵的一个顶点
- pt2:与顶点pt1对角的顶点
import cv2
import numpy as np
n = 300
img = np.ones((n+1, n+1, 3), dtype=np.uint8) * 255
img = cv2.rectangle(img, (50, 50), (n-100, n-50), (0, 0, 255), -1)
cv2.imshow('draw_rectangle', img)
cv2.waitKey()
cv2.destroyAllWindows()
绘制圆 cv2.circle()
img = cv2.circle( img, center, radius, color[, thickness[, lineType]] )
- center: 圆心坐标
- radius:半径
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
(center_x, center_y) = (round(img.shape[1]/2), round(img.shape[0]/2))
for r in range(5, round(d/2), 12):
img = cv2.circle(img, (center_x, center_y), r, (0, 0, 255), 1)
cv2.imshow('draw_circle', img)
cv2.waitKey()
cv2.destroyAllWindows()
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
for r in range(100):
# 随机生成原型坐标
center_x = np.random.randint(0, d)
center_y = np.random.randint(0, d)
# 随机生成半径大小
radius = np.random.randint(0, d/5)
# 随机生成颜色
color = np.random.randint(0, 256, (3,)).tolist()
# 画圆
img = cv2.circle(img, (center_x, center_y), radius, color, -1)
cv2.imshow('draw_circle', img)
cv2.waitKey()
cv2.destroyAllWindows()
绘制椭圆 cv2.ellipse()
img=cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType]])
- center 椭圆圆心坐标
- axes 轴长
- angle偏转角,逆时针
- startAngle 圆弧起始角度
- endAngle 圆弧终结角的角度。如果需要绘制部分曲线可以指定。
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
# 设置椭圆圆心为画布的中心
(center_x, center_y) = (round(d/2), round(d/2))
# 设置椭圆轴长度
size = (100, 200)
for r in range(10):
# 设置随机偏移角度
angle = np.random.randint(0, 361)
# 设置随机颜色
color = np.random.randint(0, 256, (3,)).tolist()
# 设置随机线条粗细
thickness = np.random.randint(1, 9)
# 画圆
img = cv2.ellipse(img, (center_x, center_y), size, angle, 0, 360, color, thickness)
cv2.imshow('draw_ellipse', img)
cv2.waitKey()
cv2.destroyAllWindows()
绘制多边形 cv2.polylines()
img = cv2.polylines( img, pts, isClosed, color[, thickness[, lineType[, shift]]])
- pts:多边形的各个顶点。构成一个数组,shape为”顶点个数 x 1 x 2“,数据类型为 numpy.int32
- isClosed:闭合标记,若该值为True, 最后一个点与第一个点连接,让多边形闭合;否则,仅是构成一个曲线。
参数 isClosed=True 时
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
# 设置顶点
points = np.array([[200, 50], [300, 200], [200, 350], [100, 200]], np.int32)
points = points.reshape((-1, 1, 2))
img = cv2.polylines(img, [points], True, (0, 250, 0), 8)
cv2.imshow('draw_polylines', img)
cv2.waitKey()
cv2.destroyAllWindows()
参数 isClosed=False 时
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
# 设置顶点
points = np.array([[200, 50], [300, 200], [200, 350], [100, 200]], np.int32)
points = points.reshape((-1, 1, 2))
img = cv2.polylines(img, [points], False, (0, 250, 0), 8)
cv2.imshow('draw_polylines', img)
cv2.waitKey()
cv2.destroyAllWindows()
在图形上绘制文字 cv2.putText()
img=cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
- text 绘制的字体
- org 字体位置,以文字的左下角为起点
- fontFace 字体类型,
- fontScale 字体大小
- bottomLeftOrigin 用于控制文字的方向,默认False,为True时,文字是垂直镜像的效果。
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
# 设置字体
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.putText(img, 'OpenCV', (0, 200), font, 3, (0, 255, 0), 15)
img = cv2.putText(img, 'OpenCV', (0, 200), font, 3, (0, 0, 255), 5)
cv2.imshow('draw_putText', img)
cv2.waitKey()
cv2.destroyAllWindows()