将帧导出到视频文件
不确定是否要将帧保留在 YCbCr 颜色空间中,但如果是这种情况...一种方法是将所有单独的帧保存到具有字段/成员 'cdata' 和 @987654323 的结构中@。在遍历帧并将它们保存到结构后,可以将结构导出到视频文件。要将视频导出到文件,必须首先使用VideoWriter() 函数创建视频对象。然后可以使用WriteVideo() 函数将整个结构写入视频对象并传递到文件中。最好记住 open() 和 close() 视频对象,然后再从该对象执行任何读取和写入,类似于处理文本文件的方式。在下面的示例中,视频被导出到.mp4 文件。帧率和质量可以分别通过点属性.FrameRate和.Quality进行配置。
方法一:使用结构→视频对象(文件)
%Creating 10 test images/frames%
Frame_1 = randi(255,[400 400 3]);
Frame_2 = randi(255,[400 400 3]);
Frame_3 = randi(255,[400 400 3]);
Frame_4 = randi(255,[400 400 3]);
Frame_5 = randi(255,[400 400 3]);
Frame_6 = randi(255,[400 400 3]);
Frame_7 = randi(255,[400 400 3]);
Frame_8 = randi(255,[400 400 3]);
Frame_9 = randi(255,[400 400 3]);
Frame_10 = randi(255,[400 400 3]);
Number_Of_Frames = 10;
[Video_Height,Video_Width,Number_Of_Channels] = size(Frame_1);
%Creating a matrix with dimensions of the video with three channels%
Colour_Channel_Matrix = zeros(Video_Height,Video_Width,3,'uint8');
%Creating a video structure to hold all the frames%
YCbCr_Movie_Structure_Array = struct('cdata',Colour_Channel_Matrix, 'colormap', []);
%Scanning the frames into the video structure%
for Frame_Index = 1: Number_Of_Frames
YCbCr_Movie_Structure_Array(Frame_Index).cdata = uint8(eval("Frame_" + num2str(Frame_Index)));
end
%Creating a video object to save the video structure to%
Video_Object = VideoWriter('Saved_Video.mp4','MPEG-4');
Video_Object.FrameRate = 30;
Video_Object.Quality = 100;
open(Video_Object);
writeVideo(Video_Object,YCbCr_Movie_Structure_Array);
close(Video_Object);
方法二:直接进入视频对象(文件)
这种方法要快得多,但在将帧写入文件之前会在操作和检查/验证帧方面失去一些灵活性。
%Creating 10 test images%
Frame_1 = randi(255,[400 400 3]);
Frame_2 = randi(255,[400 400 3]);
Frame_3 = randi(255,[400 400 3]);
Frame_4 = randi(255,[400 400 3]);
Frame_5 = randi(255,[400 400 3]);
Frame_6 = randi(255,[400 400 3]);
Frame_7 = randi(255,[400 400 3]);
Frame_8 = randi(255,[400 400 3]);
Frame_9 = randi(255,[400 400 3]);
Frame_10 = randi(255,[400 400 3]);
Images = {Frame_1,Frame_2,Frame_3,Frame_4,Frame_5,Frame_6,Frame_7,Frame_8,Frame_9,Frame_10};
Number_Of_Frames = length(Images);
%Creating a video object to save the video structure to%
Video_Object = VideoWriter('Saved_Video.mp4','MPEG-4');
Video_Object.FrameRate = 30;
Video_Object.Quality = 100;
open(Video_Object);
%Scanning the frames into the video structure%
for Frame_Index = 1: Number_Of_Frames
writeVideo(Video_Object,uint8(cell2mat(Images(Frame_Index))));
end
close(Video_Object);