-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_to_point_icp.m
531 lines (394 loc) · 18 KB
/
point_to_point_icp.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
%% File Name: point_to_point_icp.m %%%%%%%%%%%%%%%%%%%%
%
% Description: Point to Point ICP code level implementation
%
% By Jaeyoung Jo, AI LAB (wodud3743@gmail.com)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear; clc; close all;
%% Loading Point Cloud & Configure
% Configure
lambda = 0.0; % lambda for LM optimization. If 0, Gauss-Newton
max_iter = 50;
trans_epsilon = 0.005; % meter
rot_epsilone = 0.1; % deg
max_distance = 0.2; % Not used now
ndt_grid_size = 0.2; % Not used now
debug = true; % If true, show ICP progress in visualiation
% Target, Moving ground truth transformation
gt_trans_x = 1.0;
gt_trans_y = 1.5;
gt_rot_yaw = 25 * pi/180;
% Loading Point Cloud
data = load('PointCloudwithNoise.mat'); % 'example.mat' 파일에서 데이터 불러오기
pt_cloud_origin = data.inputCloud;
pt_cloud_origin = pcdownsample(pt_cloud_origin,'gridAverage',0.01); % Downsample 1cm
% Cut
% Define x range
x_min = 0.4;
x_max = 0.6;
% Extract points within the specified x range
locations = pt_cloud_origin.Location; % Get all points' locations
indices = locations(:,2) >= x_min & locations(:,2) <= x_max; % Logical indexing for x range
% Create a new point cloud with points within the x range
filtered_pt_cloud = select(pt_cloud_origin, find(indices));
pt_cloud_origin = filtered_pt_cloud;
% Method
method = "point2point"; % svd, point2point, point2plane, ndt, gicp
% affine3d input matrix is transpose of general R matrix
gt_transform_matrix = [cos(gt_rot_yaw) sin(gt_rot_yaw) 0 0; ...
-sin(gt_rot_yaw) cos(gt_rot_yaw) 0 0; ...
0 0 1 0; ...
gt_trans_x gt_trans_y 0 1];
gt_transform_affine = affine3d(gt_transform_matrix);
% Transform moving point cloud
pt_cloud_moving = pctransform(pt_cloud_origin,gt_transform_affine);
% Downsampling
pt_cloud_moving_ds = pcdownsample(pt_cloud_moving,'gridAverage',0.02); % Downsample 2cm
%% Original Display
% Display the Original and Affine Transformed 3-D Point Clouds
figure1 = figure('WindowState','normal');
axes1 = axes('Parent',figure1);
pcshow(pt_cloud_origin,'Parent',axes1);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3-D Point Cloud','FontSize',14)
pcshowpair(pt_cloud_origin,pt_cloud_moving_ds,'VerticalAxis','Z','VerticalAxisDir','Up')
%% Run Registration
fprintf('Target point Num %d \n',pt_cloud_origin.Count);
fprintf('Moving point Num %d \n',pt_cloud_moving_ds.Count);
pt_cloud_transformed = pt_cloud_moving_ds;
% SVD
if method == "svd"
% 1. SVD Based
for iter = 1:max_iter
fprintf("Iter %d \n",iter);
% Finding corresponding
numPoints = pt_cloud_transformed.Count;
nearestIndices = zeros(numPoints, 1);
for i = 1:numPoints
% pc1의 i번째 점에 대해 pc2에서 가장 가까운 한 개의 점 찾기
[indices, distances] = findNearestNeighbors(pt_cloud_origin, pt_cloud_transformed.Location(i,:), 1);
% 가장 가까운 점의 인덱스와 거리 저장
nearestIndices(i) = indices;
end
pt_cloud_matched = select(pt_cloud_origin, nearestIndices);
fprintf('Target point matched Num %d \n',pt_cloud_matched.Count);
H = zeros(3,3);
target_mean = [mean(pt_cloud_matched.Location(:,1));
mean(pt_cloud_matched.Location(:,2));
mean(pt_cloud_matched.Location(:,3));];
moving_mean = [mean(pt_cloud_transformed.Location(:,1));
mean(pt_cloud_transformed.Location(:,2));
mean(pt_cloud_transformed.Location(:,3));];
for i = 1:numPoints
target_displaced = pt_cloud_matched.Location(i,:)' - target_mean;
moving_displaced = pt_cloud_transformed.Location(i,:)' - moving_mean;
H = H + moving_displaced*target_displaced';
end
[U, S, V] = svd(H);
R = V'*U;
pitch = asin(-R(3,1));
if cos(pitch) ~= 0
roll = atan2(R(3,2)/cos(pitch), R(3,3)/cos(pitch));
yaw = atan2(R(2,1)/cos(pitch), R(1,1)/cos(pitch));
else
roll = atan2(-R(1,2), R(1,3));
yaw = 0; % 자유도 상실로 정확한 값 계산 불가
end
% 각도를 도(degree)로 변환 (선택적)
roll = rad2deg(roll);
pitch = rad2deg(pitch);
yaw = rad2deg(yaw);
T = target_mean - R*moving_mean;
fprintf('Roll %f, Pitch %f, Yaw %f \n',roll, pitch, yaw);
fprintf('X %f, Y %f, Z %f \n',T(1), T(2), T(3));
pt_transform = [R(1,1) R(1,2) R(1,3) 0; ...
R(2,1) R(2,2) R(2,3) 0; ...
R(3,1) R(3,2) R(3,3) 0; ...
T(1) T(2) T(3) 1];
tform_iter = affine3d(pt_transform);
% Transform moving point cloud
pt_cloud_transformed = pctransform(pt_cloud_transformed,tform_iter);
end
% Point to Point
elseif method == "point2point"
total_transform = zeros(3,1);
for iter = 1:max_iter
fprintf("P2Point Iter %d \n",iter);
% Finding corresponding
numPoints = pt_cloud_transformed.Count;
coeffs = zeros(numPoints,3);
coeff_distances = zeros(numPoints,1);
for i = 1:numPoints
% pc1의 i번째 점에 대해 pc2에서 가장 가까운 한 개의 점 찾기
[indices, distances] = findNearestNeighbors(pt_cloud_origin, pt_cloud_transformed.Location(i,:), 1);
% 가장 가까운 점의 인덱스와 거리 저장
coeff_distances(i) = distances;
coeffs(i,:) = pt_cloud_transformed.Location(i,:)' - pt_cloud_origin.Location(indices,:)';
coeff_norm = sqrt(coeffs(i,1)*coeffs(i,1) + coeffs(i,2)*coeffs(i,2));
coeffs(i,1) = coeffs(i,1) / coeff_norm;
coeffs(i,2) = coeffs(i,2) / coeff_norm;
end
matA = zeros(numPoints,3);
matAt = zeros(3,numPoints);
matAtA = zeros(3,3);
matB = zeros(numPoints,1);
matAtB = zeros(3,1);
matAtAdiag = zeros(3,3);
matX = zeros(3,1); % yaw x y
% Calculate residual vector
for i = 1:numPoints
rot_yaw = pt_cloud_transformed.Location(i,1) * (coeffs(i,2)) ...
- pt_cloud_transformed.Location(i,2) * (coeffs(i,1));
matA(i,1) = rot_yaw;
matA(i,2) = coeffs(i,1);
matA(i,3) = coeffs(i,2);
matB(i,1) = -coeff_distances(i);
end
matAt = matA';
diagonal = diag(matAt * matA);
matAtAdiag = diag(diagonal);
matAtA = matAt * matA + lambda * matAtAdiag;
matAtB = matAt * matB;
matX = matAtA \ matAtB;
[E, V] = eig(matAtA);
fprintf("Eigen Value yaw, x, y \n");
disp(diag(V));
fprintf("Dyaw: %f, dx %f, dy %f \n",matX(1)*180/pi, matX(2), matX(3));
pt_transform = [cos(matX(1)) sin(matX(1)) 0 0; ...
-sin(matX(1)) cos(matX(1)) 0 0; ...
0 0 1 0; ...
matX(2) matX(3) 0 1];
tform_iter = affine3d(pt_transform);
% Transform moving point cloud
pt_cloud_transformed = pctransform(pt_cloud_transformed,tform_iter);
total_transform = total_transform + matX;
if abs(matX(2)) < trans_epsilon && abs(matX(3)) < trans_epsilon && abs(matX(1))*180/pi < rot_epsilone
break
end
if debug == true
pcshowpair(pt_cloud_origin,pt_cloud_transformed,'VerticalAxis','Z','VerticalAxisDir','Up');
pause(0.1);
end
end
fprintf("Total Dyaw: %f, dx %f, dy %f \n",total_transform(1)*180/pi, total_transform(2), total_transform(3));
fprintf("Error Dyaw: %f, dx %f, dy %f \n",(gt_rot_yaw + total_transform(1))*180/pi, gt_trans_x + total_transform(2), gt_trans_y + total_transform(3));
% Point to Plane
elseif method == "point2plane"
total_transform = zeros(3,1);
affine_matrix = [1 0 0 0; ...
0 1 0 0; ...
0 0 1 0; ...
0 0 0 1];
for iter = 1:max_iter
fprintf("P2Plane Iter %d \n",iter);
% Finding corresponding
numPoints = pt_cloud_transformed.Count;
coeffs = zeros(numPoints,3);
coeff_distances = zeros(numPoints,1);
for i = 1:numPoints
% pc1의 i번째 점에 대해 pc2에서 가장 가까운 한 개의 점 찾기
[indices, distances] = findNearestNeighbors(pt_cloud_origin, pt_cloud_transformed.Location(i,:), 5);
% 가장 가까운 점의 인덱스와 거리 저장
matA0 = zeros(5, 3);
% matB0 = zeros(5, 1)
matB0 = [-1; -1; -1; -1; -1];
for j = 1:5
matA0(j, 1) = pt_cloud_origin.Location(indices(j),1);
matA0(j, 2) = pt_cloud_origin.Location(indices(j),2);
matA0(j, 3) = pt_cloud_origin.Location(indices(j),3);
end
matX0 = matA0 \ matB0;
pa = matX0(1); % x계수
pb = matX0(2); % y계수
pc = matX0(3); % z계수
pd = 1;
ps = sqrt(pa * pa + pb * pb + pc * pc);
pa = pa/ps; pb = pb/ps; pc = pc/ps; pd = pd/ps;
pd2 = pa * pt_cloud_transformed.Location(i,1) ...
+ pb * pt_cloud_transformed.Location(i,2) ...
+ pc * pt_cloud_transformed.Location(i,3) + pd;
weight = 20;
coeffs(i,:) = [pa pb pc] * weight;
coeff_distances(i) = pd2 * weight;
end
matA = zeros(numPoints,3);
matAt = zeros(3,numPoints);
matAtA = zeros(3,3);
matB = zeros(numPoints,1);
matAtB = zeros(3,1);
matAtAdiag = zeros(3,3);
matX = zeros(3,1); % yaw x y
% Calculate residual vector
for i = 1:numPoints
rot_yaw = pt_cloud_transformed.Location(i,1) * (coeffs(i,2)) ...
- pt_cloud_transformed.Location(i,2) * (coeffs(i,1));
matA(i,1) = rot_yaw;
matA(i,2) = coeffs(i,1);
matA(i,3) = coeffs(i,2);
matB(i,1) = -coeff_distances(i);
end
% LM Optimization
matAt = matA';
diagonal = diag(matAt * matA);
matAtAdiag = diag(diagonal);
matAtA = matAt * matA + lambda * matAtAdiag;
matAtB = matAt * matB;
matX = matAtA \ matAtB;
[E, V] = eig(matAtA);
fprintf("Eigen Value yaw, x, y \n");
disp(diag(V));
fprintf("Dyaw: %f, dx %f, dy %f \n",matX(1)*180/pi, matX(2), matX(3));
pt_transform = [cos(matX(1)) sin(matX(1)) 0 0; ...
-sin(matX(1)) cos(matX(1)) 0 0; ...
0 0 1 0; ...
matX(2) matX(3) 0 1];
tform_iter = affine3d(pt_transform);
affine_matrix = affine_matrix * pt_transform;
% Transform moving point cloud
pt_cloud_transformed = pctransform(pt_cloud_transformed,tform_iter);
total_transform = total_transform + matX;
if abs(matX(2)) < trans_epsilon && abs(matX(3)) < trans_epsilon && abs(matX(1))*180/pi < rot_epsilone
break
end
if debug == true
pcshowpair(pt_cloud_origin,pt_cloud_transformed,'VerticalAxis','Z','VerticalAxisDir','Up');
pause(0.1);
end
end
translation = affine_matrix(4, 1:3);
rotationMatrix = affine_matrix(1:3, 1:3);
end_pitch = asin(-rotationMatrix(3,1));
if cos(end_pitch) ~= 0
end_roll = atan2(rotationMatrix(3,2)/cos(end_pitch), rotationMatrix(3,3)/cos(end_pitch));
end_yaw = atan2(rotationMatrix(2,1)/cos(end_pitch), rotationMatrix(1,1)/cos(end_pitch));
else
end_roll = atan2(-rotationMatrix(1,2), rotationMatrix(1,3));
end_yaw = 0; % 자유도 상실
end
fprintf("Total Dyaw: %f, dx %f, dy %f \n",end_yaw*180/pi, translation(1), translation(2));
fprintf("Error Dyaw: %f, dx %f, dy %f \n",(gt_rot_yaw + end_yaw)*180/pi, gt_trans_x + translation(1), gt_trans_y + translation(2));
% NDT (TODO)
elseif method == "ndt"
total_transform = zeros(3,1);
% Map Generation
% Iteration start
for iter = 1:max_iter
end
fprintf("Total Dyaw: %f, dx %f, dy %f \n",total_transform(1)*180/pi, total_transform(2), total_transform(3));
fprintf("Error Dyaw: %f, dx %f, dy %f \n",(gt_rot_yaw + total_transform(1))*180/pi, gt_trans_x + total_transform(2), gt_trans_y + total_transform(3));
% GICP
elseif method == "gicp"
total_transform = zeros(3,1);
affine_matrix = [1 0 0 0; ...
0 1 0 0; ...
0 0 1 0; ...
0 0 0 1];
sourceNumPoints = pt_cloud_transformed.Count;
tartgetNumPoints = pt_cloud_origin.Count;
source_covariances = zeros(3,3,sourceNumPoints);
target_covariances = zeros(3,3,tartgetNumPoints);
source_cov_rotations = zeros(3,3,sourceNumPoints);
target_cov_rotations = zeros(3,3,tartgetNumPoints);
weights = zeros(sourceNumPoints,1);
e = 0.3;
C = [1 0 0;
0 1 0;
0 0 e];
% Target point covariance in advance
for i = 1:tartgetNumPoints
[indices, distances] = findNearestNeighbors(pt_cloud_origin, pt_cloud_origin.Location(i,:), 5);
neighbors = pt_cloud_origin.Location(indices, :);
meanPoint = mean(neighbors, 1);
centeredPoints = neighbors - meanPoint;
covarianceMatrix = (centeredPoints' * centeredPoints) / (size(neighbors, 1) - 1);
[V, D] = eig(covarianceMatrix);
target_cov_rotations(:,:,i) = V;
end
for iter = 1:max_iter
fprintf("GICP Iter %d \n",iter);
% Finding corresponding
coeffs = zeros(sourceNumPoints,3);
coeff_distances = zeros(sourceNumPoints,1);
for i = 1:sourceNumPoints
% source point covariance
[source_indices, source_distances] = findNearestNeighbors(pt_cloud_transformed, pt_cloud_transformed.Location(i,:), 5);
neighbors = pt_cloud_transformed.Location(source_indices, :);
meanPoint = mean(neighbors, 1);
centeredPoints = neighbors - meanPoint;
covarianceMatrix = (centeredPoints' * centeredPoints) / (size(neighbors, 1) - 1);
[V, D] = eig(covarianceMatrix);
source_cov_rotations(:,:,i) = V;
% find nearest target covariance
[target_indice, target_distance] = findNearestNeighbors(pt_cloud_origin, pt_cloud_transformed.Location(i,:), 1);
source_rotation = source_cov_rotations(:,:,i);
target_rotation = target_cov_rotations(:,:,target_indice);
dist_vec = (pt_cloud_transformed.Location(i,:)' - pt_cloud_origin.Location(target_indice,:)');
coeff_norm = sqrt(dist_vec(1)*dist_vec(1) + dist_vec(2)*dist_vec(2));
Ca = source_rotation*C*source_rotation';
Cb = target_rotation*C*target_rotation';
weight = dist_vec'*inv(Ca + Cb) * dist_vec;
weights(i) = weight;
coeffs(i,:) = dist_vec / coeff_norm * weight;
coeff_distances(i) = target_distance * weight;
end
matA = zeros(sourceNumPoints,3);
matAt = zeros(3,sourceNumPoints);
matAtA = zeros(3,3);
matB = zeros(sourceNumPoints,1);
matAtB = zeros(3,1);
matAtAdiag = zeros(3,3);
matX = zeros(3,1); % yaw x y
% Calculate residual vector
for i = 1:sourceNumPoints
rot_yaw = pt_cloud_transformed.Location(i,1) * (coeffs(i,2)) ...
- pt_cloud_transformed.Location(i,2) * (coeffs(i,1));
matA(i,1) = rot_yaw;
matA(i,2) = coeffs(i,1);
matA(i,3) = coeffs(i,2);
matB(i,1) = -coeff_distances(i);
end
% LM Optimization
matAt = matA';
diagonal = diag(matAt * matA);
matAtAdiag = diag(diagonal);
matAtA = matAt * matA + lambda * matAtAdiag;
matAtB = matAt * matB;
matX = matAtA \ matAtB;
[E, V] = eig(matAtA);
fprintf("Eigen Value yaw, x, y \n");
disp(diag(V));
fprintf("Dyaw: %f, dx %f, dy %f \n",matX(1)*180/pi, matX(2), matX(3));
pt_transform = [cos(matX(1)) sin(matX(1)) 0 0; ...
-sin(matX(1)) cos(matX(1)) 0 0; ...
0 0 1 0; ...
matX(2) matX(3) 0 1];
tform_iter = affine3d(pt_transform);
affine_matrix = affine_matrix * pt_transform;
% Transform moving point cloud
pt_cloud_transformed = pctransform(pt_cloud_transformed,tform_iter);
total_transform = total_transform + matX;
if abs(matX(2)) < trans_epsilon && abs(matX(3)) < trans_epsilon && abs(matX(1))*180/pi < rot_epsilone
break
end
if debug == true
pcshowpair(pt_cloud_origin,pt_cloud_transformed,'VerticalAxis','Z','VerticalAxisDir','Up');
pause(0.1);
end
end
translation = affine_matrix(4, 1:3);
rotationMatrix = affine_matrix(1:3, 1:3);
end_pitch = asin(-rotationMatrix(3,1));
if cos(end_pitch) ~= 0
end_roll = atan2(rotationMatrix(3,2)/cos(end_pitch), rotationMatrix(3,3)/cos(end_pitch));
end_yaw = atan2(rotationMatrix(2,1)/cos(end_pitch), rotationMatrix(1,1)/cos(end_pitch));
else
end_roll = atan2(-rotationMatrix(1,2), rotationMatrix(1,3));
end_yaw = 0; % 자유도 상실
end
fprintf("Total Dyaw: %f, dx %f, dy %f \n",end_yaw*180/pi, translation(1), translation(2));
fprintf("Error Dyaw: %f, dx %f, dy %f \n",(gt_rot_yaw + end_yaw)*180/pi, gt_trans_x + translation(1), gt_trans_y + translation(2));
end
%% Display the Original and Affine Transformed 3-D Point Clouds
pcshowpair(pt_cloud_origin,pt_cloud_transformed,'VerticalAxis','Z','VerticalAxisDir','Up')