【问题标题】:Matlab: Save image frames as YCbCr videoMatlab:将图像帧保存为 YCbCr 视频
【发布时间】:2021-05-05 20:34:34
【问题描述】:

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

Update-1

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

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

【问题讨论】:

    标签: matlab image-processing rgb ycbcr


    【解决方案1】:

    将帧导出到视频文件

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

    【讨论】:

    • 非常感谢您的详细解释。我有图像数组中的所有帧。每帧为 352x240x3 uint8。我怎样才能使用这个图像数组呢?我添加了尝试使用它时遇到的错误。谢谢。
    • 试试这个YCbCr_Movie_Structure_Array(Frame_Index).cdata = uint8(cell2mat(images(Frame_Index)));
    • 成功了。不过我想知道,有没有办法在不使用结构的情况下做到这一点?
    • 非常感谢。
    • 不太清楚这方面的细节,但这里有一个问题:YUV File Format
    猜你喜欢
    • 1970-01-01
    • 2016-11-14
    • 2011-04-04
    • 2019-01-09
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多