-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmake_Abc_fast.m
581 lines (536 loc) · 20.3 KB
/
make_Abc_fast.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
function [A, b, c, params] = make_Abc_fast(signal, spatial_size, ...
region_of_interest, options)
% [A, b, c] = make_Abc_fast(signal, spatial_size, region_of_interest, options)
% or
% [A, b, c, params] = make_Abc_fast(signal, spatial_size, ...
% region_of_interest, options)
%
% Compute A, b, and c parameters in up to four dimensions. The
% parameters relate to the local signal model
% f(x) = x^T A x + b^T x + c
% and are determined by a Gaussian weighted least squares fit. This
% implementation uses a fast hierarchical scheme of separable filters,
% described in chapter 4 of "Polynomial Expansion for Orientation and
% Motion Estimation" by Gunnar Farnebäck.
%
% signal - Signal values. Must be real and nonsparse
% and the number of dimensions, N, must be
% at most four.
%
% spatial_size [optional] - Size of the spatial support of the filters
% along each dimension. Default value is 9.
%
% region_of_interest [optional] - An Nx2 matrix where each row contains
% start and stop indices along the
% corresponding dimensions. Default value
% is all of the signal. If an empty matrix
% is entered, the default is used.
%
% options [optional] - Struct array that may contain various
% parameters that affect the algorithm.
% These are explained below.
%
% A - Computed A matrices. A has N+2
% dimensions, where the first N indices
% indicates the position in the signal and
% the last two contains the matrix for each
% point. In the case that region_of_interest
% is less than N-dimensional, the singleton
% dimensions are removed.
%
% b - Computed b vectors. b has N+1
% dimensions, where the first N indices
% indicates the position in the signal and
% the last one contains the vector for each
% point. In the case that region_of_interest
% is less than N-dimensional, the singleton
% dimensions are removed.
%
% c - Computed c scalars. c has N dimensions.
% In the case that region_of_interest
% is less than N-dimensional, the singleton
% dimensions are removed.
%
% params - Struct array containing the parameters
% that has been used by the algorithm.
%
%
% The following fields may be specified in the options parameter:
%
% options.sigma - Standard deviation of a Gaussian applicability. The
% default value is 0.15(K-1), where K is the spatial_size.
% However, if options.delta is set, that value is used
% instead.
%
% options.delta - The value of the gaussian applicability when it reaches
% the end of the supporting region along an axis. If both
% options.sigma and options.delta are set, the former is
% used.
%
% options.c - Certainty mask. Must be spatially invariant and symmetric
% with respect to all axes and have a size compatible with
% the signal dimension and the spatial_size parameter.
% Default value is all ones.
%
% Author: Gunnar Farnebäck
% Computer Vision Laboratory
% Linköping University, Sweden
N = ndims(signal);
if N == 2 & size(signal, 2) == 1
N = 1;
end
if nargin < 2
spatial_size = 9;
end
if spatial_size < 1
error('What use would such a small kernel be?')
elseif mod(spatial_size, 2) ~= 1
spatial_size = 2*floor((spatial_size-1)/2)+1;
warning(sprintf('Only kernels of odd size are allowed. Changed the size to %d.', spatial_size))
end
if nargin < 3 || isempty(region_of_interest)
if N == 1
region_of_interest = [1 size(signal, 1)];
else
region_of_interest = [ones(N, 1), size(signal)'];
end
end
sigma = 0.15 * (spatial_size - 1);
certainty = ones([repmat(spatial_size, [1 N]) 1]);
if nargin == 4
if isfield(options, 'sigma')
sigma = options.sigma;
elseif isfield(options, 'delta')
sigma = n/sqrt(-2*log(delta));
end
if isfield(options, 'c')
certainty = options.c;
end
end
n = (spatial_size-1)/2;
a = exp(-(-n:n).^2/(2*sigma^2))';
switch N
case 1
% Set up applicability and basis functions.
applicability = a;
x = (-n:n)';
b = [ones(size(x)) x x.*x];
nb = size(b, 2);
% Compute the inverse metric.
Q = zeros(nb, nb);
for i = 1:nb
for j = i:nb
Q(i,j) = sum(b(:,i).*applicability.*certainty.*b(:,j));
Q(j,i) = Q(i,j);
end
end
clear b applicability x y
Qinv = inv(Q);
% Convolutions in the x-direction.
kernelx0 = a;
kernelx1 = (-n:n)'.*a;
kernelx2 = (-n:n)'.^2.*a;
roix = region_of_interest;
roix(1) = max(roix(1), 1);
roix(2) = min(roix(2), length(signal));
conv_results = zeros([diff(region_of_interest')+1 3]);
conv_results(:,1) = conv3(signal, kernelx0, roix);
conv_results(:,2) = conv3(signal, kernelx1, roix);
conv_results(:,3) = conv3(signal, kernelx2, roix);
% Apply the inverse metric.
tmp = Qinv(1,1)*conv_results(:,1) + ...
Qinv(1,3)*conv_results(:,3);
conv_results(:,2) = Qinv(2,2)*conv_results(:,2);
conv_results(:,3) = Qinv(3,3)*conv_results(:,3) + ...
Qinv(3,1)*conv_results(:,1);
conv_results(:,1) = tmp;
clear tmp
% Build A, b, and c.
%
% A, b, and c are obtained from the convolution results according to
%
% A=[3], b=[2], c=[1].
%
A = zeros([diff(region_of_interest')+1 1 1]);
b = zeros([diff(region_of_interest')+1 1]);
A(:,1,1) = conv_results(:,3);
b(:,1) = conv_results(:,2);
c = conv_results(:,1);
A = squeeze(A);
b = squeeze(b);
c = squeeze(c);
case 2
% Set up applicability and basis functions.
applicability = a*a';
[x,y] = ndgrid(-n:n);
b = cat(3, ones(size(x)), x, y, x.*x, y.*y, x.*y);
nb = size(b, 3);
% Compute the inverse metric.
Q = zeros(nb, nb);
for i = 1:nb
for j = i:nb
Q(i,j) = sum(sum(b(:,:,i).*applicability.*certainty.*b(:,:,j)));
Q(j,i) = Q(i,j);
end
end
clear b applicability x y
Qinv = inv(Q);
% Convolutions in the y-direction.
kernely0 = a';
kernely1 = (-n:n).*a';
kernely2 = (-n:n).^2.*a';
roiy = region_of_interest+[-n n;0 0];
roiy(:,1) = max(roiy(:,1), ones(2,1));
roiy(:,2) = min(roiy(:,2), size(signal)');
conv_y0 = conv3(signal, kernely0, roiy);
conv_y1 = conv3(signal, kernely1, roiy);
conv_y2 = conv3(signal, kernely2, roiy);
% Convolutions in the x-direction.
kernelx0 = kernely0(:);
kernelx1 = kernely1(:);
kernelx2 = kernely2(:);
roix = region_of_interest;
roix = roix(1:ndims(conv_y0),:);
roix(2:end,:) = roix(2:end,:)+1-repmat(roix(2:end,1), [1 2]);
conv_results = zeros([diff(region_of_interest')+1 6]);
conv_results(:,:,1) = conv3(conv_y0, kernelx0, roix); % y0x0
conv_results(:,:,2) = conv3(conv_y0, kernelx1, roix); % y0x1
conv_results(:,:,4) = conv3(conv_y0, kernelx2, roix); % y0x2
clear conv_y0
conv_results(:,:,3) = conv3(conv_y1, kernelx0, roix); % y1x0
conv_results(:,:,6) = conv3(conv_y1, kernelx1, roix); % y1x1
clear conv_y1
conv_results(:,:,5) = conv3(conv_y2, kernelx0, roix); % y2x0
clear conv_y2
% Apply the inverse metric.
tmp = Qinv(1,1)*conv_results(:,:,1) + ...
Qinv(1,4)*conv_results(:,:,4) + ...
Qinv(1,5)*conv_results(:,:,5);
conv_results(:,:,2) = Qinv(2,2)*conv_results(:,:,2);
conv_results(:,:,3) = Qinv(3,3)*conv_results(:,:,3);
conv_results(:,:,4) = Qinv(4,4)*conv_results(:,:,4) + ...
Qinv(4,1)*conv_results(:,:,1);
conv_results(:,:,5) = Qinv(5,5)*conv_results(:,:,5) + ...
Qinv(5,1)*conv_results(:,:,1);
conv_results(:,:,6) = Qinv(6,6)*conv_results(:,:,6);
conv_results(:,:,1) = tmp;
clear tmp;
% Build A, b, and c.
%
% A, b, and c are obtained from the convolution results according to
%
% [4 6] [2]
% A=[6 5], b=[3], c=[1].
%
% where the off-diagonal elements in A additionally are halved.
%
A = zeros([diff(region_of_interest')+1 2 2]);
b = zeros([diff(region_of_interest')+1 2]);
A(:,:,1,1) = conv_results(:,:,4);
A(:,:,2,2) = conv_results(:,:,5);
A(:,:,1,2) = conv_results(:,:,6) / 2;
A(:,:,2,1) = A(:,:,1,2);
b(:,:,1) = conv_results(:,:,2);
b(:,:,2) = conv_results(:,:,3);
c = conv_results(:,:,1);
A = squeeze(A);
b = squeeze(b);
c = squeeze(c);
case 3
% Set up applicability and basis functions.
applicability = outerprod(a, a, a);
[x,y,t] = ndgrid(-n:n);
b = cat(4, ones(size(x)), x, y, t, x.*x, y.*y, t.*t, x.*y, x.*t, y.*t);
nb = size(b,4);
% Compute the inverse metric.
Q = zeros(nb, nb);
for i = 1:nb
for j = i:nb
Q(i,j) = sum(sum(sum(b(:,:,:,i).*applicability.*certainty.*b(:,:,:,j))));
Q(j,i) = Q(i,j);
end
end
clear b applicability x y t
Qinv = inv(Q);
% Convolutions in the t-direction
kernelt0 = reshape(a, [1 1 spatial_size]);
kernelt1 = reshape((-n:n)'.*a, [1 1 spatial_size]);
kernelt2 = reshape(((-n:n).^2)'.*a, [1 1 spatial_size]);
roit = region_of_interest+[-n n;-n n;0 0];
roit(:,1) = max(roit(:,1), ones(3,1));
roit(:,2) = min(roit(:,2), size(signal)');
conv_t0 = conv3(signal, kernelt0, roit);
conv_t1 = conv3(signal, kernelt1, roit);
conv_t2 = conv3(signal, kernelt2, roit);
% Convolutions in the y-direction
kernely0 = reshape(kernelt0, [1 spatial_size]);
kernely1 = reshape(kernelt1, [1 spatial_size]);
kernely2 = reshape(kernelt2, [1 spatial_size]);
roiy = region_of_interest+[-n n;0 0;0 0];
roiy(:,1) = max(roiy(:,1), ones(3,1));
roiy(:,2) = min(roiy(:,2), size(signal)');
if diff(roiy(3,:)) == 0
roiy = roiy(1:2,:);
else
roiy(3,:) = roiy(3,:)+1-roiy(3,1);
end
conv_t0y0 = conv3(conv_t0, kernely0, roiy);
conv_t0y1 = conv3(conv_t0, kernely1, roiy);
conv_t0y2 = conv3(conv_t0, kernely2, roiy);
clear conv_t0
conv_t1y0 = conv3(conv_t1, kernely0, roiy);
conv_t1y1 = conv3(conv_t1, kernely1, roiy);
clear conv_t1
conv_t2y0 = conv3(conv_t2, kernely0, roiy);
clear conv_t2
% Convolutions in the x-direction
kernelx0 = reshape(kernelt0, [spatial_size 1]);
kernelx1 = reshape(kernelt1, [spatial_size 1]);
kernelx2 = reshape(kernelt2, [spatial_size 1]);
roix = region_of_interest;
roix = roix(1:ndims(conv_t0y0),:);
roix(2:end,:) = roix(2:end,:)+1-repmat(roix(2:end,1), [1 2]);
conv_results = zeros([diff(region_of_interest')+1 10]);
conv_results(:,:,:,1) = conv3(conv_t0y0, kernelx0, roix); % t0y0x0
conv_results(:,:,:,2) = conv3(conv_t0y0, kernelx1, roix); % t0y0x1
conv_results(:,:,:,5) = conv3(conv_t0y0, kernelx2, roix); % t0y0x2
clear conv_t0y0
conv_results(:,:,:,3) = conv3(conv_t0y1, kernelx0, roix); % t0y1x0
conv_results(:,:,:,8) = conv3(conv_t0y1, kernelx1, roix); % t0y1x1
clear conv_t0y1
conv_results(:,:,:,6) = conv3(conv_t0y2, kernelx0, roix); % t0y2x0
clear conv_t0y2
conv_results(:,:,:,4) = conv3(conv_t1y0, kernelx0, roix); % t1y0x0
conv_results(:,:,:,9) = conv3(conv_t1y0, kernelx1, roix); % t1y0x1
clear conv_t1y0
conv_results(:,:,:,10) = conv3(conv_t1y1, kernelx0, roix); % t1y1x0
clear conv_t1y1
conv_results(:,:,:,7) = conv3(conv_t2y0, kernelx0, roix); % t2y0x0
clear conv_t2y0
% Apply the inverse metric.
tmp = Qinv(1,1)*conv_results(:,:,:,1) + ...
Qinv(1,5)*conv_results(:,:,:,5) + ...
Qinv(1,6)*conv_results(:,:,:,6) + ...
Qinv(1,7)*conv_results(:,:,:,7);
conv_results(:,:,:,2) = Qinv(2,2)*conv_results(:,:,:,2);
conv_results(:,:,:,3) = Qinv(3,3)*conv_results(:,:,:,3);
conv_results(:,:,:,4) = Qinv(4,4)*conv_results(:,:,:,4);
conv_results(:,:,:,5) = Qinv(5,5)*conv_results(:,:,:,5) + ...
Qinv(5,1)*conv_results(:,:,:,1);
conv_results(:,:,:,6) = Qinv(6,6)*conv_results(:,:,:,6) + ...
Qinv(6,1)*conv_results(:,:,:,1);
conv_results(:,:,:,7) = Qinv(7,7)*conv_results(:,:,:,7) + ...
Qinv(7,1)*conv_results(:,:,:,1);
conv_results(:,:,:,8) = Qinv(8,8)*conv_results(:,:,:,8);
conv_results(:,:,:,9) = Qinv(9,9)*conv_results(:,:,:,9);
conv_results(:,:,:,10) = Qinv(10,10)*conv_results(:,:,:,10);
conv_results(:,:,:,1) = tmp;
clear tmp;
% Build A, b, and c.
%
% A, b, and c are obtained from the convolution results according to
%
% [5 8 9] [2]
% A=[8 6 10], b=[3], c=[1]
% [9 10 7] [4]
%
% where the off-diagonal elements in A additionally are halved.
%
A = zeros([diff(region_of_interest')+1 3 3]);
b = zeros([diff(region_of_interest')+1 3]);
A(:,:,:,1,1) = conv_results(:,:,:,5);
A(:,:,:,2,2) = conv_results(:,:,:,6);
A(:,:,:,3,3) = conv_results(:,:,:,7);
A(:,:,:,1,2) = conv_results(:,:,:,8) / 2;
A(:,:,:,1,3) = conv_results(:,:,:,9) / 2;
A(:,:,:,2,3) = conv_results(:,:,:,10) / 2;
A(:,:,:,2,1) = A(:,:,:,1,2);
A(:,:,:,3,1) = A(:,:,:,1,3);
A(:,:,:,3,2) = A(:,:,:,2,3);
b(:,:,:,1) = conv_results(:,:,:,2);
b(:,:,:,2) = conv_results(:,:,:,3);
b(:,:,:,3) = conv_results(:,:,:,4);
c = conv_results(:,:,:,1);
A = squeeze(A);
b = squeeze(b);
c = squeeze(c);
case 4
% Set up applicability and basis functions.
applicability = outerprod(a, a, a, a);
[x,y,z,t] = ndgrid(-n:n);
b = cat(5, ones(size(x)), x, y, z, t, x.*x, y.*y, z.*z, t.*t, ...
x.*y, x.*z, x.*t, y.*z, y.*t, z.*t);
nb = size(b, 5);
% Compute the inverse metric.
Q = zeros(nb, nb);
for i = 1:nb
for j = i:nb
Q(i,j) = sum(sum(sum(sum(b(:,:,:,:,i).*applicability.*certainty.*b(:,:,:,:,j)))));
Q(j,i) = Q(i,j);
end
end
clear b applicability x y z t
Qinv = inv(Q);
% Convolutions in the t-direction
kernelt0 = reshape(a, [1 1 1 spatial_size]);
kernelt1 = reshape((-n:n)'.*a, [1 1 1 spatial_size]);
kernelt2 = reshape(((-n:n).^2)'.*a, [1 1 1 spatial_size]);
roit = region_of_interest+[-n n;-n n;-n n;0 0];
roit(:,1) = max(roit(:,1), ones(4,1));
roit(:,2) = min(roit(:,2), size(signal)');
conv_t0 = conv3(signal, kernelt0, roit);
conv_t1 = conv3(signal, kernelt1, roit);
conv_t2 = conv3(signal, kernelt2, roit);
% Convolutions in the z-direction
kernelz0 = reshape(kernelt0, [1 1 spatial_size]);
kernelz1 = reshape(kernelt1, [1 1 spatial_size]);
kernelz2 = reshape(kernelt2, [1 1 spatial_size]);
roiz = region_of_interest+[-n n;-n n;0 0;0 0];
roiz(:,1) = max(roiz(:,1), ones(4,1));
roiz(:,2) = min(roiz(:,2), size(signal)');
if diff(roiz(4,:)) == 0
roiz = roiz(1:2,:);
else
roiz(4,:) = roiz(4,:)+1-roiz(4,1);
end
conv_t0z0 = conv3(conv_t0, kernelz0, roiz);
conv_t0z1 = conv3(conv_t0, kernelz1, roiz);
conv_t0z2 = conv3(conv_t0, kernelz2, roiz);
clear conv_t0
conv_t1z0 = conv3(conv_t1, kernelz0, roiz);
conv_t1z1 = conv3(conv_t1, kernelz1, roiz);
clear conv_t1
conv_t2z0 = conv3(conv_t2, kernelz0, roiz);
clear conv_t2
% Convolutions in the y-direction
kernely0 = reshape(kernelt0, [1 spatial_size]);
kernely1 = reshape(kernelt1, [1 spatial_size]);
kernely2 = reshape(kernelt2, [1 spatial_size]);
roiy = region_of_interest+[-n n;0 0;0 0;0 0];
roiy(:,1) = max(roiy(:,1), ones(4,1));
roiy(:,2) = min(roiy(:,2), size(signal)');
roiy = roiy(1:ndims(conv_t0z0),:);
roiy(3:end,:) = roiy(3:end,:)+1-repmat(roiy(3:end,1),[1 2]);
conv_t0z0y0 = conv3(conv_t0z0, kernely0, roiy);
conv_t0z0y1 = conv3(conv_t0z0, kernely1, roiy);
conv_t0z0y2 = conv3(conv_t0z0, kernely2, roiy);
clear conv_t0z0
conv_t0z1y0 = conv3(conv_t0z1, kernely0, roiy);
conv_t0z1y1 = conv3(conv_t0z1, kernely1, roiy);
clear conv_t0z1
conv_t0z2y0 = conv3(conv_t0z2, kernely0, roiy);
clear conv_t0z2
conv_t1z0y0 = conv3(conv_t1z0, kernely0, roiy);
conv_t1z0y1 = conv3(conv_t1z0, kernely1, roiy);
clear conv_t1z0
conv_t1z1y0 = conv3(conv_t1z1, kernely0, roiy);
clear conv_t1z1
conv_t2z0y0 = conv3(conv_t2z0, kernely0, roiy);
clear conv_t2z0
% Convolutions in the x-direction
kernelx0 = reshape(kernelt0, [spatial_size 1]);
kernelx1 = reshape(kernelt1, [spatial_size 1]);
kernelx2 = reshape(kernelt2, [spatial_size 1]);
roix = region_of_interest;
roix = roix(1:ndims(conv_t0z0y0),:);
roix(2:end,:) = roix(2:end,:)+1-repmat(roix(2:end,1), [1 2]);
conv_results = zeros([diff(region_of_interest')+1 15]);
conv_results(:,:,:,:,1) = conv3(conv_t0z0y0, kernelx0, roix); % t0z0y0x0
conv_results(:,:,:,:,2) = conv3(conv_t0z0y0, kernelx1, roix); % t0z0y0x1
conv_results(:,:,:,:,6) = conv3(conv_t0z0y0, kernelx2, roix); % t0z0y0x2
clear conv_t0z0y0
conv_results(:,:,:,:,3) = conv3(conv_t0z0y1, kernelx0, roix); % t0z0y1x0
conv_results(:,:,:,:,10) = conv3(conv_t0z0y1, kernelx1, roix); % t0z0y1x1
clear conv_t0z0y1
conv_results(:,:,:,:,7) = conv3(conv_t0z0y2, kernelx0, roix); % t0z0y2x0
clear conv_t0z0y2
conv_results(:,:,:,:,4) = conv3(conv_t0z1y0, kernelx0, roix); % t0z1y0x0
conv_results(:,:,:,:,11) = conv3(conv_t0z1y0, kernelx1, roix); % t0z1y0x1
clear conv_t0z1y0
conv_results(:,:,:,:,13) = conv3(conv_t0z1y1, kernelx0, roix); % t0z1y1x0
clear conv_t0z1y1
conv_results(:,:,:,:,8) = conv3(conv_t0z2y0, kernelx0, roix); % t0z2y0x0
clear conv_t0z2y0
conv_results(:,:,:,:,5) = conv3(conv_t1z0y0, kernelx0, roix); % t1z0y0x0
conv_results(:,:,:,:,12) = conv3(conv_t1z0y0, kernelx1, roix); % t1z0y0x1
clear conv_t1z0y0
conv_results(:,:,:,:,14) = conv3(conv_t1z0y1, kernelx0, roix); % t1z0y1x0
clear conv_t1z0y1
conv_results(:,:,:,:,15) = conv3(conv_t1z1y0, kernelx0, roix); % t1z1y0x0
clear conv_t1z1y0
conv_results(:,:,:,:,9) = conv3(conv_t2z0y0, kernelx0, roix); % t2z0y0x0
clear conv_t2z0y0
% Apply the inverse metric.
tmp = Qinv(1,1)*conv_results(:,:,:,:,1) + ...
Qinv(1,6)*conv_results(:,:,:,:,6) + ...
Qinv(1,7)*conv_results(:,:,:,:,7) + ...
Qinv(1,8)*conv_results(:,:,:,:,8) + ...
Qinv(1,9)*conv_results(:,:,:,:,9);
conv_results(:,:,:,:,2) = Qinv(2,2)*conv_results(:,:,:,:,2);
conv_results(:,:,:,:,3) = Qinv(3,3)*conv_results(:,:,:,:,3);
conv_results(:,:,:,:,4) = Qinv(4,4)*conv_results(:,:,:,:,4);
conv_results(:,:,:,:,5) = Qinv(5,5)*conv_results(:,:,:,:,5);
conv_results(:,:,:,:,6) = Qinv(6,6)*conv_results(:,:,:,:,6) + ...
Qinv(6,1)*conv_results(:,:,:,:,1);
conv_results(:,:,:,:,7) = Qinv(7,7)*conv_results(:,:,:,:,7) + ...
Qinv(7,1)*conv_results(:,:,:,:,1);
conv_results(:,:,:,:,8) = Qinv(8,8)*conv_results(:,:,:,:,8) + ...
Qinv(8,1)*conv_results(:,:,:,:,1);
conv_results(:,:,:,:,9) = Qinv(9,9)*conv_results(:,:,:,:,9) + ...
Qinv(9,1)*conv_results(:,:,:,:,1);
conv_results(:,:,:,:,10) = Qinv(10,10)*conv_results(:,:,:,:,10);
conv_results(:,:,:,:,11) = Qinv(11,11)*conv_results(:,:,:,:,11);
conv_results(:,:,:,:,12) = Qinv(12,12)*conv_results(:,:,:,:,12);
conv_results(:,:,:,:,13) = Qinv(13,13)*conv_results(:,:,:,:,13);
conv_results(:,:,:,:,14) = Qinv(14,14)*conv_results(:,:,:,:,14);
conv_results(:,:,:,:,15) = Qinv(15,15)*conv_results(:,:,:,:,15);
conv_results(:,:,:,:,1) = tmp;
clear tmp;
% Build A, b, and c.
%
% A, b, and c are obtained from the convolution results according to
%
%
% [6 10 11 12] [2]
% [10 7 13 14] [3]
% A=[11 13 8 15], b=[4]. c=[1]
% [12 14 15 9] [5]
% where the off-diagonal elements in A additionally are halved.
%
A = zeros([diff(region_of_interest')+1 4 4]);
b = zeros([diff(region_of_interest')+1 4]);
A(:,:,:,:,1,1) = conv_results(:,:,:,:,6);
A(:,:,:,:,2,2) = conv_results(:,:,:,:,7);
A(:,:,:,:,3,3) = conv_results(:,:,:,:,8);
A(:,:,:,:,4,4) = conv_results(:,:,:,:,9);
A(:,:,:,:,1,2) = conv_results(:,:,:,:,10) / 2;
A(:,:,:,:,1,3) = conv_results(:,:,:,:,11) / 2;
A(:,:,:,:,1,4) = conv_results(:,:,:,:,12) / 2;
A(:,:,:,:,2,3) = conv_results(:,:,:,:,13) / 2;
A(:,:,:,:,2,4) = conv_results(:,:,:,:,14) / 2;
A(:,:,:,:,3,4) = conv_results(:,:,:,:,15) / 2;
A(:,:,:,:,2,1) = A(:,:,:,:,1,2);
A(:,:,:,:,3,1) = A(:,:,:,:,1,3);
A(:,:,:,:,4,1) = A(:,:,:,:,1,4);
A(:,:,:,:,3,2) = A(:,:,:,:,2,3);
A(:,:,:,:,4,2) = A(:,:,:,:,2,4);
A(:,:,:,:,4,3) = A(:,:,:,:,3,4);
b(:,:,:,:,1) = conv_results(:,:,:,:,2);
b(:,:,:,:,2) = conv_results(:,:,:,:,3);
b(:,:,:,:,3) = conv_results(:,:,:,:,4);
b(:,:,:,:,4) = conv_results(:,:,:,:,5);
c = conv_results(:,:,:,:,1);
A = squeeze(A);
b = squeeze(b);
c = squeeze(c);
otherwise
error('More than four dimensions are not supported.')
end
if nargout > 3
params.spatial_size = spatial_size;
params.region_of_interest = region_of_interest;
params.sigma = sigma;
params.delta = delta;
params.c = certainty;
end