-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmotionEstES.m
108 lines (86 loc) · 3.91 KB
/
motionEstES.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
% Computes motion vectors using exhaustive search method
%
% Input
% imgP : The image for which we want to find motion vectors
% imgI : The reference image
% mbSize : Size of the macroblock
% p : Search parameter (read literature to find what this means)
%
% Ouput
% motionVect : the motion vectors for each integral macroblock in imgP
% EScomputations: The average number of points searched for a macroblock
%
% Written by Aroh Barjatya
function [motionVect, EScomputations, time] = motionEstES(imgP, imgI, mbSize, p)
tic
imgI = double(imgI);
imgP = double(imgP);
[row, col] = size(imgI);
vectors = zeros(2, row * col / mbSize^2);
costs = ones(2*p + 1, 2*p +1) * 65537;
computations = 0;
% we start off from the top left of the image
% we will walk in steps of mbSize
% for every marcoblock that we look at we will look for
% a close match p pixels on the left, right, top and bottom of it
mbCount = 1;
for i = 1 : mbSize : row - mbSize + 1
for j = 1 : mbSize : col - mbSize + 1
% display the target macroblock
imgP_rect = drawRectangle(imgP, [i, j], [i+mbSize-1, j+mbSize-1], 1, 255);
subplot(1, 2, 1);
imshow(uint8(imgP_rect), 'InitialMagnification', 'fit');
title('Target Frame')
img = double(imgI);
color = 0;
% the exhaustive search starts here
% we will evaluate cost for (2p + 1) blocks vertically
% and (2p + 1) blocks horizontaly
% m is row(vertical) index
% n is col(horizontal) index
% this means we are scanning in raster order
for m = -p : p
for n = -p : p
refBlkVer = i + m; % row/Vert co-ordinate for ref block
refBlkHor = j + n; % col/Horizontal co-ordinate
if ( refBlkVer < 1 || refBlkVer+mbSize-1 > row ...
|| refBlkHor < 1 || refBlkHor+mbSize-1 > col)
continue;
end
costs(m+p+1,n+p+1) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, refBlkHor:refBlkHor+mbSize-1), mbSize);
computations = computations + 1;
% Display the searching macroblock
% imgI_rect = drawRectangle(imgI, [refBlkVer, refBlkHor], [refBlkVer+mbSize-1, refBlkHor+mbSize-1], 1, 255);
% subplot(1, 2, 2);
% imshow(uint8(imgI_rect), 'InitialMagnification', 'fit');
% title('Reference Frame');
% pause(0.1);
% Display the searching macroblock
img = drawRectangle(img, [refBlkVer, refBlkHor], [refBlkVer+mbSize-1, refBlkHor+mbSize-1], 1, color);
color = color + 25;
subplot(1, 2, 2);
imshow(uint8(img), 'InitialMagnification', 'fit');
title('Reference Frame');
pause(0.5);
end
end
% Now we find the vector where the cost is minimum
% and store it ... this is what will be passed back.
[dx, dy, ~] = minCost(costs); % finds which macroblock in imgI gave us min Cost
vectors(1, mbCount) = dy-p-1; % row co-ordinate for the vector
vectors(2, mbCount) = dx-p-1; % col co-ordinate for the vector
% display the selected macroblock
% subplot(1, 2, 2);
% imgI_rect = drawRectangle(imgI, [i+vectors(1, mbCount), j+vectors(2, mbCount)], [i+vectors(1, mbCount)+mbSize-1, j+vectors(2, mbCount)+mbSize-1], 1, 255);
% imshow(uint8(imgI_rect), 'InitialMagnification', 'fit');
% title('Reference Frame');
% pause(0.5);
mbCount = mbCount + 1;
costs = ones(2*p + 1, 2*p +1) * 65537;
end
end
motionVect = vectors;
EScomputations = computations/(mbCount - 1);
time = toc;
end