0%

OpenCV(四)-图形的绘制

一、直线

1
2
3
4
5
6
7
8
9
(function) def line(
img: MatLike, # img: 在哪个图像上画线
pt1: Point, # 开始点、结束点:指定线的开始与结束位置
pt2: Point,
color: Scalar,
thickness: int = ...,
lineType: int = ..., # 线类型:-1,4,8,16,16最光滑
shift: int = ... # Shift:坐标缩放比例
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
import cv2
import numpy as np

img = np.zeros((300, 400, 3), np.uint8)

# 画线坐标(x, y),不是设置页面的(y,x)


cv2.line(img, (10, 20), (300, 100), (0, 0, 255), 5, 16)
cv2.line(img, (50, 20), (350, 100), (0, 0, 255), 5, 4)

cv2.imshow('draw', img)
cv2.waitKey(0)

image-20241105165248658


二、矩形

1
2
3
4
5
6
7
8
9
import cv2
import numpy as np

img = np.zeros((300, 400, 3), np.uint8)

cv2.rectangle(img, (10, 10), (200, 100), (0, 0, 255), -1)

cv2.imshow('draw', img)
cv2.waitKey(0)

image-20241105165838523

三、圆

1
2
3
4
5
6
7
8
9
10
import cv2
import numpy as np

img = np.zeros((300, 400, 3), np.uint8)

# 画圆
cv2.circle(img, (150, 100), 100, (0, 0, 255), 5)

cv2.imshow('circle', img)
cv2.waitKey(0)

image-20241105170053931

四、椭圆

1
2
3
4
5
6
7
8
9
10
11
12
(function) def ellipse(
img: MatLike, # 图片
center: Point, # 圆心
axes: Size, # (a, b) 水平半轴长a,竖直半轴长b
angle: float, # 椭圆倾斜的角度,顺时针
startAngle: float, # 椭圆绘制的开始角度,从右侧开始算,xoy坐标系画的时候的x轴方向
endAngle: float, # 椭圆绘制的结束角度
color: Scalar,
thickness: int = ...,
lineType: int = ...,
shift: int = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
import cv2
import numpy as np

img = np.zeros((300, 400, 3), np.uint8)

# 画椭圆
cv2.ellipse(img, (200, 100),(50, 100), 30, 0, 150, (0, 0, 255), 4)

cv2.imshow('circle', img)
cv2.waitKey(0)

image-20241106092130718


三、多边形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(function)
def polylines(
img: MatLike,
pts: Sequence[MatLike], # 点集
isClosed: bool, # 是否闭环
color: Scalar,
thickness: int = ...,
lineType: int = ...,
shift: int = ...
) -> MatLike: ...

def polylines(
img: UMat,
pts: Sequence[UMat],
isClosed: bool,
color: Scalar,
thickness: int = ...,
lineType: int = ...,
shift: int = ...
) -> UMat: ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2
import numpy as np

img = np.zeros((300, 400, 3), np.uint8)

# 画多边形
# 这个点集必须是32位
pts = np.array([(300, 10), (150, 100), (370, 100)], np.int32)
cv2.polylines(img, [pts], True, (0, 0, 255))

# 填充多边形
cv2.fillPoly(img, [pts], (0, 255, 0))
cv2.imshow('polylines', img)
cv2.waitKey(0)

image-20241106095005718


四、文本

1
2
3
4
5
6
7
8
9
10
11
12
(function)
def putText(
img: MatLike,
text: str,
org: Point,
fontFace: int, # 字体
fontScale: float, # 字号
color: Scalar,
thickness: int = ...,
lineType: int = ...,
bottomLeftOrigin: bool = ...
) -> MatLike: ...
1
2
3
4
5
6
7
8
9
import cv2
import numpy as np

img = np.zeros((300, 400, 3), np.uint8)

# 画文本
cv2.putText(img, "Hello world!", (50, 200), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255))
cv2.imshow('putText', img)
cv2.waitKey(0)

image-20241106100503883


五、大作业:实现鼠标基本绘制


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 基本功能:
# 可以通过鼠标进行基本图形的绘制
# 1、可以画线:当用户按下l键,即选择了画线。此时,滑动鼠标,即可画线
# 2、可以画矩形:当用户按下r键,即可选择画矩形。此时,滑动鼠标,即可画矩形
# 3、可以画圆:当用户按下c键,即可选择画圆。此时,滑动鼠标,即可画圆
# ·····

# curshape: 0-drawline,1-drawrectangle,2-drawcircle

import cv2
import numpy as np

curshape = 0
startpos = (0, 0)


# 显示窗口和背景
img = np.zeros((480, 640, 3), np.uint8)

# 鼠标回调函数
def mouse_callback(event, x, y, flags, userdata):
#print(event, x, y, flags, userdata)
global curshape, startpos # 全局变量需要先进行声明,否则在其中就是当成局部变量

if (event & cv2.EVENT_LBUTTONDOWN == cv2.EVENT_LBUTTONDOWN):
startpos = (x, y)
elif (event & cv2.EVENT_LBUTTONUP == cv2.EVENT_LBUTTONUP):
if curshape == 0: # drawline
cv2.line(img, startpos, (x,y), (0,0,255))
elif curshape == 1: # drawrectangle
cv2.rectangle(img, startpos, (x,y), (0,0,255))
elif curshape == 2: # drawcircle
a = (x - startpos[0])
b = (y - startpos[1])
r = int((a**2+b**2)**0.5)
cv2.circle(img, startpos, r, (0,0,255))

# 创建窗口
cv2.namedWindow('drawShape', cv2.WINDOW_NORMAL)
cv2.resizeWindow('drawShape', 640, 360)

# 设置鼠标回调
cv2.setMouseCallback('drawShape', mouse_callback, "123")


while True:
cv2.imshow('drawShape', img)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('l'): #line
curshape = 0
elif key == ord('r'): #rectangle
curshape = 1
elif key == ord('c'): # circle
curshape = 2


cv2.destroyAllWindows()

image-20241106131016377

-------------本文结束感谢您的阅读-------------