matlab公共函数之保存YUV数据

Posted hudalikm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab公共函数之保存YUV数据相关的知识,希望对你有一定的参考价值。

matlab保存图像为YUV格式的脚本函数

% function flag = saveYUVData(filename,Y,U,V,format)
% input params.
% filename: saving data path
% Y: Y data, res. width x height
% U: U data, res. width/2 x height/2 for NV12, NV21 and YUV420P, width x height for YUV444
% V: V data, res. width/2 x height/2 for NV12, NV21 and YUV420P, width x height for YUV444 
% format: must be NV12, NV21, YUV420P or YUV444
%
% output
% flag: if successed, flag = 1, otherwise flag = 0
%
%
% Author: KevenLee 
% Contact: [email protected]
% Version: V1.0

function flag = saveYUVData(filename,Y,U,V,format)
flag = 1;

[height,width] = size(Y);

fid=fopen(filename,‘w+‘);
if fid <= 0
    flag = -1;
    disp(‘cannot open file!‘)
    return;
end

imgY = Y‘;
imgU = U‘;
imgV = V‘;
imgYUV = Y‘;
switch format
    case ‘NV21‘
        imgNV21UV = zeros(width,height/2);
        
        imgNV21UV(1:2:end,:) = imgV;
        imgNV21UV(2:2:end,:) = imgU;
        
        imgYUV = zeros(1,width*height*1.5);
        
        imgYUV(1:1:width*height) = imgY(:);
        imgYUV(width*height+1:end) = imgNV21UV(:);
    case ‘NV12‘
        imgNV21UV = zeros(width,height/2);        
        imgNV21UV(1:2:end,:) = imgU;
        imgNV21UV(2:2:end,:) = imgV;
        
        imgYUV = zeros(1,width*height*1.5);
        
        imgYUV(1:1:width*height) = imgY(:);
        imgYUV(width*height+1:end) = imgNV21UV(:);
    case ‘YUV420P‘
        imgUV = zeros(width,height/2);
        imgUV(1:1:round(width/2),:) = imgU;
        imgUV(round(width/2)+1:1:end,:) = imgV;
        
        imgYUV = zeros(1,width*height*1.5);
        
        imgYUV(1:1:width*height) = imgY(:);
        imgYUV(width*height+1:end) = imgUV(:);
    case ‘YUV444‘
        imgYUV = zeros(1,width*height*3);
        imgYUV(1:1:width*height) = imgY(:);
        imgYUV(width*height+1:1:width*height*2) = imgU(:);
        imgYUV(width*height*2+1:1:width*height*3) = imgV(:);
    otherwise
        disp(‘not support!‘)
end

fwrite(fid,imgYUV,‘uint8‘);

fclose(fid);

end

 

以上是关于matlab公共函数之保存YUV数据的主要内容,如果未能解决你的问题,请参考以下文章

JavaCV开发详解之24:使用javacv保存raw视频像素格式(yuv或者rgb),并使用ffplay播放raw

使用ffmpeg保存YUV420p文件

Matlab中的YUV视频处理

C语言实现RGB packet格式转YUV(NV21)格式

matlab递归求解最长公共子序列(LCS)问题

Unity怎么保存文件?保存文件然后继续做?