-
Notifications
You must be signed in to change notification settings - Fork 8
/
motionEstSESTSS.m
304 lines (232 loc) · 12 KB
/
motionEstSESTSS.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
% Computes motion vectors using Simple and Efficient TSS method
%
% Based on the paper by Jianhua Lu and Ming L. Liou
% IEEE Trans. on Circuits and Systems for Video Technology
% Volume 7, Number 2, April 1997 : Pages 429:433
%
% 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
% SESTSScomputations: The average number of points searched for a macroblock
%
% Written by Aroh Barjatya
function [motionVect, SESTSScomputations] = motionEstSESTSS(imgP, imgI, mbSize, stride, p)
if(size(imgP, 3)>1)
imgP = rgb2gray(imgP);
end
if(size(imgI, 3)>1)
imgI = rgb2gray(imgI);
end
[row col] = size(imgI);
vectors = zeros(2,floor(((row-mbSize+1)/stride))*floor(((col-mbSize+1)/stride)));
% we now take effectively log to the base 2 of p
% this will give us the number of steps required
L = floor(log10(p+1)/log10(2));
stepMax = 2^(L-1);
costs = ones(1,6)*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;
xrange = 1: stride : row-mbSize+1;
if(xrange(end) ~= row-mbSize+1)
xrange = [xrange row-mbSize+1];
end
yrange = 1: stride : col-mbSize+1;
if(yrange(end) ~= col-mbSize+1)
yrange = [yrange col-mbSize+1];
end
for i = xrange
for j = yrange
% the Simple and Efficient three step search starts here
%
% each step is divided into two phases
% in the first phase we evaluate the cost in two ortogonal
% directions at a distance of stepSize away
% based on a certain relationship between the three points costs
% we select the remaining TSS points in the second phase
% At the end of the second phase, which ever point has the least
% cost becomes the root for the next step.
% Please read the paper to find out more detailed information
stepSize = stepMax;
x = j;
y = i;
while (stepSize >= 1)
refBlkVerPointA = y;
refBlkHorPointA = x;
refBlkVerPointB = y;
refBlkHorPointB = x + stepSize;
refBlkVerPointC = y + stepSize;
refBlkHorPointC = x;
if ( refBlkVerPointA < 1 || refBlkVerPointA+mbSize-1 >= row ...
|| refBlkHorPointA < 1 || refBlkHorPointA+mbSize-1 >= col)
% do nothing %
else
costs(1) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointA:refBlkVerPointA+mbSize-1, ...
refBlkHorPointA:refBlkHorPointA+mbSize-1), mbSize);
computations = computations + 1;
end
if ( refBlkVerPointB < 1 || refBlkVerPointB+mbSize-1 >= row ...
|| refBlkHorPointB < 1 || refBlkHorPointB+mbSize-1 >= col)
% do nothing %
else
costs(2) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointB:refBlkVerPointB+mbSize-1, ...
refBlkHorPointB:refBlkHorPointB+mbSize-1), mbSize);
computations = computations + 1;
end
if ( refBlkVerPointC < 1 || refBlkVerPointC+mbSize-1 >= row ...
|| refBlkHorPointC < 1 || refBlkHorPointC+mbSize-1 >= col)
% do nothing %
else
costs(3) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointC:refBlkVerPointC+mbSize-1, ...
refBlkHorPointC:refBlkHorPointC+mbSize-1), mbSize);
computations = computations + 1;
end
if (costs(1) >= costs(2) && costs(1) >= costs(3))
quadrant = 4;
elseif (costs(1) >= costs(2) && costs(1) < costs(3))
quadrant = 1;
elseif (costs(1) < costs(2) && costs(1) < costs(3))
quadrant = 2;
elseif (costs(1) < costs(2) && costs(1) >= costs(3))
quadrant = 3;
end
switch quadrant
case 1
refBlkVerPointD = y - stepSize;
refBlkHorPointD = x;
refBlkVerPointE = y - stepSize;
refBlkHorPointE = x + stepSize;
if ( refBlkVerPointD < 1 || refBlkVerPointD+mbSize-1 >= row ...
|| refBlkHorPointD < 1 || refBlkHorPointD+mbSize-1 >= col)
% do nothing %
else
costs(4) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointD:refBlkVerPointD+mbSize-1, ...
refBlkHorPointD:refBlkHorPointD+mbSize-1), mbSize);
computations = computations + 1;
end
if ( refBlkVerPointE < 1 || refBlkVerPointE+mbSize-1 >= row ...
|| refBlkHorPointE < 1 || refBlkHorPointE+mbSize-1 >= col)
% do nothing %
else
costs(5) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointD:refBlkVerPointD+mbSize-1, ...
refBlkHorPointD:refBlkHorPointD+mbSize-1), mbSize);
computations = computations + 1;
end
case 2
refBlkVerPointD = y - stepSize;
refBlkHorPointD = x;
refBlkVerPointE = y - stepSize;
refBlkHorPointE = x - stepSize;
refBlkVerPointF = y;
refBlkHorPointF = x - stepSize;
if ( refBlkVerPointD < 1 || refBlkVerPointD+mbSize-1 >= row ...
|| refBlkHorPointD < 1 || refBlkHorPointD+mbSize-1 >= col)
% do nothing %
else
costs(4) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointD:refBlkVerPointD+mbSize-1, ...
refBlkHorPointD:refBlkHorPointD+mbSize-1), mbSize);
computations = computations + 1;
end
if ( refBlkVerPointE < 1 || refBlkVerPointE+mbSize-1 >= row ...
|| refBlkHorPointE < 1 || refBlkHorPointE+mbSize-1 >= col)
% do nothing %
else
costs(5) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointE:refBlkVerPointE+mbSize-1, ...
refBlkHorPointE:refBlkHorPointE+mbSize-1), mbSize);
computations = computations + 1;
end
if ( refBlkVerPointF < 1 || refBlkVerPointF+mbSize-1 >= row ...
|| refBlkHorPointF < 1 || refBlkHorPointF+mbSize-1 >= col)
% do nothing %
else
costs(6) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointF:refBlkVerPointF+mbSize-1, ...
refBlkHorPointF:refBlkHorPointF+mbSize-1), mbSize);
computations = computations + 1;
end
case 3
refBlkVerPointD = y;
refBlkHorPointD = x - stepSize;
refBlkVerPointE = y - stepSize;
refBlkHorPointE = x - stepSize;
if ( refBlkVerPointD < 1 || refBlkVerPointD+mbSize-1 >= row ...
|| refBlkHorPointD < 1 || refBlkHorPointD+mbSize-1 >= col)
% do nothing %
else
costs(4) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointD:refBlkVerPointD+mbSize-1, ...
refBlkHorPointD:refBlkHorPointD+mbSize-1), mbSize);
computations = computations + 1;
end
if ( refBlkVerPointE < 1 || refBlkVerPointE+mbSize-1 >= row ...
|| refBlkHorPointE < 1 || refBlkHorPointE+mbSize-1 >= col)
% do nothing %
else
costs(5) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointD:refBlkVerPointD+mbSize-1, ...
refBlkHorPointD:refBlkHorPointD+mbSize-1), mbSize);
computations = computations + 1;
end
case 4
refBlkVerPointD = y + stepSize;
refBlkHorPointD = x + stepSize;
if ( refBlkVerPointD < 1 || refBlkVerPointD+mbSize-1 >= row ...
|| refBlkHorPointD < 1 || refBlkHorPointD+mbSize-1 >= col)
% do nothing %
else
costs(4) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVerPointD:refBlkVerPointD+mbSize-1, ...
refBlkHorPointD:refBlkHorPointD+mbSize-1), mbSize);
computations = computations + 1;
end
otherwise
end
% Now we find the vector where the cost is minimum
% and store it ... this is what will be passed back.
% we use the matlab function min() in this case and not the one
% that is written by author: minCosts()
[cost, dxy] = min(costs); % finds which macroblock in imgI gave us min Cost
switch dxy
case 1
% x = x; y = y;
case 2
x = refBlkHorPointB;
y = refBlkVerPointB;
case 3
x = refBlkHorPointC;
y = refBlkVerPointC;
case 4
x = refBlkHorPointD;
y = refBlkVerPointD;
case 5
x = refBlkHorPointE;
y = refBlkVerPointE;
case 6
x = refBlkHorPointF;
y = refBlkVerPointF;
end
costs = ones(1,6) * 65537 ;
stepSize = stepSize / 2;
end
vectors(1,mbCount) = y - i; % row co-ordinate for the vector
vectors(2,mbCount) = x - j; % col co-ordinate for the vector
mbCount = mbCount + 1;
end
end
motionVect = vectors;
SESTSScomputations = computations/(mbCount - 1);