Matlab:将图像帧保存为 YCbCr 视频

问题描述

所以我有 10 个单独的图像帧 ycbcr 格式。如何在 Matlab 中将其导出为 ycbcr 视频,以便可以通过支持的视频播放器查看?

更新 1

for Frame_Index = 1: frames
    ycbcr_Movie_Structure_Array(Frame_Index).cdata = uint8(images(Frame_Index));
end

我收到错误: 无法从单元格转换为 uint8。

解决方法

将帧导出到视频文件

不确定是否要将帧保留在 YCbCr 颜色空间中,但如果是这种情况...一种方法是将所有单个帧保存到具有字段/成员 'cdata''colormap'。循环遍历帧并将它们保存到结构后,可以将结构导出到视频文件。要将视频导出到文件,必须首先使用 VideoWriter() 函数创建视频对象。然后可以使用 WriteVideo() 函数将整个结构写入视频对象并传递文件。在对该对象执行任何读取和写入操作之前,最好记住 open()close() 视频对象,类似于处理文本文件的方式。在下面的示例中,视频导出到 .mp4 文件。可以分别通过点属性 .FrameRate.Quality 配置帧速率和质量。

方法 1:使用结构 → 视频对象(文件)

%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,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]);

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);