-
Notifications
You must be signed in to change notification settings - Fork 26
/
ptam.py
executable file
·314 lines (234 loc) · 9.72 KB
/
ptam.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import numpy as np
import time
from itertools import chain
from collections import defaultdict
from covisibility import CovisibilityGraph
from optimization import BundleAdjustment
from mapping import Mapping
from mapping import MappingThread
from components import Measurement
from motion import MotionModel
from loopclosing import LoopClosing
class Tracking(object):
def __init__(self, params):
self.optimizer = BundleAdjustment()
self.min_measurements = params.pnp_min_measurements
self.max_iterations = params.pnp_max_iterations
def refine_pose(self, pose, cam, measurements):
assert len(measurements) >= self.min_measurements, (
'Not enough points')
self.optimizer.clear()
self.optimizer.add_pose(0, pose, cam, fixed=False)
for i, m in enumerate(measurements):
self.optimizer.add_point(i, m.mappoint.position, fixed=True)
self.optimizer.add_edge(0, i, 0, m)
self.optimizer.optimize(self.max_iterations)
return self.optimizer.get_pose(0)
class RGBDPTAM(object):
def __init__(self, params):
self.params = params
self.graph = CovisibilityGraph()
self.mapping = MappingThread(self.graph, params)
self.tracker = Tracking(params)
self.motion_model = MotionModel(params)
self.loop_closing = LoopClosing(self, params)
self.loop_correction = None
self.reference = None # reference keyframe
self.preceding = None # last keyframe
self.current = None # current frame
self.candidates = [] # candidate keyframes
self.results = [] # tracking results
self.status = defaultdict(bool)
def stop(self):
self.mapping.stop()
if self.loop_closing is not None:
self.loop_closing.stop()
def initialize(self, frame):
mappoints, measurements = frame.cloudify()
assert len(mappoints) >= self.params.init_min_points, (
'Not enough points to initialize map.')
keyframe = frame.to_keyframe()
keyframe.set_fixed(True)
self.graph.add_keyframe(keyframe)
self.mapping.add_measurements(keyframe, mappoints, measurements)
if self.loop_closing is not None:
self.loop_closing.add_keyframe(keyframe)
self.reference = keyframe
self.preceding = keyframe
self.current = keyframe
self.status['initialized'] = True
self.motion_model.update_pose(
frame.timestamp, frame.position, frame.orientation)
def track(self, frame):
while self.is_paused():
time.sleep(1e-4)
self.set_tracking(True)
self.current = frame
print('Tracking:', frame.idx, ' <- ', self.reference.id, self.reference.idx)
predicted_pose, _ = self.motion_model.predict_pose(frame.timestamp)
frame.update_pose(predicted_pose)
if self.loop_closing is not None:
if self.loop_correction is not None:
estimated_pose = g2o.Isometry3d(
frame.orientation,
frame.position)
estimated_pose = estimated_pose * self.loop_correction
frame.update_pose(estimated_pose)
self.motion_model.apply_correction(self.loop_correction)
self.loop_correction = None
local_mappoints = self.filter_points(frame)
measurements = frame.match_mappoints(
local_mappoints, Measurement.Source.TRACKING)
print('measurements:', len(measurements), ' ', len(local_mappoints))
tracked_map = set()
for m in measurements:
mappoint = m.mappoint
mappoint.update_descriptor(m.get_descriptor())
mappoint.increase_measurement_count()
tracked_map.add(mappoint)
try:
self.reference = self.graph.get_reference_frame(tracked_map)
pose = self.tracker.refine_pose(frame.pose, frame.cam, measurements)
frame.update_pose(pose)
self.motion_model.update_pose(
frame.timestamp, pose.position(), pose.orientation())
# if len() > 40:
self.candidates.append(frame)
self.params.relax_tracking(False)
self.results.append(True) # tracking succeed
except:
self.params.relax_tracking(True)
self.results.append(False) # tracking fail
remedy = False
if self.results[-2:].count(False) == 2:
if (len(self.candidates) > 0 and
self.candidates[-1].idx > self.preceding.idx):
frame = self.candidates[-1]
remedy = True
else:
print('tracking failed!')
if remedy or (
self.results[-1] and self.should_be_keyframe(frame, measurements)):
keyframe = frame.to_keyframe()
keyframe.update_reference(self.reference)
keyframe.update_preceding(self.preceding)
self.mapping.add_keyframe(keyframe, measurements)
if self.loop_closing is not None:
self.loop_closing.add_keyframe(keyframe)
self.preceding = keyframe
print('new keyframe', keyframe.idx)
self.set_tracking(False)
def filter_points(self, frame):
# local_mappoints = self.graph.get_local_map(self.tracked_map)[0]
local_mappoints = self.graph.get_local_map_v2(
[self.preceding, self.reference])[0]
can_view = frame.can_view(local_mappoints)
print('filter points:', len(local_mappoints), can_view.sum(),
len(self.preceding.mappoints()),
len(self.reference.mappoints()))
checked = set()
filtered = []
for i in np.where(can_view)[0]:
pt = local_mappoints[i]
if pt.is_bad():
continue
pt.increase_projection_count()
filtered.append(pt)
checked.add(pt)
for reference in set([self.preceding, self.reference]):
for pt in reference.mappoints(): # neglect can_view test
if pt in checked or pt.is_bad():
continue
pt.increase_projection_count()
filtered.append(pt)
return filtered
def should_be_keyframe(self, frame, measurements):
if self.adding_keyframes_stopped():
return False
n_matches = len(measurements)
return (n_matches < self.params.min_tracked_points and
n_matches > self.params.pnp_min_measurements)
def set_loop_correction(self, T):
self.loop_correction = T
def is_initialized(self):
return self.status['initialized']
def pause(self):
self.status['paused'] = True
def unpause(self):
self.status['paused'] = False
def is_paused(self):
return self.status['paused']
def is_tracking(self):
return self.status['tracking']
def set_tracking(self, status):
self.status['tracking'] = status
def stop_adding_keyframes(self):
self.status['adding_keyframes_stopped'] = True
def resume_adding_keyframes(self):
self.status['adding_keyframes_stopped'] = False
def adding_keyframes_stopped(self):
return self.status['adding_keyframes_stopped']
if __name__ == '__main__':
import cv2
import g2o
import os
import sys
import argparse
from threading import Thread
from components import Camera
from components import RGBDFrame
from feature import ImageFeature
from params import Params
from dataset import TUMRGBDDataset, ICLNUIMDataset
parser = argparse.ArgumentParser()
parser.add_argument('--no-viz', action='store_true', help='do not visualize')
parser.add_argument('--dataset', type=str, default='ICL',
help='dataset (TUM/ICL-NUIM)')
# parser.add_argument('--path', type=str, help='dataset path',
# default='path/to/your/TUM_RGBD/rgbd_dataset_freiburg1_room')
parser.add_argument('--path', type=str, help='dataset path',
default='path/to/your/ICL-NUIM_RGBD/living_room_traj3_frei_png')
args = parser.parse_args()
if 'tum' in args.dataset.lower():
dataset = TUMRGBDDataset(args.path)
elif 'icl' in args.dataset.lower():
dataset = ICLNUIMDataset(args.path)
params = Params()
ptam = RGBDPTAM(params)
if not args.no_viz:
from viewer import MapViewer
viewer = MapViewer(ptam, params)
height, width = dataset.rgb.shape[:2]
cam = Camera(
dataset.cam.fx, dataset.cam.fy, dataset.cam.cx, dataset.cam.cy,
width, height, dataset.cam.scale,
params.virtual_baseline, params.depth_near, params.depth_far,
params.frustum_near, params.frustum_far)
durations = []
for i in range(len(dataset))[:]:
feature = ImageFeature(dataset.rgb[i], params)
depth = dataset.depth[i]
if dataset.timestamps is None:
timestamp = i / 20.
else:
timestamp = dataset.timestamps[i]
time_start = time.time()
feature.extract()
frame = RGBDFrame(i, g2o.Isometry3d(), feature, depth, cam, timestamp=timestamp)
if not ptam.is_initialized():
ptam.initialize(frame)
else:
ptam.track(frame)
duration = time.time() - time_start
durations.append(duration)
print('duration', duration)
print()
print()
if not args.no_viz:
viewer.update()
print('num frames', len(durations))
print('num keyframes', len(ptam.graph.keyframes()))
print('average time', np.mean(durations))
ptam.stop()
if not args.no_viz:
viewer.stop()