-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLSGCPD.m
399 lines (333 loc) · 12.4 KB
/
LSGCPD.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
% LSG-CPD: CPD with Local Surface Geometry
% Author: Weixiao Liu, Hongtao Wu
% Johns Hopkins University
% Nov 6th, 2020
function [tform] = LSGCPD(source, target, varargin)
% --------------------------- varargin ------------------------------------
flag = cellfun(@isequal, varargin, repmat({'maxIter'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
parm.maxIter = varargin{idx};
else
parm.maxIter = 50;
end
flag = cellfun(@isequal, varargin, repmat({'tolerance'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
parm.tolerance = varargin{idx};
else
parm.tolerance = 1e-3;
end
flag = cellfun(@isequal, varargin, repmat({'outlierRatio'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
parm.w = varargin{idx};
else
parm.w = 0.1;
end
flag = cellfun(@isequal, varargin, repmat({'truncationThreshold'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
parm.truncate_threshold = varargin{idx};
else
parm.truncate_threshold = 0.19;
end
flag = cellfun(@isequal, varargin, repmat({'optimizationIter'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
parm.opti_maxIter = varargin{idx};
else
parm.opti_maxIter = 2;
end
flag = cellfun(@isequal, varargin, repmat({'optimizationTolerance'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
parm.opti_tolerance = varargin{idx};
else
parm.opti_tolerance = 1e-3;
end
flag = cellfun(@isequal, varargin, repmat({'neighbours'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
parm.neighbours = varargin{idx};
else
parm.neighbours = 10;
end
flag = cellfun(@isequal, varargin, repmat({'maxPlaneRatio'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
parm.alimit = varargin{idx};
else
parm.alimit = 30;
end
% --------------------------- Load Data -----------------------------------
flag = cellfun(@isequal, varargin, repmat({'dataType'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
if strcmp(varargin{idx}, 'array')
parm.output = 1;
X = source;
Y = target;
else
if strcmp(varargin{idx}, 'pointCloud')
parm.output = 0;
X = gpuArray(source.Location); % source
Y = gpuArray(target.Location); % target (GMM)
else
error('Input data type unsupported.')
end
end
else
parm.output = 0;
X = gpuArray(source.Location); % source
Y = gpuArray(target.Location); % target (GMM)
end
% ----------------------volume of bounding cube----------------------------
V = (max(Y(:, 1)) - min(Y(:, 1))) * ...
(max(Y(:, 2)) - min(Y(:, 2))) * ...
(max(Y(:, 3)) - min(Y(:, 3)));
% ------------------------ Confidence Filtering ---------------------------
flag = cellfun(@isequal, varargin, repmat({'confidenceFilter'},...
size(varargin)));
if any(flag)
idx = circshift(flag,1);
if strcmp(varargin{idx}, 'true')
% Depth of points
X_depth = X(:, 3);
Y_depth = Y(:, 3);
% Confidence of points
confidence_X = confidence_filter(X_depth);
confidence_Y = confidence_filter(Y_depth);
% Truncate the points if confidence < threshold
X = X(confidence_X > parm.truncate_threshold, :);
Y = Y(confidence_Y > parm.truncate_threshold, :);
% Truncate the corresponding confidence
confidence_X = confidence_X(confidence_X > parm.truncate_threshold, :);
confidence_Y = confidence_Y(confidence_Y > parm.truncate_threshold, :);
% Size of points
N = size(X, 1);
M = size(Y, 1);
% Compute surface normal and variation
[Normal_cpu, Curvature] = findPointNormals(gather(Y), parm.neighbours);
% pi(m)
f_Y = confidence_Y ./ sum(confidence_Y);
else
% Size of points
N = size(X, 1);
M = size(Y, 1);
% Confidence is simply one if not weighted
confidence_X = ones(N, 1);
% Compute surface normal and variation
[Normal_cpu, Curvature] = findPointNormals(target.Location, ...
parm.neighbours);
% pi(m)
f_Y = single(ones(size(Y, 1), 1)) ./ size(Y, 1);
end
else
% Size of points
N = size(X, 1);
M = size(Y, 1);
% Confidence is simply one if not weighted
confidence_X = ones(N, 1);
% Compute surface normal and variation
[Normal_cpu, Curvature] = findPointNormals(target.Location, ...
parm.neighbours);
% pi(m)
f_Y = single(ones(size(Y, 1), 1)) ./ size(Y, 1);
end
Normal = gpuArray(Normal_cpu);
% ------------------ Compute centroid and xform to centroid ---------------
flag = cellfun(@isequal, varargin, repmat({'xform2center'}, size(varargin)));
parm.mean_xform = 0;
if any(flag)
idx = circshift(flag,1);
if strcmp(varargin{idx}, 'true')
parm.mean_xform = 1;
xmean = mean(X);
ymean = mean(Y);
X = X - xmean;
Y = Y - ymean;
xmean = gather(xmean);
ymean = gather(ymean);
end
end
% --------------- Pre-calculations and transpose of locations ---------------
Y_Normal = arrayfun(@(a1, a2, a3, b1, b2, b3) a1 * b1 + a2 * b2 + a3 * b3, ...
Y(:, 1), Y(:, 2), Y(:, 3), Normal(:, 1), Normal(:, 2), Normal(:, 3));
X = X';
Y = Y';
X_X2 = vecnorm(X).^2;
Y_Y2 = (vecnorm(Y).^2)';
X_Y = X_X2 + Y_Y2;
X_cpu = gather(X);
Y_cpu = gather(Y);
E1 = [0 0 0; 0 0 -1; 0 1 0];
E2 = [0 0 1; 0 0 0; -1 0 0];
E3 = [0 -1 0; 1 0 0; 0 0 0];
E1X_cpu = gather(E1 * X);
E2X_cpu = gather(E2 * X);
E3X_cpu = gather(E3 * X);
% --------------- Initialize sigma2 ---------------
flag = cellfun(@isequal, varargin, repmat({'sigma2'}, size(varargin)));
if any(flag)
idx = circshift(flag,1);
sigma2 = varargin{idx};
else
sigma2 = 0;
for i = 1:3
sigma2 = sigma2 + sum(sum((X(i, :)' - Y(i, :)).^2));
end
sigma2 = sigma2 / (3 * M * N);
end
% Pre-calculations---------------------------------------------------------
parm.lambda = 0.2; % lambda
a = max(2 ./ (1 + exp(parm.lambda .* (3 - 1 ./ Curvature))) - 1, 0) .* parm.alimit;
vol = (a + 1) .^ (1 / 2);
f_Y = f_Y .* vol;
% estimate outlier weight--------------------------------------------------
w0 = V * parm.w * reshape(f_Y, 1, []) * ...
single((2 * pi * sigma2) ^ (- 3 / 2) .* (a + 1) .^ (1 / 2));
w0 = w0 / (1 - parm.w + w0);
wn = reshape(1 - (1 - w0) .* confidence_X, 1, []);
f_X = (1 - wn) ./ wn;
F_matrix = f_X .* f_Y;
invSigma_flatten_const = zeros(M, 9); % M x 9
y_invSigma_const = zeros(M, 3); % M x 3
y_invSigma_y_const = zeros(M, 1);
for m = 1:M
invSigma_const_m = a(m) .* Normal_cpu(m, :)' * Normal_cpu(m, :) + eye(3);
invSigma_flatten_const(m, :) = reshape(invSigma_const_m, 1, []);
y_invSigma_const(m, :) = Y_cpu(:, m)' * invSigma_const_m;
y_invSigma_y_const(m) = Y_cpu(:, m)' * invSigma_const_m * Y_cpu(:, m);
end
invSigma_flatten_const = gpuArray(single(invSigma_flatten_const));
y_invSigma_const = gpuArray(single(y_invSigma_const));
y_invSigma_y_const = gpuArray(single(y_invSigma_y_const));
% -------------------------------EM-process--------------------------------
% initialize EM parameters
iter = 0;
loglikelihood = 0;
R = eye(3);
t = [0; 0; 0];
% start EM iterations
while iter <= parm.maxIter
loglikelihood_prev = loglikelihood;
% -----------------------------E-step----------------------------------
C = single((2 * pi * sigma2) ^ (3 / 2) * (1 / V));
c = -1 / (2 * sigma2);
RX = gpuArray(R * X_cpu);
[P, M_0, M_1, M_2] = E_step(RX, Y, Normal, t, a, X_Y, ...
F_matrix, Y_Normal, N, C, c,...
invSigma_flatten_const, ...
y_invSigma_const, y_invSigma_y_const);
% -----------------------------M-step----------------------------------
[R, t] = NewtonSE3(R, t, M_0, M_1, X_cpu, N, E1X_cpu, E2X_cpu, E3X_cpu, ...
parm.opti_maxIter, parm.opti_tolerance);
iter = iter + 1;
% ----------------Shrinking and Convergence Checking-------------------
[loglikelihood, sigma2] = Shrink_step(R, t, X_cpu, P, M_0, M_1, M_2, N);
% Check convergency
if abs(loglikelihood - loglikelihood_prev) / loglikelihood < ...
parm.tolerance || loglikelihood < 1e-5
break
end
end
if parm.mean_xform == 1
t = t + ymean' - (R * xmean');
end
% return transform
if parm.output == 1
tform = [R, t; 0 0 0 1];
else
tform = rigid3d(R', t');
end
end
%---------------------------Utility-Functions------------------------------
function [P, M_0, M_1, M_2] = E_step(RX, Y, Normal, t, a, X_Y, ...
F_matrix, Y_Normal, N, C, c,...
invSigma_flatten_const, y_invSigma_const, y_invSigma_y_const)
P = F_matrix .* exp(c * (a .* ((Normal * RX + Normal * t - Y_Normal) .^ 2) + ...
(X_Y + t' * t + 2 * (t' * RX - Y' * RX - Y' * t))));
denominator = sum(P) + C;
P = P ./ denominator;
M_0_flatten = P' * invSigma_flatten_const; % N x 9
M_0 = gather(reshape(M_0_flatten', 3, 3, N));
M_1 = gather(P' * y_invSigma_const); % N x 3
M_2 = gather(P' * y_invSigma_y_const); % N
end
%--------------------------------------------------------------------------
function [g_gradient, H] = GradientSE3(R, t, M_0, M_1, X, N, E1X, E2X, E3X)
M_1_flatten = reshape(M_1', 1, 3 * N);
gX_flatten = reshape(R * X + t, 3 * N, 1);
g_E1_X = reshape(R * E1X + t, 1, 3, N); % 1 * 3 * N
g_E2_X = reshape(R * E2X + t, 1, 3, N); % 1 * 3 * N
g_E3_X = reshape(R * E3X + t, 1, 3, N); % 1 * 3 * N
g_E4_X = reshape(R(:, 1), 1, 3);
g_E5_X = reshape(R(:, 2), 1, 3);
g_E6_X = reshape(R(:, 3), 1, 3);
g_E1_X_flatten = reshape(g_E1_X, 3 * N, 1); % 3N * 1
g_E2_X_flatten = reshape(g_E2_X, 3 * N, 1); % 3N * 1
g_E3_X_flatten = reshape(g_E3_X, 3 * N, 1); % 3N * 1
g_E4_X_flatten = reshape(repmat(R(:, 1), 1, N), 3 * N, 1); % 3N * 1
g_E5_X_flatten = reshape(repmat(R(:, 2), 1, N), 3 * N, 1); % 3N * 1
g_E6_X_flatten = reshape(repmat(R(:, 3), 1, N), 3 * N, 1); % 3N * 1
gE1X_M_0 = reshape(pagemtimes(g_E1_X, M_0), 1, 3 * N);
gE2X_M_0 = reshape(pagemtimes(g_E2_X, M_0), 1, 3 * N);
gE3X_M_0 = reshape(pagemtimes(g_E3_X, M_0), 1, 3 * N);
gE4X_M_0 = reshape(pagemtimes(g_E4_X, M_0), 1, 3 * N);
gE5X_M_0 = reshape(pagemtimes(g_E5_X, M_0), 1, 3 * N);
gE6X_M_0 = reshape(pagemtimes(g_E6_X, M_0), 1, 3 * N);
g_gradient = 2 .* ([gE1X_M_0; gE2X_M_0; gE3X_M_0; gE4X_M_0; gE5X_M_0; ...
gE6X_M_0] * gX_flatten - (M_1_flatten * ...
[g_E1_X_flatten, g_E2_X_flatten, g_E3_X_flatten,...
g_E4_X_flatten, g_E5_X_flatten, g_E6_X_flatten])');
H = 2 .* ([gE1X_M_0; gE2X_M_0; gE3X_M_0; gE4X_M_0; gE5X_M_0; gE6X_M_0] * ...
[g_E1_X_flatten, g_E2_X_flatten, g_E3_X_flatten, ...
g_E4_X_flatten, g_E5_X_flatten, g_E6_X_flatten]);
end
%--------------------------------------------------------------------------
function [R, t] = NewtonSE3(R, t, M_0, M_1, X, N, E1X, E2X, E3X, maxIter, tolerance)
iter = 1;
while iter <= maxIter
% Calculate gradient and Hessian Matrix
[g_gradient, H] = GradientSE3(R, t, M_0, M_1, X, N, E1X, E2X, E3X);
% Check optimality
if norm(g_gradient) <= tolerance
break
else
% Newton update
x_opti = - (((1 / 2) .* (H + H')) \ eye(6)) * g_gradient;
X_opti = [0 -x_opti(3) x_opti(2) x_opti(4); ...
x_opti(3) 0 -x_opti(1) x_opti(5); ...
-x_opti(2) x_opti(1) 0 x_opti(6);
0 0 0 0];
g = [R t; 0 0 0 1] * expm(X_opti);
R = g(1 : 3, 1 : 3);
t = g(1 : 3, 4);
end
iter = iter + 1;
end
end
%--------------------------------------------------------------------------
function [loglikelihood, sigma2] = Shrink_step(R, t, X, P, M_0, M_1, M_2, N)
gX = R * X + t;
gX_T = reshape(gX, 1, 3, N);
gX = reshape(gX, 3, 1, N);
% Calculate Log-likelihood
loglikelihood = sum(pagemtimes(gX_T, pagemtimes(M_0, gX)));
loglikelihood = loglikelihood - 2 * reshape(gX, 3*N, 1)' * reshape(M_1', 3*N, 1);
loglikelihood = loglikelihood + sum(M_2);
sum_P = sum(sum(P));
% Shrink local covariances
sigma2 = gather(loglikelihood / (3 * sum_P));
end
%--------------------------------------------------------------------------
function [confidence] = confidence_filter(depth)
p1 = 0.002203;
p2 = -0.001028;
p3 = 0.0005351;
min_depth = 0.4;
error = p1 * depth.^2 + p2 * depth + p3;
confidence = (p1 * min_depth + p2 * min_depth + p3) ./ error;
end
%--------------------------------end---------------------------------------