Step 2 - Motion Vector Estimation
Step 2 - Motion Vector Estimation
Lets consider the example below
Now lets test the above scenario with our code.
function [motion_vec,y_pos,x_pos] = motion_estimate(image1,window, block,block_index)
%input the macroblock from image2 to be searched and the window[x0 y0 x1 y1].As results it provide the motion vector in [y x] format
motion_vec=[];
score=10000000000000000;
y_pos=0;x_pos=0;
for y=window(2):window(4)
for x=window(1):window(3)
mmse = sum(sum(abs(cell2mat(block)-cell2mat(image1(y,x))).^2));
if(mmse<score)
score=mmse;
motion_vec = [(block_index(1)-y) (block_index(2)-x)];
y_pos = y;
x_pos = x;
end
end
end
end
%mse = sum(sum(abs(h.^2-k.^2)))
It works! Now we have to find a way to do this to the entire image by moving the window appropriately.
Step 3 - Motion Vector Array
[IMG1,img_y,img_x] = macroblocks(frame1,macroblock_size);
[IMG2,img_y,img_x] = macroblocks(frame2,macroblock_size);
mv_array = MotionVecArray(IMG1,IMG2,4);
Now lets try the simple example we considered above.
function mv_array = MotionVecArray(image1,image2,w)
%image1 - cell array if image1
%image2 - cell array if image2
%w= window size - 4
mv_array = {};
window = ones(1,4);
for y=1:size(image2,1)
for x=1:size(image2,2)
%-------determining window limits------------------------------
%-------x0 in window-------------------------------------------
if (x-w<1)
window(1) = 1;
else
window(1) = x-w;
end
%-------y0 in window-------------------------------------------
if (y-w<1)
window(2) = 1;
else
window(2) = y-w;
end
%-------x1 in window-------------------------------------------
if (x+w>size(image2,2))
window(3) = size(image2,2);
else
window(3) = x+w;
end
%-------y1 in window-------------------------------------------
if (y+w>size(image2,1))
window(4) = size(image2,1);
else
window(4) = y+w;
end
%motion vector determination for each block
[motion_vec,~,~] = motion_estimate(image1,window,image2(y,x),[y x]);
mv_array(y,x)={motion_vec};
end
end
end
Step 4 - Motion Compensation
motionCompensatedIMG = motionCompensation(IMG1,mv_array);
Lets consider the example we considered through out. We can see that from motion vector array and previous image which is image 1 it has reconstructed the image 2. Notice that the [5 5;5 5] is missing.
Now lets do the same on a real image.
Frame 1 (Image 1)
Frame 2 (Image 2)
Predicted Frame 2 using Frame 1 (Motion compensated image 2)
function motionCompensatedIMG = motionCompensation(image1,mvarray)
motionCompensatedIMG={};
for y=1:size(image1,1)
for x=1:size(image1,2)
temp=cell2mat(mvarray(y,x));
y_pos = y+temp(1);
x_pos = x+temp(2);
motionCompensatedIMG(y,x)=image1(y_pos,x_pos);
end
end
end