Matlab:将图像帧保存为 YCbCr 视频
Matlab: Save image frames as YCbCr video
所以我有 10 个 YCbCr 格式的独立图像帧。如何在 Matlab 中将其导出为 YCbCr 视频,以便可以通过支持的视频播放器观看?
Update-1
for Frame_Index = 1: frames
YCbCr_Movie_Structure_Array(Frame_Index).cdata = uint8(images(Frame_Index));
end
我收到错误:
无法从单元格转换为 uint8。
将帧扩展到视频文件
不确定是否要将帧保留为 YCbCr 颜色-space,但如果是这种情况...一种方法是将所有单独的帧保存到具有 fields/members '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);
方法二:直接导入视频对象(文件)
这种方法要快得多,但在将它们写入文件之前,在操作和 checking/validating 框架方面失去了一些灵活性。
%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);
所以我有 10 个 YCbCr 格式的独立图像帧。如何在 Matlab 中将其导出为 YCbCr 视频,以便可以通过支持的视频播放器观看?
Update-1
for Frame_Index = 1: frames
YCbCr_Movie_Structure_Array(Frame_Index).cdata = uint8(images(Frame_Index));
end
我收到错误: 无法从单元格转换为 uint8。
将帧扩展到视频文件
不确定是否要将帧保留为 YCbCr 颜色-space,但如果是这种情况...一种方法是将所有单独的帧保存到具有 fields/members '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);
方法二:直接导入视频对象(文件)
这种方法要快得多,但在将它们写入文件之前,在操作和 checking/validating 框架方面失去了一些灵活性。
%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);