% medemo.m - motion estimation demonstration % (C) 2006 by Yu Hen Hu % created: 12/20/2006 % step by step motion estimation using 4 x 4 macro block over % a 16 x 16 reference frame % cur: current block 4 x 4 % ref: reference frame: 16 x 16 clear all; close all load medemo.mat; % contains ref and cur echo on % assume the 4 x 4 current block cur is located at (2, 2) block position % of the 16 x 16 current frame. % Thus, the origin in the reference frame is the (5, 5) pixel locations % The search range is +- 2 pixels in each direction. % full block motion estimation is to be performed % Note that the matrix indices start at 1 rather than 0 or negative integers, % so we use sad(m+3,n+3) instead of sad(m,n) echo off sadmin=inf; MV=[0 0]; for m=-2:2, for n=-2:2, sad(m+3,n+3)=sum(sum(abs(cur-ref(5+m:8+m,5+n:8+n)))); if sad(m+3,n+3)< sadmin, MV=[m n]; sadmin=sad(m+3,n+3); end end % n loop end % m-loop disp(['Sad value for (m,n) in [-2:2, -2:2]: ']); disp(sad) disp(['Min sad value = ' num2str(sadmin) ', MV = [' ... int2str([m n]) ']']);