Skip to content

Commit 64c3742

Browse files
committed
add animation
1 parent d37dad8 commit 64c3742

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed
122 KB
Loading

SLAM/iterative_closest_point/iterative_closest_point.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
3-
Iterative Closet Point (ICP) SLAM example
3+
Iterative Closest Point (ICP) SLAM example
44
55
author: Atsushi Sakai (@Atsushi_twi)
66
@@ -17,7 +17,19 @@
1717
show_animation = True
1818

1919

20-
def ICP_matching(pdata, data):
20+
def ICP_matching(ppoints, cpoints):
21+
"""
22+
Iterative Closest Point matching
23+
24+
- input
25+
ppoints: 2D points in the previous frame
26+
cpoints: 2D points in the current frame
27+
28+
- output
29+
R: Rotation matrix
30+
T: Translation vector
31+
32+
"""
2133
H = None # homogeneraous transformation matrix
2234

2335
dError = 1000.0
@@ -29,17 +41,17 @@ def ICP_matching(pdata, data):
2941

3042
if show_animation:
3143
plt.cla()
32-
plt.plot(pdata[0, :], pdata[1, :], ".r")
33-
plt.plot(data[0, :], data[1, :], ".b")
44+
plt.plot(ppoints[0, :], ppoints[1, :], ".r")
45+
plt.plot(cpoints[0, :], cpoints[1, :], ".b")
3446
plt.plot(0.0, 0.0, "xr")
3547
plt.axis("equal")
3648
plt.pause(1.0)
3749

38-
inds, error = nearest_neighbor_assosiation(pdata, data)
39-
Rt, Tt = SVD_motion_estimation(pdata[:, inds], data)
50+
inds, error = nearest_neighbor_assosiation(ppoints, cpoints)
51+
Rt, Tt = SVD_motion_estimation(ppoints[:, inds], cpoints)
4052

4153
# update current points
42-
data = (Rt * data) + Tt
54+
cpoints = (Rt * cpoints) + Tt
4355

4456
H = update_homogenerous_matrix(H, Rt, Tt)
4557

@@ -79,20 +91,20 @@ def update_homogenerous_matrix(Hin, R, T):
7991
return Hin * H
8092

8193

82-
def nearest_neighbor_assosiation(pdata, data):
94+
def nearest_neighbor_assosiation(ppoints, cpoints):
8395

8496
# calc the sum of residual errors
85-
ddata = pdata - data
86-
d = np.linalg.norm(ddata, axis=0)
97+
dcpoints = ppoints - cpoints
98+
d = np.linalg.norm(dcpoints, axis=0)
8799
error = sum(d)
88100

89101
# calc index with nearest neighbor assosiation
90102
inds = []
91-
for i in range(data.shape[1]):
103+
for i in range(cpoints.shape[1]):
92104
minid = -1
93105
mind = float("inf")
94-
for ii in range(pdata.shape[1]):
95-
d = np.linalg.norm(pdata[:, ii] - data[:, i])
106+
for ii in range(ppoints.shape[1]):
107+
d = np.linalg.norm(ppoints[:, ii] - cpoints[:, i])
96108

97109
if mind >= d:
98110
mind = d
@@ -103,13 +115,13 @@ def nearest_neighbor_assosiation(pdata, data):
103115
return inds, error
104116

105117

106-
def SVD_motion_estimation(pdata, data):
118+
def SVD_motion_estimation(ppoints, cpoints):
107119

108-
pm = np.matrix(np.mean(pdata, axis=1))
109-
cm = np.matrix(np.mean(data, axis=1))
120+
pm = np.matrix(np.mean(ppoints, axis=1))
121+
cm = np.matrix(np.mean(cpoints, axis=1))
110122

111-
pshift = np.matrix(pdata - pm)
112-
cshift = np.matrix(data - cm)
123+
pshift = np.matrix(ppoints - pm)
124+
cshift = np.matrix(cpoints - cm)
113125

114126
W = cshift * pshift.T
115127
u, s, vh = np.linalg.svd(W)
@@ -135,16 +147,16 @@ def main():
135147
# previous points
136148
px = (np.random.rand(nPoint) - 0.5) * fieldLength
137149
py = (np.random.rand(nPoint) - 0.5) * fieldLength
138-
pdata = np.matrix(np.vstack((px, py)))
150+
ppoints = np.matrix(np.vstack((px, py)))
139151

140152
# current points
141153
cx = [math.cos(motion[2]) * x - math.sin(motion[2]) * y + motion[0]
142154
for (x, y) in zip(px, py)]
143155
cy = [math.sin(motion[2]) * x + math.cos(motion[2]) * y + motion[1]
144156
for (x, y) in zip(px, py)]
145-
data = np.matrix(np.vstack((cx, cy)))
157+
cpoints = np.matrix(np.vstack((cx, cy)))
146158

147-
R, T = ICP_matching(pdata, data)
159+
R, T = ICP_matching(ppoints, cpoints)
148160

149161

150162
if __name__ == '__main__':

0 commit comments

Comments
 (0)