|
1 | 1 | 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 | +% |
8 | 15 |
|
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; |
11 | 18 |
|
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; |
16 | 29 | 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))); |
19 | 32 | end
|
20 | 33 | end
|
21 | 34 | end
|
22 | 35 |
|
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 |
26 | 46 |
|
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; |
34 | 50 |
|
35 | 51 | end
|
36 | 52 |
|
0 commit comments