Asked by Chaine San Buenaventura
on 30 Sep 2013

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

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);

Answer by Image Analyst
on 30 Sep 2013

Check out the VideoReader() class.

Sign in to comment.

Answer by 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.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Thien Thu Ngo (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/88710-how-to-read-a-yuv-or-avi-file-into-matlab-for-video-processing#comment_478647

## Walter Roberson (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/88710-how-to-read-a-yuv-or-avi-file-into-matlab-for-video-processing#comment_478657

## Walter Roberson (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/88710-how-to-read-a-yuv-or-avi-file-into-matlab-for-video-processing#comment_478912

Sign in to comment.