在我的这篇博客中(点击打开原文链接)讲到如何读取4:2:0格式YUV序列的Y分量、U分量以及V分量,并分别保存为.yuv格式(matlab实现)。
现在我们将三个分量合并成一个完整的彩色yuv序列,代码比较简单,可以看里面的注释,就不废话了。
Y_U_V_Merge.m
%% 将Y、U和V三个分量合成一个完整的YUV序列
clear
clc
%% Read yuv from file
row=144; % 长度
col=176; % 宽度
fnum=300; % 帧数
% Y、U以及V三个分量序列
seqY= 'foreman_qcif_300_Y.yuv';
seqU= 'foreman_qcif_300_U.yuv';
seqV= 'foreman_qcif_300_V.yuv';
rec_Y=zeros(col,row,fnum);
rec_U=zeros(col/2,row/2,fnum);
rec_V=zeros(col/2,row/2,fnum);
len=length(seqY);
yuvName=seqY(1:len-6); %读取视频序列名称
fid_Y = fopen(seqY, 'r');
fid_U = fopen(seqU, 'r');
fid_V = fopen(seqV, 'r');
%% 合成的.yuv序列名称
yuvSeq_Merge=sprintf('%s%s',yuvName,'_yuvMerge.yuv');
outfid_yuv=fopen(yuvSeq_Merge,'wb');
for f=1:fnum
[rec_Y(:,:,f)]=fread(fid_Y,[col,row],'uchar');
buf=uint8(rec_Y(:,:,f));
fwrite(outfid_yuv,buf,'uchar');
[rec_U(:,:,f)]=fread(fid_U,[col/2,row/2],'uchar');
buf=uint8(rec_U(:,:,f));
fwrite(outfid_yuv,buf,'uchar');
[rec_V(:,:,f)]=fread(fid_V,[col/2,row/2],'uchar');
buf=uint8(rec_V(:,:,f));
fwrite(outfid_yuv,buf,'uchar');
end
fclose(fid_Y);
fclose(fid_U);
fclose(fid_V);
fclose(outfid_yuv);