opencv制作和存储视频avi(python)

假设k张图片序列存在数组(张量)frames:512×512×3×k,如何制作成视频,并另存为avi?

import cv2

videoWriter = cv2.VideoWriter('MySaveVideo.avi', cv2.VideoWriter_fourcc('I', '4', '2', '0'), 20, (512,512))

for i in range(20):
    temp = frames[:,:,:,i]    
    cv2.imshow('a',temp)
    videoWriter.write(temp)
    time.sleep(0.1)
    cv2.waitKey(10)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters

filenameName of the output video file.
fourcc4-character code of codec used to compress the frames. For example, VideoWriter::fourcc('P','I','M','1') is a MPEG-1 codec, VideoWriter::fourcc('M','J','P','G') is a motion-jpeg codec etc. List of codes can be obtained at Video Codecs by FOURCC page. FFMPEG backend with MP4 container natively uses other values as fourcc code: see ObjectType, so you may receive a warning message from OpenCV about fourcc code conversion.
fpsFramerate of the created video stream.
frameSizeSize of the video frames.
isColorIf it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames (the flag is currently supported on Windows only).

Tips:

  • With some backends fourcc=-1 pops up the codec selection dialog from the system.
  • To save image sequence use a proper filename (eg. img_%02d.jpg) and fourcc=0 OR fps=0. Use uncompressed image format (eg. img_%02d.BMP) to save raw frames.
  • Most codecs are lossy. If you want lossless video file you need to use a lossless codecs (eg. FFMPEG FFV1, Huffman HFYU, Lagarith LAGS, etc...)
  • If FFMPEG is enabled, using codec=0; fps=0; you can create an uncompressed (raw) video file.

FOURCC代码查这里

https://www.fourcc.org/codecs.php

 

以下是一个简单的Python代码示例,用于采集ZED相机的左右摄像头视频并将其保存AVI格式文件。 首先,需要安装ZED SDK和OpenCV库。然后,使用以下代码: ```python import pyzed.sl as sl import cv2 def main(): # 初始化ZED相机 zed = sl.Camera() # 设置相机参数 init_params = sl.InitParameters() init_params.camera_resolution = sl.RESOLUTION.HD1080 init_params.camera_fps = 30 # 打开相机 err = zed.open(init_params) if err != sl.ERROR_CODE.SUCCESS: print("相机打开失败!") exit(-1) # 创建视频编码器 fourcc = cv2.VideoWriter_fourcc(*'XVID') left_video = cv2.VideoWriter('left.avi', fourcc, 30.0, (init_params.camera_resolution.width, init_params.camera_resolution.height), True) right_video = cv2.VideoWriter('right.avi', fourcc, 30.0, (init_params.camera_resolution.width, init_params.camera_resolution.height), True) # 循环读取帧数据并保存视频 while True: # 采集一帧 if zed.grab() == sl.ERROR_CODE.SUCCESS: # 获取左右摄像头图像 left_image = sl.Mat() right_image = sl.Mat() zed.retrieve_image(left_image, sl.VIEW.LEFT) zed.retrieve_image(right_image, sl.VIEW.RIGHT) # 将图像转换为OpenCV格式 left_frame = left_image.get_data() right_frame = right_image.get_data() left_frame = cv2.cvtColor(left_frame, cv2.COLOR_RGBA2RGB) right_frame = cv2.cvtColor(right_frame, cv2.COLOR_RGBA2RGB) # 保存视频帧 left_video.write(left_frame) right_video.write(right_frame) # 显示图像 cv2.imshow("Left", left_frame) cv2.imshow("Right", right_frame) # 按下ESC键退出 if cv2.waitKey(1) == 27: break # 释放资源 zed.close() left_video.release() right_video.release() cv2.destroyAllWindows() if __name__ == '__main__': main() ``` 该代码打开ZED相机,设置相机参数,创建视频编码器,并在循环读取帧数据并保存视频。在每个循环中,它采集左右摄像头的图像,将其转换为OpenCV格式,并将其保存到相应的AVI文件中。此外,它还将图像显示在窗口中,以便您可以实时查看采集的图像。最后,按下ESC键退出程序并释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞行codes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值