当前位置:   article > 正文

python中cv2的基本操作——视频、图片的读写_python cv2 mr

python cv2 mr
'''
总结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()
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/823805
推荐阅读
相关标签
  

闽ICP备14008679号