首页 > 解决方案 > 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。

标签: matlabimage-processingrgbycbcr

解决方案


将帧导出到视频文件

不确定是否要将帧保留在 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,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);

推荐阅读