17鼠标响应与操作

•回调函数参数: int event, int x, int y, int flags, void **userdata*

Event表示鼠标事件

(x, y)表示当前鼠标位置

Flags表示鼠标状态

Userdata表示回调用户数据,可以为空

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
b1 = cv.imread("123.jpg")
img = np.copy(b1)
x1 = -1
y1 = -1
x2 = -1
y2 = -1
def mouse_drawing(event, x, y, flags, param):
global x1, y1, x2, y2
if event == cv.EVENT_LBUTTONDOWN:
x1 = x
y1 = y
if event == cv.EVENT_MOUSEMOVE:
if x1 <0 or y1 <0:
return
x2 = x
y2 = y
dx = x2 - x1
dy = y2 - y1
if dx > 0 and dy > 0:
b1[:,:,:] = img[:,:,:]
cv.rectangle(b1, (x1, y1), (x2, y2), (0, 0, 255), 2, 8, 0)
if event == cv.EVENT_LBUTTONUP:
x2 = x
y2 = y
dx = x2 - x1
dy = y2 - y1
if dx > 0 and dy > 0:
b1[:,:,:] = img[:,:,:]
cv.rectangle(b1, (x1, y1), (x2, y2), (0, 0, 255), 2, 8, 0)
x1 = -1
x2 = -1
y1 = -1
y2 = -1


def mouse_demo():
cv.namedWindow("mouse_demo",cv.WINDOW_AUTOSIZE)
cv.setMouseCallback("mouse_demo",mouse_drawing)
while True:
cv.imshow("mouse_demo",b1)
c = cv.waitKey(10)
if c == 27:
break
cv.destroyAllWindows()

18图像像素类型转换与归一化

归一化函数

•cv.normalize( src, dst[, alpha[, beta[, norm_type[, dtype[, mask]]]]] ) -> dst

•src表示输入图像, dst表示输出图像

•alpha, beta 默认是1, 0,是归一化的区间值

•norm_type默认是NORM_L2,

•norm_type常用是NORM_MINMAX

Imread读入默认是uint8, 转换为float32,通过imshow显示之前,必须归一化到[0~1]之间。

把float32的归一化图像转换为uint8类型:np.uint8(image*255)

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
#归一化
def trackbar_callback(pos):
print(pos)

def norm_demo():
image_unit8 = cv.imread("123.jpg")
cv.imshow("image_uint8",image_unit8)
img_f32 = np.float32(image_unit8)
# cv.imshow("img_f32",img_f32)
# cv.normalize(img_f32, img_f32, 1, 0, cv.NORM_MINMAX)
# cv.imshow("norm-img_f32",img_f32)
# cv.waitKey(0)
# cv.destroyAllWindows()

cv.namedWindow("norm_demo",cv.WINDOW_AUTOSIZE)
cv.createTrackbar("mormtype", "norm_demo", 10, 3, trackbar_callback)
while True:
dst = np.float32(image_unit8)
pos = cv.getTrackbarPos("normtype","norm-demo")
if pos == 0:
cv.normalize(dst, dst, 1, 0, cv.NORM_MINMAX)
if pos == 1:
cv.normalize(dst, dst, 1, 0, cv.NORM_L1)
if pos == 2:
cv.normalize(dst, dst, 1, 0, cv.NORM_L2)
if pos == 3:
cv.normalize(dst, dst, 1, 0, cv.NORM_INF)
cv.imshow("norm-demo",img_f32)
c = cv.waitKey(50)
if c == 27:
break
cv.destroyAllWindows()

19图像几何变换

•cv.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]] ) -> dst

•src表示输入图像

•M 表示2x3变换矩阵

•dsize表示目标图像dst的大小

•支持平移变换、放缩变换、旋转变换

获取旋转矩阵

•旋转矩阵获取cv.getRotationMatrix2D

•Center表示旋转中心, angle表示度数,大于零表示逆时针旋转, scale表示放缩尺度大小。

翻转与特殊角度旋转

•cv.flip(src, flipCode[, dst] ) ->dst

•cv.rotate(src, rotateCode[, dst] ) -> dst

•src表示输入图像

•flipCode支持0水平、1垂直,-1对角线翻转,

•rotateCode支持旋转90°,180°,270°

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
#图像几何变换
def affine_demo():
image = cv.imread("123.jpg")
h, w, c = image.shape
cx = int(w / 2)
cy = int(h / 2)
cv.imshow("image",image)

M = np.zeros((2,3), dtype=np.float32)
M[0, 0] = .7
M[1, 1] = .7
M[0, 2] = 0
M[1, 2] = 0
print("M(2x3) = \n", M)
dst = cv.warpAffine(image, M, (int(w*.7), int(h*.7)))
cv.imshow("rescale-demo",dst)
cv.imwrite("result.png",dst)

#获取旋转矩阵,degree > 0 表示逆时针旋转,原点在左上角
M = cv.getRotationMatrix2D((w/2, h/2), 45.0, 1.0)
dst = cv.warpAffine(image, M, (w,h))
cv.imshow("rotate-demo",dst)

dst = cv.flip(image, 0)
cv.imshow("flip-demo",dst)

cv.waitKey(0)
cv.destroyAllWindows()

20视频读写处理

视频标准与格式

•SD(Standard Definition)标清480P

•HD(High Definition)高清720P/1080P

•UHD(Ultra High Definition)超高清4K/2160P

•分辨率表示

•SD-640x480, 704x480, 720x480, 848x480等

•HD-960x720,1280x720,1440x1080,1920x1080

•UHD-4K,2160P

视频读取函数

cv.VideoCapture ( filename, index, apiPreference)

•filename表示视频文件

•Index表示USB摄像头或者web camera的索引

•apiPreference = CAP_ANY意思自动决定第三方视频库如: cv.CAP_FFMPEG, cv.CAP_DSHOW

查询视频属性

•VideoCaput的get方法

•cv.CAP_PROP_FRAME_WIDT

•cv.CAP_PROP_FRAME_HEIGHT

•cv.CAP_PROP_FPS(对视频流来说是0)

•cv.CAP_PROP_FOURCC

•cv.CAP_PROP_FRAME_COUNT

视频文件保存

cv.VideoWriter(

​ filename, 保存文件名称

​ fourcc, 编码方式

​ fps, 帧率

​ frameSize 视频帧大小,与实现大小相符,否则无法保存

​ [, isColor] )

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
#20视频读写处理
def video_demo():
cap = cv.VideoCapture("456.avi")
# query video file metadata
fps = cap.get(cv.CAP_PROP_FPS)
frame_w = cap.get(cv.CAP_PROP_FRAME_WIDTH)
frame_h = cap.get(cv.CAP_PROP_FRAME_HEIGHT)
print(fps, frame_w, frame_h)
# encode mode
# fourcc =cv.VideoWriter_fourcc(*'vp09')
fourcc = cap.get(cv.CAP_PROP_FOURCC)
# create Video writer
writer = cv.VideoWriter('output.mp4', int(fourcc), fps, (int(frame_w), int(frame_h)))
# loop read frame until last frame
while True:
ret, frame = cap.read()
if ret is not True:
break
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
cv.imshow("hsv",hsv)
cv.imshow("frame",frame)
c = cv.waitKey(1)
if c == 27:
break
writer.write(frame)

# release camera resource
cap.release()
writer.release()