参考链接: Drawing Functions in OpenCV
实验代码展示代码:
import numpy as np
import cv2 as cv
import time
# Create a black image
img = np.zeros((800,800,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
# 绘制直线
# img = cv.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
img = cv.line(img,(0,0),(799,799),(255,0,0),5) # 两个端点的坐标,BGR分量,直线的宽度
# 绘制矩形
img = cv.rectangle(img,(384,20),(500,128),(0,255,0),3)
img = cv.rectangle(img,(104,320),(200,400),(0,0,255),-1)
# 左上点的坐标和右下角点的坐标,BGR值,边框宽度,-1表示填充
# 绘制圆形
# 中心点坐标以及半径,设定线条宽度,其中-1表示填充封闭图形
img = cv.circle(img,(77,243), 63, (0,255,0), -1)
# 绘制椭圆
# img = cv.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])
# 椭圆中心点坐标,横轴和纵轴的长度,旋转角度,椭圆弧线起始和终止的角度范围,弧线颜色,线条宽度-1表示填充
cv.ellipse(img,(556,256),(100,50),10,0,360,(255,255,0),-1)
# 绘制多边形
# img = cv.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]])
pts1 = np.array([[10,30],[700,130],[770,320],[450,710]], np.int32)
pts1 = pts1.reshape((-1,1,2)) # 维度必须是ROWSx1x2
pts2 = np.array([[500,600],[300,700],[370,320],[150,610]], np.int32)
pts2 = pts2.reshape((-1,1,2)) # 维度必须是ROWSx1x2
cv.polylines(img,[pts1],False,(0,255,255)) # 不连接首尾顶点
cv.polylines(img,[pts2],True,(255,255,255)) # 连接首尾顶点
# 绘制文字
font = cv.FONT_HERSHEY_SIMPLEX
# img = cv.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
# 文字, 显示位置, 字体, 基于字体对象基础尺寸的显示文字大小,字体的粗细
cv.putText(img,'OpenCV',(150,400), font, 4,(255,255,255),4,cv.LINE_AA)
# 显示图片
showTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
cv.imshow('LinMaZi-frame-LinZuQuan ' + showTime, img)
k = cv.waitKey(0) # 接收一个按键,否则程序一闪而过
运行结果截图: