Loading and reading yuv files into matlab for video processing

5 views (last 30 days)
Can somebody explain how this code works? What are the output variables, such as mov here. This code basically loads the avi file, originally a yuv file, into Matlab for processing. But I don't know the details in this code.
%f1: anchor frame; f2: target frame, fp: predicted image;
%mvx,mvy: store the MV image
%widthxheight: image size; N: block size, R: search range
clear; clc;
%load sample_avi;
%x = sample_avi;
%for k = 1:150 % 150 frames
%c{k} = struct2cell(x(k));
%xx = c{k};
%Ximg{k} = xx{1}; % cell array of 150 frames 144x176x3
%end
%# Get the video data:
vidObj = mmreader('foreman_qcif.avi'); %# Create a video file object
nFrames = vidObj.NumberOfFrames; %# Get the number of frames
vidHeight = vidObj.Height; %# Get the image height
vidWidth = vidObj.Width; %# Get the image width
%# Preallocate the structure array of movie frames:
mov(1:nFrames) = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),...
'colormap',[]); %# Note that colormap is empty!
%# Read each video frame into the structure array:
for k = 1:nFrames
mov(k).cdata = read(vidObj,k) %# Place frame k in the cdata field of mov(k)
end
Can this code be used for my main program below? This code is used for predicting the second frame from the first frame through motion estimation and compensation. Can somebody again help me out on the details of this code?
f1 = mov(1).cdata %first frame
f2 = mov(2).cdata %seconde frame
R = input('search range = ');
% convert images to grayscale
%f1 = 0.2989*f1(:,:,1) + 0.5870*f1(:,:,2) + 0.1140*f1(:,:,3);
%f2 = 0.2989*f2(:,:,1) + 0.5870*f2(:,:,2) + 0.1140*f2(:,:,3);
[height width] = size(f1); %size of first frame
N = 16; % blocksize
mvx=[]; mvy=[]; ii = 1; fp = uint8(zeros(height,width));
for i = 1:N:height
jj = 1;
for j = 1:N:width %for every block in the anchor frame
MAD_min = 256*N*N
MAD = MAD_min
dx = 0
dy = 0
for k = -R:1:R % defines search range
for l = -R:1:R %defines search range, for every search candidate
if ~(i+k<=0 | j+l<=0 | i+k+N-1>height | j+l+N-1>width) % if inside boundary
MAD = sum(sum(abs(f1(i:i+N-1,j:j+N-1)-f2(i+k:i+k+N-1,j+l:j+l+N-1))))
if MAD < MAD_min
MAD_min = MAD
dy = k
dx = l
else
MAD = MAD_min
dx = dx
dy = dy
end
else
MAD = MAD_min
dx = dx
dy = dy
end
% calculate MAD for this candidate
end
end
%put the best matching block in the predicted image
fp(i:i+N-1,j:j+N-1) = f2(i+dy:i+dy+N-1,j+dx:j+dx+N-1);
mvx(ii,jj) = dx;
mvy(ii,jj) = dy; %record the estimated MV
jj = jj + 1
end
ii = ii + 1
end
figure; imshow(f1); title('anchor image');
figure; imshow(f2); title('target image');
figure; imshow(fp); title('predicted image');
figure; imshow(f1-fp); title('prediction image error');
figure; quiver(mvx,mvy); axis ij;
f11 = double(f1); fpp = double(fp);
err = 0;
for c1 = 1:height
for c2 = 1:width
err = err + (f11(c1,c2) - fpp(c1,c2))^2;
end
end
mse = err/(height*width);
psnr = 10*log10((pk^2)/mse);
pk = 255; %double(max(max(f1)));
fprintf('PSNR = %f dB\n',psnr);
What is the output of this code?

Answers (1)

Ching-Huang Hsieh
Ching-Huang Hsieh on 7 Jan 2014
%f1: anchor frame; f2: target frame, fp: predicted image;
f2?

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!