clc;
%fid=fopen('foreman.yuv','r');
fid=fopen('suzie_qcif.yuv','r');
% for mobcal
% row=640*2;
% col=360*2;
% for container/news/foreman.yuv
row=176*2;
col=144*2;
% for city.yuv
% row=352*2;
% col=288*2;
frames=100;
%tr_res='high-foreman';
tr_res='high-container';
for frame=1:frames
Y=zeros(row,col,frames);
U=zeros(row/2,col/2,frames);
V=zeros(row/2,col/2,frames);
UU=zeros(row,col,frames);
VV=zeros(row,col,frames);
[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');
UU(1:2:row-1,1:2:col-1,frame)=U(:,:,frame);
UU(1:2:row-1,2:2:col,frame)=U(:,:,frame);
UU(2:2:row,1:2:col-1,frame)=U(:,:,frame);
UU(2:2:row,2:2:col,frame)=U(:,:,frame);
VV(1:2:row-1,1:2:col-1,frame)=V(:,:,frame);
VV(1:2:row-1,2:2:col,frame)=V(:,:,frame);
VV(2:2:row,1:2:col-1,frame)=V(:,:,frame);
VV(2:2:row,2:2:col,frame)=V(:,:,frame);
R=Y+1.140*(VV-128);
G=Y+0.395*(UU-128)-0.581*(VV-128);
B=Y+2.032*(UU-128);
for i=1:row
for j=1:col
if R(i,j,frame)<0
R(i,j,frame)=0;
end
if R(i,j,frame)>255
R(i,j,frame)=255;
end
if G(i,j,frame)<0
G(i,j,frame)=0;
end
if G(i,j,frame)>255
G(i,j,frame)=255;
end
if B(i,j,frame)<0
B(i,j,frame)=0;
end
if B(i,j,frame)>255
B(i,j,frame)=255;
end
end
end
R=R/255;
G=G/255;
B=B/255;
rgb=cat(3,R(:,:,frame)',G(:,:,frame)',B(:,:,frame)');
name=sprintf('%03d',frame);
name1=[name,'.jpg'];
imwrite(rgb, fullfile(tr_res,name1),'JPG');
% figure,imshow(rgb);
end