-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbak_kabsch_local_minima_problem.py
176 lines (126 loc) · 4.59 KB
/
bak_kabsch_local_minima_problem.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
166
167
168
169
170
171
172
173
174
175
176
#! /usr/bin/env python
import copy
import time
import pdb
import icp
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
def plot_super(map_obstacles, laser_obstacles, title):
f = plt.figure()
sp = f.add_subplot(111)
sp.plot(map_obstacles[:, 0], map_obstacles[:, 1], '.')
sp.plot(laser_obstacles[:, 0], laser_obstacles[:, 1], '.r')
sp.set_title(title)
plt.draw()
def kabsch(p, q):
p, q = expand(p, q)
cov = np.dot(p.T, q)
U, s, V = np.linalg.svd(cov)
d = np.sign(np.linalg.det(np.multiply(V.T, U.T)))
t = np.dot(V.T, np.matrix([[1, 0], [0, d]]))
r = np.dot(t, U.T)
return r
def expand(p, q):
diff = np.max([p.shape[0], q.shape[0]]) - \
np.min([p.shape[0], q.shape[0]])
if p.shape[0] >= q.shape[0]:
for i in xrange(0, diff):
q = np.vstack([q, [0, 0]])
else:
for i in xrange(0, diff):
p = np.vstack([p, [0, 0]])
return p, q
def rotate(p, theta):
rot = np.matrix([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
return np.dot(p, rot.T)
def translate(p, t):
return np.add(p, t.T)
def bounding_box(v):
min_x, min_y = np.min(v, axis=0)
max_x, max_y = np.max(v, axis=0)
return np.array([(min_x, min_y), (max_x, min_y),
(max_x, max_y), (min_x, max_y)])
def do_kdtree(source, query):
mytree = sp.spatial.cKDTree(source)
dist, indexes = mytree.query(query)
return indexes
def compute_error(p, q):
n = len(p)
sum = 0
for i in xrange(n):
for j in xrange(n):
sum += np.square((np.linalg.norm(p[i] - p[j]) - np.linalg.norm(q[i] - q[j])))
return sum / np.square(n)
def relocalize(map_obstacles, laser_obstacles):
map_bounding = bounding_box(map_obstacles)
starting = True
while True:
# Randomize translation
x = np.random.uniform(map_bounding[0][0],
map_bounding[1][0])
y = np.random.uniform(map_bounding[0][1],
map_bounding[2][1])
if starting:
t = np.array([0, 0])
laser_trans = laser_obstacles
else:
# Apply translation
t = np.array([x, y])
laser_trans = translate(laser_obstacles, t)
# Find optimal rotation
r = kabsch(map_obstacles, laser_trans)
theta = np.arccos(r.item(0, 1))
# Apply rotation
laser_trans_rot = rotate(laser_trans, theta)
map_closest = np.array([])
print "Iterations:", len(laser_trans_rot)
start = time.time()
# for i in laser_trans_rot:
# # Get closest point
# closest = min(map_copy, key=lambda x: np.linalg.norm(x - i))
# if len(map_closest) == 0:
# map_closest = np.array(closest)
# else:
# map_closest = np.vstack((map_closest, closest))
nn_idx = do_kdtree(map_obstacles, laser_trans_rot)
nn = map_obstacles[nn_idx]
end = time.time()
print "The loop took " + str(end - start) + " seconds."
mse = (np.square(laser_trans_rot - nn)).mean(axis=None)
# mse = np.sum((np.square(laser_trans_rot - nn))) # .mean(axis=None)
# mse = compute_error(laser_trans_rot, nn)
# mse = 1
print "MSE:", mse
if mse <= 0.05:
# m = icp.icp(map_obstacles, laser_trans_rot)
# print "ICP Result:", m
break
starting = False
# pdb.set_trace()
return t, r
def main():
map_obstacles = np.loadtxt('obstacles_map.txt')
laser_obstacles = np.loadtxt('obstacles_laser.txt')
true_rotation = np.pi / 10
true_translation = np.array([5, -5])
laser_rot = rotate(laser_obstacles, true_rotation)
laser_trans = translate(laser_rot, true_translation)
t, r = relocalize(map_obstacles, laser_trans)
theta = np.arccos(r.item(0, 1))
print "True Rotation:", true_rotation
print "True Translation:", true_translation
print "-------------------------------------"
print "Estimated Rotation:", theta
print "Estimated Translation:", t
print "-------------------------------------"
print "Rotation Error:", np.abs(true_rotation - theta)
print "Translation Error:", true_translation - t
laser_reloc = rotate(laser_rot, -np.arccos(r.item(0, 1)))
plot_super(map_obstacles, laser_obstacles, "Original")
plot_super(map_obstacles, laser_trans, "Measure misaligned with map")
plot_super(map_obstacles, laser_reloc, "Measure realigned with map")
plt.show()
if __name__ == '__main__':
main()