-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicp.py
165 lines (122 loc) · 4.55 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Iterative Closest Point (ICP) SLAM example
author: Atsushi Sakai (@Atsushi_twi), Göktuğ Karakaşlı
"""
import math
import matplotlib.pyplot as plt
import numpy as np
from test_data import TestData
# ICP parameters
EPS = 0.0001
MAX_ITER = 100
show_animation = True
def icp_matching(previous_points, current_points):
"""
Iterative Closest Point matching
- input
previous_points: 2D points in the previous frame
current_points: 2D points in the current frame
- output
R: Rotation matrix
T: Translation vector
"""
H = None # homogeneous transformation matrix
dError = 1000.0
preError = 1000.0
count = 0
while dError >= EPS:
count += 1
if show_animation: # pragma: no cover
plt.cla()
# 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])
# plt.plot(previous_points[0, :], previous_points[1, :], ".r")
plt.plot(previous_points[0, :], previous_points[1, :], color=(1.0, 0.37, 0.22, 1.0), marker=".", linestyle='')
plt.plot(current_points[0, :], current_points[1, :], color=(0.15, 0.65, 0.65, 1.0), marker=".", linestyle='')
plt.plot(0.0, 0.0, "xr")
plt.axis("equal")
plt.pause(0.05)
indexes, error = nearest_neighbor_association(previous_points, current_points)
Rt, Tt = svd_motion_estimation(previous_points[:, indexes], current_points)
# update current points
current_points = (Rt @ current_points) + Tt[:, np.newaxis]
H = update_homogeneous_matrix(H, Rt, Tt)
dError = abs(preError - error)
preError = error
print("Residual:", error)
if dError <= EPS:
print("Converge", error, dError, count)
break
elif MAX_ITER <= count:
print("Not Converge...", error, dError, count)
break
R = np.array(H[0:2, 0:2])
T = np.array(H[0:2, 2])
return R, T
def update_homogeneous_matrix(Hin, R, T):
H = np.zeros((3, 3))
H[0, 0] = R[0, 0]
H[1, 0] = R[1, 0]
H[0, 1] = R[0, 1]
H[1, 1] = R[1, 1]
H[2, 2] = 1.0
H[0, 2] = T[0]
H[1, 2] = T[1]
if Hin is None:
return H
else:
return Hin @ H
def nearest_neighbor_association(previous_points, current_points):
# calc the sum of residual errors
delta_points = previous_points - current_points
d = np.linalg.norm(delta_points, axis=0)
error = sum(d)
# calc index with nearest neighbor assosiation
d = np.linalg.norm(np.repeat(current_points, previous_points.shape[1], axis=1)
- np.tile(previous_points, (1, current_points.shape[1])), axis=0)
indexes = np.argmin(d.reshape(current_points.shape[1], previous_points.shape[1]), axis=1)
return indexes, 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 main():
print(__file__ + " start!!")
# simulation parameters
nPoint = 1000
fieldLength = 50.0
motion = [0.5, 2.0, np.deg2rad(-10.0)] # movement [x[m],y[m],yaw[deg]]
nsim = 3 # number of simulation
for _ in range(nsim):
# previous points
td = TestData()
p = td.nominal_input.sweep
px = p[:,0]
py = p[:,1]
# px = (np.random.rand(nPoint) - 0.5) * fieldLength
# py = (np.random.rand(nPoint) - 0.5) * fieldLength
previous_points = np.vstack((px, py))
input_data = td.top_wall_input1
c = input_data.sweep
cx = c[:,0]
cy = c[:,1]
# current points
# cx = [math.cos(motion[2]) * x - math.sin(motion[2]) * y + motion[0]
# for (x, y) in zip(px, py)]
# cy = [math.sin(motion[2]) * x + math.cos(motion[2]) * y + motion[1]
# for (x, y) in zip(px, py)]
current_points = np.vstack((cx, cy))
R, T = icp_matching(previous_points, current_points)
print("Calculated R:", R)
print("Calculated T:", T)
print("Real R: ", [[math.sin(input_data.theta), math.cos(input_data.theta)], [-math.cos(input_data.theta), math.sin(input_data.theta)]])
print("Real T: ", [input_data.y, -input_data.x])
if __name__ == '__main__':
main()