Skip to content

Commit 54da359

Browse files
committed
motion estimation almost finished
1 parent 9f79eb6 commit 54da359

File tree

7 files changed

+3466
-72
lines changed

7 files changed

+3466
-72
lines changed

Motion-Estimation/Block_Matching.m

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
function [ x_predicted, y_predicted, min_MSE ] = Block_Matching( reference_frame, new_frame, p, x, y, block_size )
2-
%Block_Matching Algorithm. Block Matching between 2 subsequent frames to detect motion estimation
3-
% reference_frame The frame{t-1}
4-
% new_frame The frame{t}
5-
% p Search parameter
6-
% x,y Position withing the frame
7-
% block_size Size of the macroblock
2+
% Block_Matching Algorithm. Block Matching between 2 subsequent frames to detect motion estimation
3+
%
4+
% Input
5+
% reference_frame : The frame{t-1}
6+
% new_frame : The frame{t}
7+
% p : Search parameter
8+
% x,y : Position withing the frame
9+
% block_size : Size of the macroblock
10+
%
11+
% Ouput
12+
% motion_vect : Motion vector for each macroblock
13+
% min_MSE : The minimum MSE (Mean Square Error)
14+
%
815

9-
reference_block = reference_frame(x:x+block_size,y:y+block_size,:);
10-
MSE = zeros(p,p);
16+
reference_block = reference_frame(x:x+(block_size-1),y:y+(block_size-1));
17+
MSE = ones(2*p + 1, 2*p +1) * 65537;
1118

12-
for new_xpos=x-p:1:x+(p-1)
13-
for new_ypos=y-p:1:y+(p-1)
14-
if(new_xpos+block_size>size(reference_frame,1) || new_ypos+block_size>size(reference_frame,2) || new_xpos<=0 || new_ypos<=0)
15-
MSE(new_xpos-(x-p)+1,new_ypos-(y-p)+1)=1000;
19+
% Search the correspondence of the macroblock of a reference frame
20+
% using an exhaustive method. This method searches for the minimum
21+
% cost function at each possible location with respect to the search parameter.
22+
23+
for m=-p:p
24+
for n=-p:p
25+
new_xpos = x + m;
26+
new_ypos = y + n;
27+
if(new_xpos+block_size>size(reference_frame,1) || new_ypos+block_size>size(reference_frame,2) || new_xpos<1 || new_ypos<1)
28+
continue;
1629
else
17-
new_block =new_frame(new_xpos:new_xpos+block_size,new_ypos:new_ypos+block_size,:);
18-
MSE(new_xpos-(x-p)+1,new_ypos-(y-p)+1)=double(mean2(((reference_block-new_block).^2)));
30+
new_block =new_frame(new_xpos:new_xpos+(block_size-1),new_ypos:new_ypos+(block_size-1));
31+
MSE(m+p+1,n+p+1)=double(mean2(((reference_block-new_block).^2)));
1932
end
2033
end
2134
end
2235

23-
min_MSE = min(min(MSE));
24-
25-
[min_x,min_y]=find(min_MSE);
36+
% Create the motion vectors with the minimum cost function.
37+
min = 70000;
38+
for i = 1:size(MSE,1)
39+
for j = 1:size(MSE,2)
40+
if (MSE(i,j) < min)
41+
min = MSE(i,j);
42+
dx = j; dy = i;
43+
end
44+
end
45+
end
2646

27-
if(isempty(min_x)&&isempty(min_y)) % no movement detected
28-
x_predicted = x;
29-
y_predicted = y;
30-
else
31-
x_predicted = min_x+(x-p);
32-
y_predicted = min_y+(y-p);
33-
end
47+
min_MSE = min;
48+
x_predicted = dx;
49+
y_predicted = dy;
3450

3551
end
3652

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function estimated_image = Motion_Compensation( reference_frame, motion_vect, block_size)
2+
%Motion_Compensation algorithm. Predicts the next image using the motion
3+
%vectors
4+
%
5+
% Input
6+
% reference_frame : The frame{t-1}
7+
% motion_vect : The motion vectors for each macroblock
8+
% block_size : Size of the macroblock
9+
%
10+
% Output
11+
% estimated_image : Result of using the motion vectors to predict a frame in a video stream
12+
%
13+
14+
[row, col] = size(reference_frame);
15+
vect_count = 1;
16+
image_compensation = reference_frame;
17+
18+
for i = 1:block_size:row-block_size
19+
for j = 1:block_size:col-block_size
20+
dy = motion_vect(1,vect_count);
21+
dx = motion_vect(2,vect_count);
22+
y_block = i + dy;
23+
x_block = j + dx;
24+
if(y_block>=1&&x_block>=1&&dy~=0&&dx~=0)
25+
image_compensation(i:i+block_size-1,j:j+block_size-1) = reference_frame(y_block:y_block+block_size-1, x_block:x_block+block_size-1);
26+
end
27+
vect_count = vect_count + 1;
28+
end
29+
end
30+
31+
estimated_image = image_compensation;
32+
33+
end
34+

Motion-Estimation/Motion_Estimation.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function motion_vectors = Motion_Estimation(reference_frame, new_frame, block_size, p)
2+
%Motion_Estimation algorithm Computes motion vectors using exhaustive search method
3+
%
4+
% Input
5+
% reference_frame : The frame{t-1}
6+
% new_frame : The frame{t}
7+
% p : Search parameter
8+
% block_size : Size of the macroblock
9+
%
10+
% Ouput
11+
% motion_vectors : The motion vectors for each macroblock
12+
%
13+
14+
[rows, cols] = size(reference_frame);
15+
motion_vectors = zeros(3,rows*cols/block_size^2);
16+
17+
vect_count = 1;
18+
for posx = 1:block_size:rows-block_size
19+
for posy = 1:block_size:cols-block_size
20+
[predicted_posx,predicted_posy,min_MSE]=Block_Matching( reference_frame, new_frame, p, posx, posy, block_size );
21+
motion_vectors(1:2,vect_count) = [predicted_posx,predicted_posy];
22+
motion_vectors(3,vect_count) = min_MSE;
23+
vect_count = vect_count+1;
24+
end
25+
end
26+
27+
end
28+

0 commit comments

Comments
 (0)