码迷,mamicode.com
首页 > 其他好文 > 详细

matlab公共函数之保存YUV数据

时间:2017-08-23 20:47:30      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:ase   div   cti   format   .com   idt   vda   ros   uda   

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

 

matlab公共函数之保存YUV数据

标签:ase   div   cti   format   .com   idt   vda   ros   uda   

原文地址:http://www.cnblogs.com/Keven-Lee/p/7419859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!