一、直线绘制
函数原型
line(img,pt1,pt2,color,thickness=,lineType=,shift=)
使用示例
start = eval(input("请输入起点坐标(形如(x,y)):"))
end = eval(input("请输入终点坐标(形如(x,y)):"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.line(img,start,end,color,line_width)
二、矩形绘制
函数原型
rectangle(img,pt1,pt2,color,thickness=,lineType=,shift=)
使用示例
start = eval(input("请输入矩形左上角坐标(形如(x,y)):"))
end = eval(input("请输入右下角坐标(形如(x,y)):"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.rectangle(img,start,end,color,line_width)
三、圆形绘制
函数原型
circle(img,center,radius,color,thickness=,lineType=,shift=)
使用示例
center = eval(input("请输入圆心坐标(形如(x,y)):"))
radius = int(input("半径:"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.circle(img,center,radius,color,line_width)
四、椭圆绘制
函数原型
ellipse(img,center,axes,angle,startAngle,endAngle,color,thickness=,lineType=,shift=)
使用示例
center = eval(input("请输入圆心坐标(形如(x,y)):"))
axes = eval(input("请输入轴长度(形如(长轴长度,短轴长度)):"))
angle = int(input("椭圆逆时针旋转角度:"))
# 弧度起点角度将顺时针转到终点角度
startAngle = int(input("椭圆弧起点:"))
endAngle = int(input("椭圆弧终点:"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.ellipse(img,center,axes,angle,startAngle,endAngle,color,line_width)
五、多边形绘制
函数原型
polylines(img,center,pts,isClosed,thickness=,lineType=,shift=)
使用示例
n = int(input("多边形点数:"))
points = []
for _ in range(n):
point = eval(input("多边形点坐标(形如[x,y]):"))
points.append(point)
points = np.array(points,np.int32)
points = points.reshape((-1,1,2))
is_close = eval(input("是否闭合(True/False):"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.polylines(img,[points],is_close,color,line_width)
六、文本绘制
函数原型
putText(img,text,org,fontFace,fontScale,color,thickness=,lineType=,bottomLeftOrigin=)
使用示例
font = cv.FONT_HERSHEY_SIMPLEX
line_type = cv.LINE_AA
text = input("请输入文本内容:")
start = eval(input("请输入文本左下角坐标(形如(x,y)):"))
font_scale = int(input("字体放大比例:"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.putText(img,text,start,font,font_scale,color,line_width,line_type)
七、综合程序
程序功能
- 用户可循环选择绘图操作,绘图时需要定义绘图参数;
- 用户每次可以从上述6个绘图操作中选择一个;
- 每个操作后绘图窗口都会进行相应更新;
- 按下 q 键退出绘图,其他键继续绘图;
用户通过控制台输入绘制参数
绘制一个小机器人演示
画布大小为 512x512,画布颜色为黑色
7.1 完整程序代码
import cv2 as cv
import numpy as np
# 画直线
def draw_line(img):
start = eval(input("请输入起点坐标(形如(x,y)):"))
end = eval(input("请输入终点坐标(形如(x,y)):"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.line(img,start,end,color,line_width)
# 画矩形
def draw_rectangle(img):
start = eval(input("请输入矩形左上角坐标(形如(x,y)):"))
end = eval(input("请输入右下角坐标(形如(x,y)):"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.rectangle(img,start,end,color,line_width)
# 画圆
def draw_circle(img):
center = eval(input("请输入圆心坐标(形如(x,y)):"))
radius = int(input("半径:"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.circle(img,center,radius,color,line_width)
# 画椭圆
def draw_ellipse(img):
center = eval(input("请输入圆心坐标(形如(x,y)):"))
axes = eval(input("请输入轴长度(形如(长轴长度,短轴长度)):"))
angle = int(input("椭圆逆时针旋转角度:"))
startAngle = int(input("椭圆弧起点:"))
endAngle = int(input("椭圆弧终点:"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.ellipse(img,center,axes,angle,startAngle,endAngle,color,line_width)
# 画多边形
def draw_poly(img):
n = int(input("多边形点数:"))
points = []
for _ in range(n):
point = eval(input("多边形点坐标(形如[x,y]):"))
points.append(point)
points = np.array(points,np.int32)
points = points.reshape((-1,1,2))
is_close = eval(input("是否闭合(True/False):"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.polylines(img,[points],is_close,color,line_width)
# 向图像添加文本
def draw_text(img):
font = cv.FONT_HERSHEY_SIMPLEX
line_type = cv.LINE_AA
text = input("请输入文本内容:")
start = eval(input("请输入文本左下角坐标(形如(x,y)):"))
font_scale = int(input("字体放大比例:"))
color = eval(input("颜色BGR表示(形如(b,g,r)):"))
line_width = int(input("线宽:"))
cv.putText(img,text,start,font,font_scale,color,line_width,line_type)
# 绘图程序
def test():
names = ['直线',"矩形","圆","椭圆","多边形","文本"]
values = np.arange(1,len(names)+1)
choices = dict(zip(names,values))
# 全黑画板
img = np.zeros((512,512,3),np.uint8)
while True:
choice = int(input(f"从{choices}中选择绘图操作:"))
if choice == 1:
draw_line(img)
elif choice == 2:
draw_rectangle(img)
elif choice == 3:
draw_circle(img)
elif choice == 4:
draw_ellipse(img)
elif choice == 5:
draw_poly(img)
elif choice == 6:
draw_text(img)
else:
print("选项超出范围")
cv.imshow("image",img)
if cv.waitKey(0) == ord('q'):
break
if __name__ == "__main__":
test()
小机器人的参数配置
# 左触角
1
(50,50)
(100,100)
(255,0,0)
2
# 头
2
(100,100)
(300,300)
(0,255,0)
5
# 右触角
1
(300,100)
(350,50)
(255,0,0)
2
# 左眼睛
3
(150,150)
10
(0,0,255)
2
# 右眼睛
3
(250,150)
10
(0,0,255)
2
# 微笑
4
(200,230)
(60,40)
0
0
180
(0,0,255)
4
# 身体
5
6
[100,300]
[50,350]
[50,450]
[350,450]
[350,350]
[300,300]
True
(155,255,155)
5
# Logo
6
Smile,OpenCV
(150,400)
1
(120,120,255)
2