-
Notifications
You must be signed in to change notification settings - Fork 2
/
covisibility.py
executable file
·245 lines (191 loc) · 6.62 KB
/
covisibility.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
from threading import Lock
from collections import defaultdict, Counter
from itertools import chain
class GraphKeyFrame(object):
def __init__(self):
self.id = None
self.meas = dict()
self.covisible = defaultdict(int)
self._lock = Lock()
def __hash__(self):
return self.id
def __eq__(self, rhs):
return (isinstance(rhs, GraphKeyFrame) and
self.id == rhs.id)
def __lt__(self, rhs):
return self.id < rhs.id # predate
def __le__(self, rhs):
return self.id <= rhs.id
def measurements(self):
with self._lock:
return self.meas.keys()
def mappoints(self):
with self._lock:
return self.meas.values()
def add_measurement(self, m):
with self._lock:
self.meas[m] = m.mappoint
def remove_measurement(self, m):
with self._lock:
try:
del self.meas[m]
except KeyError:
pass
def covisibility_keyframes(self):
with self._lock:
return self.covisible.copy() # shallow copy
def add_covisibility_keyframe(self, kf):
with self._lock:
self.covisible[kf] += 1
class GraphMapPoint(object):
def __init__(self):
self.id = None
self.meas = dict()
self._lock = Lock()
def __hash__(self):
return self.id
def __eq__(self, rhs):
return (isinstance(rhs, GraphMapPoint) and
self.id == rhs.id)
def __lt__(self, rhs):
return self.id < rhs.id
def __le__(self, rhs):
return self.id <= rhs.id
def measurements(self):
with self._lock:
return self.meas.keys()
def keyframes(self):
with self._lock:
return self.meas.values()
def add_measurement(self, m):
with self._lock:
self.meas[m] = m.keyframe
def remove_measurement(self, m):
with self._lock:
try:
del self.meas[m]
except KeyError:
pass
class GraphMeasurement(object):
def __init__(self):
self.keyframe = None
self.mappoint = None
@property
def id(self):
return (self.keyframe.id, self.mappoint.id)
def __hash__(self):
return hash(self.id)
def __eq__(self, rhs):
return (isinstance(rhs, GraphMeasurement) and
self.id == rhs.id)
class CovisibilityGraph(object):
def __init__(self, ):
self._lock = Lock()
self.kfs = []
self.pts = set()
self.kfs_set = set()
self.meas_lookup = dict()
def keyframes(self):
with self._lock:
return self.kfs.copy()
def mappoints(self):
with self._lock:
return self.pts.copy()
def add_keyframe(self, kf):
with self._lock:
self.kfs.append(kf)
self.kfs_set.add(kf)
def add_mappoint(self, pt):
with self._lock:
self.pts.add(pt)
def remove_mappoint(self, pt):
with self._lock:
try:
for m in pt.measurements():
m.keyframe.remove_measurement(m)
del self.meas_lookup[m.id]
self.pts.remove(pt)
except:
pass
def add_measurement(self, kf, pt, meas):
with self._lock:
if kf not in self.kfs_set or pt not in self.pts:
return
for m in pt.measurements():
if m.keyframe == kf:
continue
kf.add_covisibility_keyframe(m.keyframe)
m.keyframe.add_covisibility_keyframe(kf)
meas.keyframe = kf
meas.mappoint = pt
kf.add_measurement(meas)
pt.add_measurement(meas)
self.meas_lookup[meas.id] = meas
def remove_measurement(self, m):
m.keyframe.remove_measurement(m)
m.mappoint.remove_measurement(m)
with self._lock:
try:
del self.meas_lookup[m.id]
except:
pass
def has_measurement(self, *args):
with self._lock:
if len(args) == 1: # measurement
return args[0].id in self.meas_lookup
elif len(args) == 2: # keyframe, mappoint
id = (args[0].id, args[1].id)
return id in self.meas_lookup
else:
raise TypeError
def get_reference_frame(self, seedpoints):
assert len(seedpoints) > 0
visible = [pt.keyframes() for pt in seedpoints]
visible = Counter(chain(*visible))
return visible.most_common(1)[0][0]
def get_local_map(self, seedpoints, window_size=15):
reference = self.get_reference_frame(seedpoints)
covisible = chain(
reference.covisibility_keyframes().items(), [(reference, float('inf'))])
covisible = sorted(covisible, key=lambda _:_[1], reverse=True)
local_map = [seedpoints]
local_keyframes = []
for kf, n in covisible[:window_size]:
if n < 1:
continue
local_map.append(kf.mappoints())
local_keyframes.append(kf)
local_map = list(set(chain(*local_map)))
return local_map, local_keyframes
def get_local_map_v2(self, seedframes, window_size=12, loop_window_size=8):
covisible = []
for kf in set(seedframes):
covisible.append(Counter(kf.covisibility_keyframes()))
covisible = sum(covisible, Counter())
for kf in set(seedframes):
covisible[kf] = float('inf')
local = sorted(
covisible.items(), key=lambda _:_[1], reverse=True)
id = max([_.id for _ in covisible])
loop_frames = [_ for _ in local if _[0].id < id-50]
local = local[:window_size]
loop_local = []
if len(loop_frames) > 0:
loop_covisible = sorted(
loop_frames[0][0].covisibility_keyframes().items(),
key=lambda _:_[1], reverse=True)
for kf, n in loop_covisible:
if kf not in set([_[0] for _ in local]):
loop_local.append((kf, n))
if len(loop_local) >= loop_window_size:
break
local = chain(local, loop_local)
local_map = []
local_keyframes = []
for kf, n in local:
if n < 1:
continue
local_map.append(kf.mappoints())
local_keyframes.append(kf)
local_map = list(set(chain(*local_map)))
return local_map, local_keyframes