-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration.py
235 lines (188 loc) · 8.83 KB
/
registration.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
import numpy as np
import scipy as sp
from ipdb import set_trace as st
import skimage as ski
import utils
from matplotlib import pyplot as plt
import matplotlib as mpl
from common import *
from munch import Munch as M
from scipy import sparse
from scipy.interpolate import Rbf
import os
class Register:
def __init__(
self,
work_dir,
params = M(
max_stars = 500, #take this many stars at most
nneighbors = 500, #must be even (1k before)
#more stars
# max_stars = 5000, #take this many stars at most
# nneighbors = 1000, #must be even (1k before)
ba_max_ratio = 0.99,
cb_max_ratio = 0.99,
epsilon = 1E-3, #match tol
min_abs_diff = 1, #abs and rel diff for match success
min_rel_diff = 1.4,
ransac_iters = 50,
ransac_keep_percentile = 99,
linear_fit_tol = 2.0, #pixels tol on linear fit
)):
self.work_dir = work_dir
for k in params:
setattr(self, k, params[k])
def gen_triangles(self, pts):
NN = min(self.nneighbors, 2*((pts.shape[0]-1)//2))
#1st nn is the point itself, so we need to trim it out...
indices = utils.get_nearest_neighbors(pts, NN+1)[1][:,1:] #N x nbs
indices = indices.reshape(pts.shape[0], NN//2, 2)
indices = cat(
(indices,
np.tile(np.arange(pts.shape[0])[:,None,None], (1,NN//2,1))),
axis = 2
)
indices = indices.reshape(-1,3) #triangle indices..
distances = np.stack((
np.linalg.norm(pts[indices[:,0]] - pts[indices[:,1]], axis = 1),
np.linalg.norm(pts[indices[:,0]] - pts[indices[:,2]], axis = 1),
np.linalg.norm(pts[indices[:,1]] - pts[indices[:,2]], axis = 1),
), axis = 1) #Tx3 distancse
#a triangle has 5 components...
#1. ratio of sides b/a, c/b and index of the elements i,j,k
# long and medium being i, long and short being j, medium and short being k
dorder = distances.argsort(axis=1)
dsorted = np.sort(distances, axis = -1)
# there's a kind of clever trick here...
# if dorder[:,0] == 0, then shortest edge is 01, which means lm must be 2
# if dorder[:,2] == 2, then longest edge is 12, which means lm must be 0
lm_i = 2*(dorder[:,0] == 0) + 1*(dorder[:,0] == 1) + 0*(dorder[:,0] == 2)
ms_i = 2*(dorder[:,2] == 0) + 1*(dorder[:,2] == 1) + 0*(dorder[:,2] == 2)
ls_i = 3 - lm_i - ms_i
ba_r = dsorted[:,1] / dsorted[:,2]
cb_r = dsorted[:,0] / dsorted[:,1]
tri_ratio = np.stack((ba_r, cb_r), axis = 1)
tri_index_local = np.stack((lm_i, ms_i, ls_i), axis = 1)
tri_index = indices[
np.arange(indices.shape[0]).repeat(3),
tri_index_local.reshape(-1)
].reshape(-1,3)
#filter ba as described in paper to reduce false matches
valid = (ba_r < self.ba_max_ratio) & (cb_r < self.cb_max_ratio)
tri_ratio = tri_ratio[valid]
tri_index = tri_index[valid]
# visualize trangles
# ax = plt.axes()
# scatter(pts, ax)
# tripaths = mmap(lambda tri: mpl.path.Path(pts[tri][:,::-1], closed=False), tri_index)
# patches = mpl.collections.PathCollection(tripaths, linewidths=0.1, facecolors='none')
# ax.add_artist(patches)
# plt.show()
print(f'{tri_ratio.shape[0]} triangles generated')
return M(ratio = tri_ratio, index = tri_index)
def match_triangles(self, source, target, source_tris, target_tris):
''' using the voting algorithm '''
N, M = source.shape[0], target.shape[0]
# scatterk(source_tris.ratio, c='red')
# scatterk(target_tris.ratio, c='blue')
# plt.show()
matches = utils.nearby_pairs(source_tris.ratio, target_tris.ratio, self.epsilon)
print(f'{matches.shape[0]} triangle correspondences found')
source_pts = source_tris.index[matches[:,0]].reshape(-1)
target_pts = target_tris.index[matches[:,1]].reshape(-1)
coords = np.stack([source_pts, target_pts], axis = 1)
ucoords, counts = np.unique(coords, axis = 0, return_counts = True)
votes = np.zeros((N,M), dtype = int)
votes[ucoords[...,0], ucoords[...,1]] = counts
#"best" matches
cy, cx = ((votes >= votes.max(axis=0)[None]) & (votes >= votes.max(axis=1)[...,None])).nonzero()
cvotes = votes[cy,cx]
#now we need the runner up votes
runner_up = np.maximum(
np.sort(votes, axis = 1)[:,2][:,None],
np.sort(votes, axis = 0)[-2][None],
)[cy,cx]
good_match = (cvotes-runner_up >= self.min_abs_diff) & (cvotes/(runner_up+1E-3) > self.min_rel_diff)
matches = np.stack((cy,cx),axis=1)[good_match]
print(f'matched {len(matches)} stars')
return matches
def ransac_linear(self, source, target):
'''
fit a affine transform from source to target
discard outlier points
'''
valid = np.ones(source.shape[0], dtype = np.bool)
for i in range(self.ransac_iters):
T, residuals = utils.fit_affine(source[valid], target[valid])
residuals = np.linalg.norm(residuals, axis = -1)
if i == self.ransac_iters -1:
valid_criteria = residuals < self.linear_fit_tol
else:
valid_criteria = residuals < np.percentile(residuals, self.ransac_keep_percentile)
valid[valid] &= valid_criteria
if residuals[valid_criteria].mean() < self.linear_fit_tol/2:
break
source = source[valid]
target = target[valid]
print(f'{source.shape[0]} inlier stars, mean error {residuals.mean():.3f} px')
print(T)
return source, target, T
def register(self, source, target):
source, target = source[...,:2], target[...,:2] #don't make use of std
source_tris = self.gen_triangles(source)
target_tris = self.gen_triangles(target)
matches = self.match_triangles(source, target, source_tris, target_tris)
source_matched = source[matches[...,0]]
target_matched = target[matches[...,1]]
# ax = plt.axes()
# scatterk(source_matched, c='red', ax=ax)
# scatterk(target_matched, c='blue', ax=ax)
# corresp = mmap(lambda st: mpl.path.Path(np.stack(st,axis=0)[:,::-1]), zip(source_matched, target_matched))
# patches = mpl.collections.PathCollection(corresp, linewidths=1, facecolors='none', alpha=0.5)
# ax.add_artist(patches)
# plt.show()
# st()
#this step fits a linear transform and discards outliers
source_lin, target_lin, T = self.ransac_linear(source_matched, target_matched)
# source_at_target = dehom(hom(source_lin) @ T)
# ax = plt.axes()
# scatterk(source_at_target, c='red', ax=ax)
# scatterk(target_lin, c='blue', ax=ax)
# corresp = mmap(lambda st: mpl.path.Path(np.stack(st,axis=0)[:,::-1]), zip(source_at_target, target_lin))
# patches = mpl.collections.PathCollection(corresp, linewidths=1, facecolors='none', alpha=0.5)
# ax.add_artist(patches);
# ax.set_aspect('equal')
# plt.show()
# st()
return cat((source_lin, target_lin), axis=1) #Nx4
def __call__(self, paths, other=None):
stars = mmap(np.load, paths)
#sort by #stars, descending
paths, stars = zip(*sorted(
zip(paths, stars),
key = lambda x: -x[1].shape[0]
))
if other is None:
stars = [star[-self.max_stars:] for star in stars]
for i, star in enumerate(stars[1:]):
matches = self.register(stars[0], star)
name0 = os.path.basename(paths[0]).replace('.npy', '')
namei = os.path.basename(paths[i+1]).replace('.npy', '')
out_path = f'{self.work_dir}/registration/{name0}-{namei}'
np.save(out_path, matches)
else:
other_stars = mmap(np.load, other)
other_paths, other_stars = zip(*sorted(
zip(other, other_stars),
key = lambda x: -x[1].shape[0]
))
refstars = other_stars[0][-self.max_stars:]
stars = [star[-self.max_stars:] for star in stars]
for i, star in enumerate(stars):
matches = self.register(refstars, star)
name0 = 'rel'
namei = os.path.basename(paths[i]).replace('.npy', '')
out_path = f'{self.work_dir}/registration/{name0}-{namei}'
np.save(out_path, matches)
if __name__ == '__main__':
pass