我是靠谱客的博主 酷酷草丛,这篇文章主要介绍python中cv2的基本操作——视频、图片的读写,现在分享给大家,希望可以做个参考。

复制代码
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
60
61
62
63
64
65
66
67
68
69
''' 总结cv2相关内容 ''' import cv2 import numpy as np ''' 读图片 ''' image_path = '/data/weichai/0817_choice_frame/8.4_sortheast/1.jpg' image = cv2.imread(image_path) # image是一个矩阵。shape: (hight, weight, channel) ''' 保存图片 ''' output_path = '/home/liulihao/test/demo.jpg' save_image = np.zeros([200,100,3]) cv2.imwrite(output_path, save_image) # save_image是一个矩阵,shape: (hight, weight, channel) ''' 读视频 ''' input_video = '/data/cylinder_demo/raw_data/qiping_merge_20210824.avi' video_reader = cv2.VideoCapture(input_video) fps = video_reader.get(5) frame_id = 0 have_more_frame, frame = video_reader.read() # have_more_frame是一个布尔变量,代表此帧之后视频中是否还有帧 # frame是图片转化成的矩阵。shape: (hight, weight, channel) while have_more_frame: '''此处添加具体操作''' # print(frame_id) frame_id += 1 have_more_frame, frame = video_reader.read() video_reader.release() ''' 保存视频 ''' output_video_path = "/home/liulihao/test/test.avi" output_fps = 30 output_height, output_width,_ = image.shape video_writer = cv2.VideoWriter( output_video_path, # cv2.VideoWriter_fourcc(*'MP4V'), cv2.VideoWriter_fourcc(*'XVID'), # cv2.VideoWriter_fourcc('I', '4', '2', '0'), # cv2.VideoWriter_fourcc('M','J','P','G'), # https://docs.opencv.org/master/dd/d43/tutorial_py_video_display.html output_fps, # fps 每秒的帧数 (output_width, output_height), # resolution ) for i in range(30): video_writer.write(image) video_writer.release()

最后

以上就是酷酷草丛最近收集整理的关于python中cv2的基本操作——视频、图片的读写的全部内容,更多相关python中cv2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部