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: hudalikm@163.com
% 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
浙公网安备 33010602011771号