1、YUV序列(4:2:0)下采样
matlab代码:
yuvDownSample.m
clear
clc
%% Read yuv from file
yuvseq = 'kendo0.yuv'; % 输入yuv序列名称
fid = fopen(yuvseq, 'r');
row=1024;col=768; % 输入yuv序列尺寸
fnum=300; % 要转换的帧数
Y=zeros(row,col,fnum);
U=zeros(row/2,col/2,fnum);
V=zeros(row/2,col/2,fnum);
YY=zeros(row/4,col/4,fnum); % 4倍下采样(除以4)
UU=zeros(row/8,col/8,fnum);
VV=zeros(row/8,col/8,fnum);
for frame=1:fnum
[Y(:,:,frame),count] = fread(fid,[row,col],'uchar');
[U(:,:,frame),count1]=fread(fid,[row/2,col/2],'uchar');
[V(:,:,frame),count2]=fread(fid,[row/2,col/2],'uchar');
YY(:,:,frame) = Y(1:4:row,1:4:col,frame);
UU(:,:,frame) = U(1:4:row/2,1:4:col/2,frame);
VV(:,:,frame) = V(1:4:row/2,1:4:col/2,frame);
end
filename=sprintf('%s%s','downSample_',yuvseq); % 下采样后输出yuv序列名称
outfid=fopen(filename,'wb');
for frame=1:fnum
fwrite(outfid,YY(:,:,frame), 'uchar');
fwrite(outfid,UU(:,:,frame), 'uchar');
fwrite(outfid,VV(:,:,frame), 'uchar');
end
fclose(fid);
fclose(outfid);
2、YUV序列(4:2:0)转换为灰度YUV序列(4:0:0)
主函数:main_color2gray.m
function main_color2gary()
% 帧数
f=300;
% 参数downSample_kendo0.yuv为输入视频(4:2:0),
% 参数downSample_kendo0_gray.yuv为转换输出的视频(4:0:0)
% 参数f为转换帧数
% 参数'downSample'表示输入视频的尺寸,可以进入color2gary内查看及设置。
color2gary('downSample_kendo0.yuv','kendo0_gray.yuv',f,'downSample');
子函数:color2gray.m
function color2gary(fin,fout,fnum,frmsz)
if isequal(frmsz,'qcif')
fwid=176;
fhei=144;
elseif isequal(frmsz,'cif')
fwid=352;
fhei=288;
elseif isequal(frmsz,'downSample')
fwid=256;
fhei=192;
elseif isequal(frmsz,'1024x768')
fwid=1024;
fhei=768;
elseif isequal(frmsz,'1280x720')
fwid=1280;
fhei=720;
elseif isequal(frmsz,'1920x1080')
fwid=1920;
fhei=1080;
elseif isequal(frmsz,'832x480')
fwid=832;
fhei=480;
elseif isequal(frmsz,'roi')
fwid=128;
fhei=192;
end
rec=zeros(fhei,fwid,fnum);
for f=1:fnum
rec(:,:,f)=open420Y(fin,fwid,fhei,f);
end
saveY(rec,fout);
子函数:open420Y.m
function img=open420Y(fname,width,height,frame_idx)
fid=fopen(fname,'r');
fsize=width*height;
frame_no=length(frame_idx);
img=zeros(height,width,frame_no);
for i=1:frame_no
fseek(fid,(frame_idx(i)-1)*fsize*3/2,'bof');
buf=fread(fid,fsize,'uint8');
img(:,:,i)=double(reshape(buf,width,height).');
end
fclose(fid);
子函数:
saveY
.m
function saveY(img,fname)
fid=fopen(fname,'w');
[row,col,frm]=size(img);
for i=1:frm
buf=uint8(img(:,:,i).');
count=fwrite(fid,buf,'uint8');
end
fclose(fid);
多视点视频序列下载地址: http://www.tanimoto.nuee.nagoya-u.ac.jp/~fukushima/mpegftv/