Loading

OpenCV在图像上标注矩形和文本

image

1.OpenCV在图像上标注矩形

用法:

cv2.rectangle(image, start_point, end_point, color, thickness)

参数讲解:

start_point: 图像左上角坐标(x1,y1)
end_point: 图像右下角坐标(x2,y2)
color: 颜色 (B,G,R) 格式
thickness: 矩形边框线的宽度,若为-1则表示填充矩形

示例:
img = cv2.imread(image_path)
cv2.rectangle(img,  (human_box[0], human_box[1]), (human_box[2], human_box[3]), (0, 0, 255), 3 )   # 红色框
cv2.imwrite(output_path, img)
效果:

image

2.OpenCV在图像上标注文本

用法:

cv2.putText(image, text, coordinate , font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

参数讲解:

text: 要标注的文本,str类型
coordinate: 坐标,是一个形如 (int类型, int类型) 的 tuple
font: 字体
fontScale: 字体比例因子,乘以font-specific基本大小即为展示的字体
color: 颜色 (B,G,R) 格式
thickness: 线的粗细
lineType : 可选参数,表示要使用的线条类型
bottomLeftOrigin : 可选参数。如果为true,则图像数据原点位于左下角。否则默认位于左上角

示例:
img = cv2.imread(image_path)
i = 0
for coor in joints_18.tolist():
	cv2.putText(img, str(i), (int(coor[0]), int(coor[1])), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, 4)
	i += 1
cv2.imwrite(output_path, img)
效果:

image

posted @ 2021-11-19 15:33  少年人永远倔强  阅读(808)  评论(0编辑  收藏  举报