-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolepair.py
337 lines (284 loc) · 13 KB
/
solepair.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sole import Sole
from utils.icp import icp, nearest_neighbor
from utils.util import WILLIAMS_PURPLE, WILLIAMS_GOLD
class SolePair():
'''
SolePair is a class that makes a pair of shoes. Shoeprint pairs must be
input as "mated" or "non-mated". This class has functions to align shoeprint
pairs and plot both pairs at once.
'''
def __init__(self, Q: Sole, K: Sole, mated: bool, aligned=False,
T: np.ndarray = np.identity(3, int)) -> None:
'''
SolePairs are initialized as two Sole obejcts and a boolean operator to
designate the shoeprint pair as "mated" or "non-mated".
Inputs:
Q (Sole): Shoeprint Q
K (Sole): Shoeprint K
mated (bool): Whether or not Q and K are a mated pair
aligned (bool): Whether Q and K are pre-aligned. Defaults to False
T (np.ndarray): Transformation matrix to align K to Q, of shape 3x3.
Defaults with the identity matrix.
'''
self._Q = Q
self._K = K
self._mated = mated
self._aligned = aligned
self._T = T
@property
def Q(self) -> Sole:
'''Getter function for Q Sole object '''
return self._Q
@property
def K(self) -> Sole:
'''Getter function for K Sole object'''
return self._K
@property
def mated(self) -> bool:
'''Getter function for mated boolean'''
return self._mated
@property
def aligned(self) -> bool:
'''Getter function for aligned boolean'''
return self._aligned
@property
def T(self) -> bool:
'''Getter function for the transformation matrix'''
return self._T
@aligned.setter
def aligned(self, value: bool) -> None:
'''Setter method for aligned boolean'''
self._aligned = value
@T.setter
def T(self, value: np.ndarray) -> None:
'''Setter method for the transformation matrix'''
self._T = value
def _equalize(self):
'''
Equalize is a helper function to the implementation of ICP. It selects
points such that the shoeprint point-clouds of Q and K have an equal
number of points.
Returns:
(pd.DataFrame)
(pd.DataFrame)
'''
q_pts = np.array(self.Q.coords)
k_pts = np.array(self.K.coords)
if q_pts.shape[0] > k_pts.shape[0]:
return q_pts[np.random.choice(q_pts.shape[0], k_pts.shape[0], replace=False), :], k_pts
elif q_pts.shape[0] < k_pts.shape[0]:
return q_pts, k_pts[np.random.choice(k_pts.shape[0], q_pts.shape[0], replace=False), :]
else:
return q_pts, k_pts
def _transform(self, k_pts: np.ndarray, T: np.ndarray) -> np.ndarray:
'''
Given coordinates and a transformation array found in ICP, _transform is
a helper function to icp_transform that moves the she coordinates by the
translation of T.
Inputs:
k_pts (np.ndarray): the points of shoe K, untransformed, shape nx2
T (np.ndarray): a 3x3 array used for transformation:
cos(t) -sin(t) x-translation
sin(t) cos(t) y-translation
0 0 1
Returns:
(np.ndarray): the transformed shoe k, shape nx2
'''
k_hom = np.hstack((k_pts, np.ones((k_pts.shape[0], 1))))
t_k_hom = np.dot(k_hom, T.T)
transformed_k = t_k_hom[:, :2] / t_k_hom[:, 2][:, np.newaxis]
return transformed_k
def icp_transform(self, max_iterations: int = 10000,
tolerance: float = 0.00001,
downsample_rate: float = 1.0,
random_seed: int = 0,
shift_left=False,
shift_right=False,
shift_up=False,
shift_down=False,
two_way=False,
overlap_threshold=3):
'''
icp_transform performs the alignment of the shoe pair. This uses the
iterative closest point algorithm.
The default is to do iterative closest point with no intial shift. To
make the algorithm more effective, the user can sepcify up to four
additional icp trials with different initial shifts. Two-way icp tries
aligning Q on K and K on Q and chooses the best alignment based on the
overlap_threshold. This implementation of ICP always shifts shoe K.
icp converges once the rotation matrix gets closet to 0.
Inputs:
max_iterations (int): the maximum number of iterations for icp to
converge
tolerance (float): the tolerance for the rotation matrix
downsample_rate (float)
random_seed (int)
shift_left (bool): try an intial start where the moving shoe starts
to the left of the base shoe.
shift_right (bool)
shift_up (bool)
shift_down (bool)
two_way (bool)
overlap_threshold (int): tolerance to decide best ICP in two-way
Returns:
Q coords (pd.DataFrame), new K coords (pd.DataFrame)
'''
# Default: no shift
shifts = [(0, 0)]
range = 2 * max((self.K.coords.x.max() - self.K.coords.x.min()),
(self.K.coords.y.max() - self.K.coords.y.min()))
if shift_left:
shifts.append((-range, 0))
if shift_right:
shifts.append((range, 0))
if shift_down:
shifts.append((0, -range))
if shift_up:
shifts.append((0, range))
best_T = None
best_propn_overlap = -1
best_shift = None
# record apply_to_k for the transformation that got the best proportion overlap
best_apply_to_k = None
for shift in shifts:
# apply shift
self.K.coords.loc[:, "x"] += shift[0]
self.K.coords.loc[:, "y"] += shift[1]
T, propn_overlap, apply_to_k = self._icp_helper(max_iterations=max_iterations,
tolerance=tolerance,
downsample_rate=downsample_rate,
random_seed=random_seed,
two_way=two_way,
overlap_threshold=overlap_threshold)
# Percent overlap: higher the better
if propn_overlap > best_propn_overlap:
best_propn_overlap = propn_overlap
best_T = T
best_shift = shift
best_apply_to_k = apply_to_k
# reverse shift
self.K.coords.loc[:, "x"] -= shift[0]
self.K.coords.loc[:, "y"] -= shift[1]
# If we are not applying the transformation matrix to K,
# we get the reversed T first and apply it to K later.
# Note: the reverse of a homogenous transformation is its inverse
if not best_apply_to_k:
best_T = np.linalg.inv(best_T)
self.T = best_T
# Apply the best_shift
self.K.coords.loc[:, "x"] += best_shift[0]
self.K.coords.loc[:, "y"] += best_shift[1]
self.K.coords_full.loc[:, "x"] += best_shift[0]
self.K.coords_full.loc[:, "y"] += best_shift[1]
transformed_k = pd.DataFrame(
self._transform(self.K.coords.to_numpy(), self.T))
# transform the full coordinates as well for PC metrics
transformed_k_full = pd.DataFrame(
self._transform(self.K.coords_full.to_numpy(), self.T))
self.K.aligned_coordinates = transformed_k.rename(
columns={0: 'x', 1: 'y'})
self.K.aligned_full = transformed_k_full.rename(
columns={0: 'x', 1: 'y'})
self.aligned = True
# reverse best_shift
self.K.coords.loc[:, "x"] -= best_shift[0]
self.K.coords.loc[:, "y"] -= best_shift[1]
self.K.coords_full.loc[:, "x"] -= best_shift[0]
self.K.coords_full.loc[:, "y"] -= best_shift[1]
return self.Q.coords, self.K.aligned_coordinates, self.Q.coords_full, self.K.aligned_full
def _icp_helper(self, max_iterations: int = 10000,
tolerance: float = 1e-5,
downsample_rate: float = 1.0,
random_seed: int = 0,
two_way: bool = False,
overlap_threshold=3):
'''
This is a helper function for performing ICP registration. This function
performs ICP between the Q and K shoeprint point-clouds. If two_way is
true, it calculates the proportion overlap of points with tolerance
threshold overlap_threshold for performing ICP moving Q to K and moving
K to Q. It then selects and returns the best version of the ICP
implementaiton. The returns include the best transformation matrix, the
corresponding proportion overlap, and the corresponding direction.
Inputs:
max_iterations (int): The maximum number of iterations for ICP.
Default is 10000.
tolerance (float): Tolerance for ICP convergence. Default is 1e-5.
downsample_rate (float): Proportion of points used for ICP. Default
is 1.0 (no downsampling).
random_seed (int): Default is 0.
two_way (bool): Performs two-way ICP comparison. Default is False.
overlap_trheshold: Threshold for proportion overlap. Default is 3.
Returns:
(np.ndarray): The transformation matrix aligning Q to K or K to Q
(float): the proportion overlap of the given transformation
(bool): Indicates whether the transformation should be applied to K
(True) or to Q (False)
'''
# Equalize and downsample Q and K pointclouds for faster computation
q_pts, k_pts = self._equalize()
np.random.seed(random_seed)
num_samples = int(k_pts.shape[0] * downsample_rate)
sample_indices_q = np.random.choice(
q_pts.shape[0], num_samples, replace=False)
sample_indices_k = np.random.choice(
k_pts.shape[0], num_samples, replace=False)
q_pts = q_pts[sample_indices_q]
k_pts = k_pts[sample_indices_k]
# Perform ICP
T_kq, distances_kq, _ = icp(k_pts, q_pts,
max_iterations=max_iterations,
tolerance=tolerance)
# Calculate proportion overlap based on the ICP of moving K to Q
propn_overlap_kq = np.sum(
distances_kq <= overlap_threshold) / num_samples
apply_to_k = True
# Calculate ICP in both directions
if two_way:
T_qk, _, __ = icp(q_pts, k_pts,
max_iterations=max_iterations,
tolerance=tolerance)
# To keep the metric of comparing two icps consistent
distances_qk_k_as_base, _ = nearest_neighbor(
k_pts, self._transform(q_pts, T_qk))
# Calculate proportion overlap of ICP on K to Q
propn_overlap_qk = np.sum(
distances_qk_k_as_base <= overlap_threshold) / num_samples
# Choose the better proportion overlap (higher is better)
if propn_overlap_qk > propn_overlap_kq:
apply_to_k = False
return T_qk, propn_overlap_qk, apply_to_k
return T_kq, propn_overlap_kq, apply_to_k
def plot(self,
size: float = 0.1,
aligned: bool = False,
color_q = WILLIAMS_GOLD,
color_k = WILLIAMS_PURPLE,
filename = None):
'''
A function for plotting and saving the graph of shoeprint Q and
shoeprint K. Shoeprint Q will always be on top.
Inputs:
size (float): The size of the dots on the plot of the image.
aligned (bool): Indicating if you want to access the aligned image.
color_q (str): A hex code for the color of shoeprint Q
color_k (str): A hex code for the color of shoeprint K
filename (str): A name for saving the plot. If no name is given,
the plot will not be saved.
'''
if aligned:
plt.scatter(self.K.aligned_coordinates.x, self.K.aligned_coordinates.y,
s=size, label="Aligned K", color=color_k)
else:
plt.scatter(self.K.coords.x, self.K.coords.y,
s=size, label="K", color=color_k)
plt.scatter(self.Q.coords.x, self.Q.coords.y,
s=size, label="Q", color=color_q)
plt.gca().set_aspect('equal')
plt.legend()
if filename is not None:
plt.savefig(filename, dpi=600)
plt.show()