[Python 教程] OpenCV 绘图教程:图形与文本标注
OpenCV 绘图教程:图形与文本标注
本文介绍如何在 OpenCV 中绘制各种图形和添加文本,用于图像标注和可视化。
一、绘制基本图形
1.1 创建画布
import cv2 import numpy as np # 创建黑色画布 img = np.zeros((512, 512, 3), dtype=np.uint8) # 或者创建白色画布 img = np.ones((512, 512, 3), dtype=np.uint8) * 255
1.2 绘制直线
# cv2.line(图像,起点,终点,颜色,线宽) cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
1.3 绘制矩形
# cv2.rectangle(图像,左上角,右下角,颜色,线宽) # 空心矩形 cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3) # 实心矩形(线宽=-1) cv2.rectangle(img, (100, 100), (200, 200), (0, 0, 255), -1)
1.4 绘制圆形
# cv2.circle(图像,圆心,半径,颜色,线宽) cv2.circle(img, (447, 63), 63, (0, 0, 255), -1)
1.5 绘制椭圆
# cv2.ellipse(图像,中心,轴,角度,起始角,结束角,颜色,线宽) cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, (255, 0, 0), -1)
1.6 绘制多边形
# 定义顶点 pts = np.array([[10, 5], [20, 10], [10, 15], [5, 10]], np.int32) pts = pts.reshape((-1, 1, 2)) # 绘制多边形 cv2.polylines(img, [pts], True, (0, 255, 255), 3)
二、添加文本
2.1 基本文本
# cv2.putText(图像,文本,位置,字体,大小,颜色,线宽) cv2.putText(img, 'OpenCV', (10, 500), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 2)
2.2 可用字体
fonts = [
cv2.FONT_HERSHEY_SIMPLEX, # 普通字体
cv2.FONT_HERSHEY_PLAIN, # 简单字体
cv2.FONT_HERSHEY_DUPLEX, # 复杂字体
cv2.FONT_HERSHEY_COMPLEX, # 复杂字体
cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, # 手写体
]
for i, font in enumerate(fonts):
cv2.putText(img, f'Font {i}', (10, 50 + i * 40),
font, 1, (0, 255, 0), 2)三、实际应用示例
3.1 目标检测标注
def draw_detection_box(img, x, y, w, h, label, color=(0, 255, 0)): """绘制检测框和标签""" # 绘制矩形框 cv2.rectangle(img, (x, y), (x + w, y + h), color, 2) # 添加标签背景 label_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)[0] cv2.rectangle(img, (x, y - label_size[1] - 10), (x + label_size[0], y), color, -1) # 添加标签文本 cv2.putText(img, label, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2) # 使用示例 draw_detection_box(img, 100, 100, 200, 200, 'Person: 0.95')
3.2 绘制关键点
# 绘制人脸关键点 landmarks = [(100, 100), (150, 120), (200, 100), (150, 150)] for point in landmarks: cv2.circle(img, point, 5, (0, 255, 0), -1) # 连接关键点 for i in range(len(landmarks) - 1): cv2.line(img, landmarks[i], landmarks[i + 1], (0, 255, 0), 2)
3.3 绘制箭头
def draw_arrow(img, start, end, color=(0, 255, 0), thickness=2): """绘制带箭头的线段""" cv2.line(img, start, end, color, thickness) # 计算箭头 angle = np.arctan2(end[1] - start[1], end[0] - start[0]) arrow_length = 20 arrow_angle = np.pi / 6 pt1 = (int(end[0] - arrow_length * np.cos(angle - arrow_angle)), int(end[1] - arrow_length * np.sin(angle - arrow_angle))) pt2 = (int(end[0] - arrow_length * np.cos(angle + arrow_angle)), int(end[1] - arrow_length * np.sin(angle + arrow_angle))) cv2.line(img, end, pt1, color, thickness) cv2.line(img, end, pt2, color, thickness) draw_arrow(img, (50, 256), (450, 256))
四、综合示例
import cv2
import numpy as np
# 创建画布
img = np.zeros((600, 800, 3), dtype=np.uint8)
# 添加标题
cv2.putText(img, 'OpenCV Drawing Demo', (200, 50),
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
# 绘制各种图形
cv2.line(img, (50, 100), (200, 100), (255, 0, 0), 2)
cv2.rectangle(img, (250, 80), (400, 120), (0, 255, 0), 2)
cv2.circle(img, (550, 100), 40, (0, 0, 255), -1)
cv2.ellipse(img, (150, 250), (80, 40), 45, 0, 360, (255, 255, 0), 2)
# 添加说明
cv2.putText(img, 'Line', (100, 130), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
cv2.putText(img, 'Rectangle', (250, 130), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.putText(img, 'Circle', (520, 130), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# 显示
cv2.imshow('Drawing Demo', img)
cv2.waitKey(0)
cv2.destroyAllWindows()五、总结
本文介绍了 OpenCV 中的绘图功能,包括基本图形绘制、文本添加和实际应用示例。这些技能在目标检测可视化、图像标注、数据增强等场景中非常实用。
