-
Notifications
You must be signed in to change notification settings - Fork 45
/
feature.py
executable file
·295 lines (234 loc) · 10.5 KB
/
feature.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
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
import numpy as np
from utils import Isometry3d, to_rotation
class Feature(object):
# id for next feature
next_id = 0
# Takes a vector from the cam0 frame to the cam1 frame.
R_cam0_cam1 = None
t_cam0_cam1 = None
def __init__(self, new_id=0, optimization_config=None):
# An unique identifier for the feature.
self.id = new_id
# Store the observations of the features in the
# state_id(key)-image_coordinates(value) manner.
self.observations = dict() # <StateID, vector4d>
# 3d postion of the feature in the world frame.
self.position = np.zeros(3)
# A indicator to show if the 3d postion of the feature
# has been initialized or not.
self.is_initialized = False
# Optimization configuration for solving the 3d position.
self.optimization_config = optimization_config
def cost(self, T_c0_ci, x, z):
"""
Compute the cost of the camera observations
Arguments:
T_c0_c1: A rigid body transformation takes a vector in c0 frame
to ci frame. (Isometry3d)
x: The current estimation. (vec3)
z: The ith measurement of the feature j in ci frame. (vec2)
Returns:
e: The cost of this observation. (double)
"""
# Compute hi1, hi2, and hi3 as Equation (37).
alpha, beta, rho = x
h = T_c0_ci.R @ np.array([alpha, beta, 1.0]) + rho * T_c0_ci.t
# Predict the feature observation in ci frame.
z_hat = h[:2] / h[2]
# Compute the residual.
e = ((z_hat - z)**2).sum()
return e
def jacobian(self, T_c0_ci, x, z):
"""
Compute the Jacobian of the camera observation
Arguments:
T_c0_c1: A rigid body transformation takes a vector in c0 frame
to ci frame. (Isometry3d)
x: The current estimation. (vec3)
z: The ith measurement of the feature j in ci frame. (vec2)
Returns:
J: The computed Jacobian. (Matrix23)
r: The computed residual. (vec2)
w: Weight induced by huber kernel. (double)
"""
# Compute hi1, hi2, and hi3 as Equation (37).
alpha, beta, rho = x
h = T_c0_ci.R @ np.array([alpha, beta, 1.0]) + rho * T_c0_ci.t
h1, h2, h3 = h
# Compute the Jacobian.
W = np.zeros((3, 3))
W[:, :2] = T_c0_ci.R[:, :2]
W[:, 2] = T_c0_ci.t
J = np.zeros((2, 3))
J[0] = W[0]/h3 - W[2]*h1/(h3*h3)
J[1] = W[1]/h3 - W[2]*h2/(h3*h3)
# Compute the residual.
z_hat = np.array([h1/h3, h2/h3])
r = z_hat - z
# Compute the weight based on the residual.
e = np.linalg.norm(r)
if e <= self.optimization_config.huber_epsilon:
w = 1.0
else:
w = self.optimization_config.huber_epsilon / (2*e)
return J, r, w
def generate_initial_guess(self, T_c1_c2, z1, z2):
"""
Compute the initial guess of the feature's 3d position using
only two views.
Arguments:
T_c1_c2: A rigid body transformation taking a vector from c2 frame
to c1 frame. (Isometry3d)
z1: feature observation in c1 frame. (vec2)
z2: feature observation in c2 frame. (vec2)
Returns:
p: Computed feature position in c1 frame. (vec3)
"""
# Construct a least square problem to solve the depth.
m = T_c1_c2.R @ np.array([*z1, 1.0])
a = m[:2] - z2*m[2] # vec2
b = z2*T_c1_c2.t[2] - T_c1_c2.t[:2] # vec2
# Solve for the depth.
depth = a @ b / (a @ a)
p = np.array([*z1, 1.0]) * depth
return p
def check_motion(self, cam_states):
"""
Check the input camera poses to ensure there is enough translation
to triangulate the feature
Arguments:
cam_states: input camera poses. (dict of <CAMStateID, CAMState>)
Returns:
True if the translation between the input camera poses
is sufficient. (bool)
"""
if self.optimization_config.translation_threshold < 0:
return True
observation_ids = list(self.observations.keys())
first_id = observation_ids[0]
last_id = observation_ids[-1]
first_cam_pose = Isometry3d(
to_rotation(cam_states[first_id].orientation).T,
cam_states[first_id].position)
last_cam_pose = Isometry3d(
to_rotation(cam_states[last_id].orientation).T,
cam_states[last_id].position)
# Get the direction of the feature when it is first observed.
# This direction is represented in the world frame.
feature_direction = np.array([*self.observations[first_id][:2], 1.0])
feature_direction = feature_direction / np.linalg.norm(feature_direction)
feature_direction = first_cam_pose.R @ feature_direction
# Compute the translation between the first frame and the last frame.
# We assume the first frame and the last frame will provide the
# largest motion to speed up the checking process.
translation = last_cam_pose.t - first_cam_pose.t
parallel = translation @ feature_direction
orthogonal_translation = translation - parallel * feature_direction
return (np.linalg.norm(orthogonal_translation) >
self.optimization_config.translation_threshold)
def initialize_position(self, cam_states):
"""
Intialize the feature position based on all current available
measurements.
The computed 3d position is used to set the position member variable.
Note the resulted position is in world frame.
Arguments:
cam_states: A dict containing the camera poses with its ID as the
associated key value. (dict of <CAMStateID, CAMState>)
Returns:
True if the estimated 3d position of the feature is valid. (bool)
"""
cam_poses = [] # [Isometry3d]
measurements = [] # [vec2]
T_cam1_cam0 = Isometry3d(
Feature.R_cam0_cam1, Feature.t_cam0_cam1).inverse()
for cam_id, m in self.observations.items():
try:
cam_state = cam_states[cam_id]
except KeyError:
continue
# Add measurements.
measurements.append(m[:2])
measurements.append(m[2:])
# This camera pose will take a vector from this camera frame
# to the world frame.
cam0_pose = Isometry3d(
to_rotation(cam_state.orientation).T, cam_state.position)
cam1_pose = cam0_pose * T_cam1_cam0
cam_poses.append(cam0_pose)
cam_poses.append(cam1_pose)
# All camera poses should be modified such that it takes a vector
# from the first camera frame in the buffer to this camera frame.
T_c0_w = cam_poses[0]
cam_poses_tmp = []
for pose in cam_poses:
cam_poses_tmp.append(pose.inverse() * T_c0_w)
cam_poses = cam_poses_tmp
# Generate initial guess
initial_position = self.generate_initial_guess(
cam_poses[-2], measurements[0], measurements[-2])
solution = np.array([*initial_position[:2], 1.0]) / initial_position[2]
# Apply Levenberg-Marquart method to solve for the 3d position.
lambd = self.optimization_config.initial_damping
inner_loop_count = 0
outer_loop_count = 0
is_cost_reduced = False
delta_norm = float('inf')
# Compute the initial cost.
total_cost = 0.0
# for i, cam_pose in enumerate(cam_poses):
for cam_pose, measurement in zip(cam_poses, measurements):
total_cost += self.cost(cam_pose, solution, measurement)
# Outer loop.
while (outer_loop_count <
self.optimization_config.outer_loop_max_iteration
and delta_norm >
self.optimization_config.estimation_precision):
A = np.zeros((3, 3))
b = np.zeros(3)
for cam_pose, measurement in zip(cam_poses, measurements):
J, r, w = self.jacobian(cam_pose, solution, measurement)
if w == 1.0:
A += J.T @ J
b += J.T @ r
else:
A += w * w * J.T @ J
b += w * w * J.T @ r
# Inner loop.
# Solve for the delta that can reduce the total cost.
while (inner_loop_count <
self.optimization_config.inner_loop_max_iteration
and not is_cost_reduced):
delta = np.linalg.solve(A + lambd * np.identity(3), b) # vec3
new_solution = solution - delta
delta_norm = np.linalg.norm(delta)
new_cost = 0.0
for cam_pose, measurement in zip(cam_poses, measurements):
new_cost += self.cost(
cam_pose, new_solution, measurement)
if new_cost < total_cost:
is_cost_reduced = True
solution = new_solution
total_cost = new_cost
lambd = max(lambd/10., 1e-10)
else:
is_cost_reduced = False
lambd = min(lambd*10., 1e12)
inner_loop_count += 1
inner_loop_count = 0
outer_loop_count += 1
# Covert the feature position from inverse depth
# representation to its 3d coordinate.
final_position = np.array([*solution[:2], 1.0]) / solution[2]
# Check if the solution is valid. Make sure the feature
# is in front of every camera frame observing it.
is_valid_solution = True
for pose in cam_poses:
position = pose.R @ final_position + pose.t
if position[2] <= 0:
is_valid_solution = False
break
# Convert the feature position to the world frame.
self.position = T_c0_w.R @ final_position + T_c0_w.t
self.is_initialized = is_valid_solution
return is_valid_solution