-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathicp.py
144 lines (104 loc) · 4.01 KB
/
icp.py
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
# Iterative Closest Point Algorithm
# Ref: https://github.com/AtsushiSakai/PythonRobotics/blob/53eae53b5a78a08b7ce4c6ffeed727c1d6a0ab2e/SLAM/iterative_closest_point/iterative_closest_point.py
import numpy as np
import matplotlib.pyplot as plt
from utils import wrapAngle
EPS = 0.0001
MAX_ITER = 100
show_animation = False
if show_animation:
fig = plt.figure()
def icp_matching(edges, scan, pose):
if len(scan) < 5 or len(edges) < len(scan):
return None
# delete duplicate scans
scan = np.unique(scan, axis=0)
# transpose edges and scan to match the implementation of algorithm
edges = edges.T
scan = scan.T
H = np.diag([1, 1, 1]) # homogeneous transformation matrix
dError = np.inf
preError = np.inf
count = 0
while dError >= EPS:
count += 1
indexes, total_error = nearest_neighbor_association(edges, scan)
edges_matched = edges[:, indexes]
if show_animation:
plot_points(edges_matched, scan, fig)
# perform RANSAC
min_error = np.float('inf')
best_Rt = None
best_Tt = None
for _ in range(15):
sample = np.random.choice(scan.shape[1], 5, replace=False)
Rt, Tt = svd_motion_estimation(edges_matched[:, sample], scan[:, sample])
temp_points = (Rt @ scan) + Tt[:, np.newaxis]
_, error = nearest_neighbor_association(edges_matched, temp_points)
if error < min_error:
min_error = error
best_Rt = Rt
best_Tt = Tt
# update current scan for iterative refinement
scan = (best_Rt @ scan) + best_Tt[:, np.newaxis]
dError = preError - total_error
#print("Residual:", error)
preError = total_error
H = update_homogeneous_matrix(H, best_Rt, best_Tt)
if MAX_ITER <= count:
break
R = np.array(H[0:-1, 0:-1])
T = np.array(H[0:-1, -1])
if abs(T[0]) > 5 or abs(T[1]) > 5:
return None
else:
x = pose[0] + T[0]
y = pose[1] + T[1]
orientation = wrapAngle(pose[2] + np.arctan2(R[1][0], R[0][0]))
return np.array((x, y, orientation))
def update_homogeneous_matrix(Hin, R, T):
H = np.zeros((3, 3))
H[0:2, 0:2] = R
H[0:2, 2] = T
H[2, 2] = 1.0
return Hin @ H
def nearest_neighbor_association(prev_points, curr_points):
d = np.linalg.norm(np.repeat(curr_points, prev_points.shape[1], axis=1)
- np.tile(prev_points, (1, curr_points.shape[1])), axis=0)
d = d.reshape(curr_points.shape[1], prev_points.shape[1])
indexes = np.argmin(d, axis=1)
error = np.min(d, axis=1)
return indexes, np.sum(error)
def svd_motion_estimation(previous_points, current_points):
pm = np.mean(previous_points, axis=1)
cm = np.mean(current_points, axis=1)
p_shift = previous_points - pm[:, np.newaxis]
c_shift = current_points - cm[:, np.newaxis]
W = c_shift @ p_shift.T
u, s, vh = np.linalg.svd(W)
R = (u @ vh).T
t = pm - (R @ cm)
return R, t
def plot_points(previous_points, current_points, figure):
# for stopping simulation with the esc key.
plt.gcf().canvas.mpl_connect(
'key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
if previous_points.shape[0] == 3:
plt.clf()
axes = figure.add_subplot(111, projection='3d')
axes.scatter(previous_points[0, :], previous_points[1, :],
previous_points[2, :], c="r", marker=".")
axes.scatter(current_points[0, :], current_points[1, :],
current_points[2, :], c="b", marker=".")
axes.scatter(0.0, 0.0, 0.0, c="r", marker="x")
figure.canvas.draw()
else:
plt.cla()
plt.plot(previous_points[0, :], previous_points[1, :], ".r", markersize=1)
plt.plot(current_points[0, :], current_points[1, :], ".b", markersize=1)
plt.plot(0.0, 0.0, "xr")
plt.axis("equal")
plt.pause(0.01)
plt.draw()
plt.clf()