假设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
filename | Name of the output video file. |
fourcc | 4-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. |
fps | Framerate of the created video stream. |
frameSize | Size of the video frames. |
isColor | If 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
) andfourcc=0
ORfps=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