# How to read a yuv (or avi file) into matlab for video processing

49 views (last 30 days)
Chaine San Buenaventura on 30 Sep 2013
Commented: Walter Roberson on 23 Aug 2017
What code should i insert above the code below which reads a yuv file (but if not possible, an avi file) for the following video processing. The code below is an algorithm that performs motion compensation. What are the output of this code?
% 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
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
dy = k
dx = l
else
dx = dx
dy = dy
end
else
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);
Walter Roberson on 23 Aug 2017

Image Analyst on 30 Sep 2013

Knut on 22 Aug 2017
The weights in the top of your code seems to be the Y-weights in standard YCbCr: https://en.wikipedia.org/wiki/YUV
Thus it is probably written to accept "rgb" as input and generate a luma mixture for processing.
I don't know that there is a single standardized YUV video format (it could have subsampled chroma or not, interleaved or planar color components etc). Thus you need to figure out your YUV video format, possibly use fopen/fread/fclose to read each frame into memory, discard the U and V planes and feed the Y plane directly into line#5 of your script.

### Categories

Find more on Computer Vision With Simulink 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!