-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotionEstDS.m
More file actions
410 lines (341 loc) · 18.6 KB
/
motionEstDS.m
File metadata and controls
410 lines (341 loc) · 18.6 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
% Computes motion vectors using Diamond Search method
%
% Based on the paper by Shan Zhu, and Kai-Kuang Ma
% IEEE Trans. on Image Processing
% Volume 9, Number 2, February 2000 : Pages 287:290
%
% 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
% DScomputations: The average number of points searched for a macroblock
%
% Written by Aroh Barjatya
function motionVect = motionEstDS(imgP, imgI, mbSize, p)
[row col] = size(imgI);
vectors = zeros(2,row*col/mbSize^2);
costs = ones(1, 9) * 65537;
% 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));
% The index points for Large Diamond search pattern
LDSP(1,:) = [ 0 -2];
LDSP(2,:) = [-1 -1];
LDSP(3,:) = [ 1 -1];
LDSP(4,:) = [-2 0];
LDSP(5,:) = [ 0 0];
LDSP(6,:) = [ 2 0];
LDSP(7,:) = [-1 1];
LDSP(8,:) = [ 1 1];
LDSP(9,:) = [ 0 2];
% The index points for Small Diamond search pattern
SDSP(1,:) = [ 0 -1];
SDSP(2,:) = [-1 0];
SDSP(3,:) = [ 0 0];
SDSP(4,:) = [ 1 0];
SDSP(5,:) = [ 0 1];
% 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
% computations = 0;
mbCount = 1;
for i = 1 : mbSize : row-mbSize+1
for j = 1 : mbSize : col-mbSize+1
% the Diamond search starts
% we are scanning in raster order
x = j;
y = i;
costs(5) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(i:i+mbSize-1,j:j+mbSize-1),mbSize);
% computations = computations + 1;
% This is the first search so we evaluate all the 9 points in LDSP
for k = 1:9
refBlkVer = y + LDSP(k,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(k,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
continue;
end
if (k == 5)
continue
end
costs(k) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
[cost, point] = min(costs);
% The SDSPFlag is set to 1 when the minimum
% is at the center of the diamond
if (point == 5)
SDSPFlag = 1;
else
SDSPFlag = 0;
if ( abs(LDSP(point,1)) == abs(LDSP(point,2)) )
cornerFlag = 0;
else
cornerFlag = 1; % the x and y co-ordinates not equal on corners
end
xLast = x;
yLast = y;
x = x + LDSP(point, 1);
y = y + LDSP(point, 2);
costs = ones(1,9) * 65537;
costs(5) = cost;
end
while (SDSPFlag == 0)
if (cornerFlag == 1)
for k = 1:9
refBlkVer = y + LDSP(k,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(k,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
continue;
end
if (k == 5)
continue
end
if ( refBlkHor >= xLast - 1 & refBlkHor <= xLast + 1 ...
& refBlkVer >= yLast - 1 & refBlkVer <= yLast + 1 )
continue;
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
continue;
else
costs(k) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
end
else
switch point
case 2
refBlkVer = y + LDSP(1,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(1,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(1) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
% computations = computations + 1;
end
refBlkVer = y + LDSP(2,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(2,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(2) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
% computations = computations + 1;
end
refBlkVer = y + LDSP(4,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(4,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(4) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
case 3
refBlkVer = y + LDSP(1,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(1,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(1) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
refBlkVer = y + LDSP(3,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(3,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(3) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
refBlkVer = y + LDSP(6,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(6,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(6) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
case 7
refBlkVer = y + LDSP(4,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(4,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(4) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
refBlkVer = y + LDSP(7,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(7,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(7) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
refBlkVer = y + LDSP(9,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(9,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(9) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
case 8
refBlkVer = y + LDSP(6,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(6,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(6) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
refBlkVer = y + LDSP(8,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(8,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(8) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
refBlkVer = y + LDSP(9,2); % row/Vert co-ordinate for ref block
refBlkHor = x + LDSP(9,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
% do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
% do nothing, outside search window
else
costs(9) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
otherwise
end
end
[cost, point] = min(costs);
if (point == 5)
SDSPFlag = 1;
else
SDSPFlag = 0;
if ( abs(LDSP(point,1)) == abs(LDSP(point,2)) )
cornerFlag = 0;
else
cornerFlag = 1;
end
xLast = x;
yLast = y;
x = x + LDSP(point, 1);
y = y + LDSP(point, 2);
costs = ones(1,9) * 65537;
costs(5) = cost;
end
end % while loop ends here
% we now enter the SDSP calculation domain
costs = ones(1,5) * 65537;
costs(3) = cost;
for k = 1:5
refBlkVer = y + SDSP(k,2); % row/Vert co-ordinate for ref block
refBlkHor = x + SDSP(k,1); % col/Horizontal co-ordinate
if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
| refBlkHor < 1 | refBlkHor+mbSize-1 > col)
continue; % do nothing, outside image boundary
elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
| refBlkVer > i+p)
continue; % do nothing, outside search window
end
if (k == 3)
continue
end
costs(k) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
imgI(refBlkVer:refBlkVer+mbSize-1, ...
refBlkHor:refBlkHor+mbSize-1), mbSize);
%computations = computations + 1;
end
[cost, point] = min(costs);
x = x + SDSP(point, 1);
y = y + SDSP(point, 2);
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;
costs = ones(1,9) * 65537;
end
end
motionVect = vectors;
%DScomputations = computations/(mbCount - 1);