-
Notifications
You must be signed in to change notification settings - Fork 72
/
aligning.py
196 lines (163 loc) · 8.32 KB
/
aligning.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
'''
Normalized Object Coordinate Space for Category-Level 6D Object Pose and Size Estimation
RANSAC for Similarity Transformation Estimation
Written by Srinath Sridhar
'''
import numpy as np
import cv2
import itertools
def estimateSimilarityTransform(source: np.array, target: np.array, verbose=False):
SourceHom = np.transpose(np.hstack([source, np.ones([source.shape[0], 1])]))
TargetHom = np.transpose(np.hstack([target, np.ones([source.shape[0], 1])]))
# Auto-parameter selection based on source-target heuristics
TargetNorm = np.mean(np.linalg.norm(target, axis=1))
SourceNorm = np.mean(np.linalg.norm(source, axis=1))
RatioTS = (TargetNorm / SourceNorm)
RatioST = (SourceNorm / TargetNorm)
PassT = RatioST if(RatioST>RatioTS) else RatioTS
StopT = PassT / 100
nIter = 100
if verbose:
print('Pass threshold: ', PassT)
print('Stop threshold: ', StopT)
print('Number of iterations: ', nIter)
SourceInliersHom, TargetInliersHom, BestInlierRatio = getRANSACInliers(SourceHom, TargetHom, MaxIterations=nIter, PassThreshold=PassT, StopThreshold=StopT)
if(BestInlierRatio < 0.1):
print('[ WARN ] - Something is wrong. Small BestInlierRatio: ', BestInlierRatio)
return None, None, None, None
Scales, Rotation, Translation, OutTransform = estimateSimilarityUmeyama(SourceInliersHom, TargetInliersHom)
if verbose:
print('BestInlierRatio:', BestInlierRatio)
print('Rotation:\n', Rotation)
print('Translation:\n', Translation)
print('Scales:', Scales)
return Scales, Rotation, Translation, OutTransform
def estimateRestrictedAffineTransform(source: np.array, target: np.array, verbose=False):
SourceHom = np.transpose(np.hstack([source, np.ones([source.shape[0], 1])]))
TargetHom = np.transpose(np.hstack([target, np.ones([source.shape[0], 1])]))
RetVal, AffineTrans, Inliers = cv2.estimateAffine3D(source, target)
# We assume no shear in the affine matrix and decompose into rotation, non-uniform scales, and translation
Translation = AffineTrans[:3, 3]
NUScaleRotMat = AffineTrans[:3, :3]
# NUScaleRotMat should be the matrix SR, where S is a diagonal scale matrix and R is the rotation matrix (equivalently RS)
# Let us do the SVD of NUScaleRotMat to obtain R1*S*R2 and then R = R1 * R2
R1, ScalesSorted, R2 = np.linalg.svd(NUScaleRotMat, full_matrices=True)
if verbose:
print('-----------------------------------------------------------------------')
# Now, the scales are sort in ascending order which is painful because we don't know the x, y, z scales
# Let's figure that out by evaluating all 6 possible permutations of the scales
ScalePermutations = list(itertools.permutations(ScalesSorted))
MinResidual = 1e8
Scales = ScalePermutations[0]
OutTransform = np.identity(4)
Rotation = np.identity(3)
for ScaleCand in ScalePermutations:
CurrScale = np.asarray(ScaleCand)
CurrTransform = np.identity(4)
CurrRotation = (np.diag(1 / CurrScale) @ NUScaleRotMat).transpose()
CurrTransform[:3, :3] = np.diag(CurrScale) @ CurrRotation
CurrTransform[:3, 3] = Translation
# Residual = evaluateModel(CurrTransform, SourceHom, TargetHom)
Residual = evaluateModelNonHom(source, target, CurrScale,CurrRotation, Translation)
if verbose:
# print('CurrTransform:\n', CurrTransform)
print('CurrScale:', CurrScale)
print('Residual:', Residual)
print('AltRes:', evaluateModelNoThresh(CurrTransform, SourceHom, TargetHom))
if Residual < MinResidual:
MinResidual = Residual
Scales = CurrScale
Rotation = CurrRotation
OutTransform = CurrTransform
if verbose:
print('Best Scale:', Scales)
if verbose:
print('Affine Scales:', Scales)
print('Affine Translation:', Translation)
print('Affine Rotation:\n', Rotation)
print('-----------------------------------------------------------------------')
return Scales, Rotation, Translation, OutTransform
def getRANSACInliers(SourceHom, TargetHom, MaxIterations=100, PassThreshold=200, StopThreshold=1):
BestResidual = 1e10
BestInlierRatio = 0
BestInlierIdx = np.arange(SourceHom.shape[1])
for i in range(0, MaxIterations):
# Pick 5 random (but corresponding) points from source and target
RandIdx = np.random.randint(SourceHom.shape[1], size=5)
_, _, _, OutTransform = estimateSimilarityUmeyama(SourceHom[:, RandIdx], TargetHom[:, RandIdx])
Residual, InlierRatio, InlierIdx = evaluateModel(OutTransform, SourceHom, TargetHom, PassThreshold)
if Residual < BestResidual:
BestResidual = Residual
BestInlierRatio = InlierRatio
BestInlierIdx = InlierIdx
if BestResidual < StopThreshold:
break
# print('Iteration: ', i)
# print('Residual: ', Residual)
# print('Inlier ratio: ', InlierRatio)
return SourceHom[:, BestInlierIdx], TargetHom[:, BestInlierIdx], BestInlierRatio
def evaluateModel(OutTransform, SourceHom, TargetHom, PassThreshold):
Diff = TargetHom - np.matmul(OutTransform, SourceHom)
ResidualVec = np.linalg.norm(Diff[:3, :], axis=0)
Residual = np.linalg.norm(ResidualVec)
InlierIdx = np.where(ResidualVec < PassThreshold)
nInliers = np.count_nonzero(InlierIdx)
InlierRatio = nInliers / SourceHom.shape[1]
return Residual, InlierRatio, InlierIdx[0]
def evaluateModelNoThresh(OutTransform, SourceHom, TargetHom):
Diff = TargetHom - np.matmul(OutTransform, SourceHom)
ResidualVec = np.linalg.norm(Diff[:3, :], axis=0)
Residual = np.linalg.norm(ResidualVec)
return Residual
def evaluateModelNonHom(source, target, Scales, Rotation, Translation):
RepTrans = np.tile(Translation, (source.shape[0], 1))
TransSource = (np.diag(Scales) @ Rotation @ source.transpose() + RepTrans.transpose()).transpose()
Diff = target - TransSource
ResidualVec = np.linalg.norm(Diff, axis=0)
Residual = np.linalg.norm(ResidualVec)
return Residual
def testNonUniformScale(SourceHom, TargetHom):
OutTransform = np.matmul(TargetHom, np.linalg.pinv(SourceHom))
ScaledRotation = OutTransform[:3, :3]
Translation = OutTransform[:3, 3]
Sx = np.linalg.norm(ScaledRotation[0, :])
Sy = np.linalg.norm(ScaledRotation[1, :])
Sz = np.linalg.norm(ScaledRotation[2, :])
Rotation = np.vstack([ScaledRotation[0, :] / Sx, ScaledRotation[1, :] / Sy, ScaledRotation[2, :] / Sz])
print('Rotation matrix norm:', np.linalg.norm(Rotation))
Scales = np.array([Sx, Sy, Sz])
# # Check
# Diff = TargetHom - np.matmul(OutTransform, SourceHom)
# Residual = np.linalg.norm(Diff[:3, :], axis=0)
return Scales, Rotation, Translation, OutTransform
def estimateSimilarityUmeyama(SourceHom, TargetHom):
# Copy of original paper is at: http://web.stanford.edu/class/cs273/refs/umeyama.pdf
SourceCentroid = np.mean(SourceHom[:3, :], axis=1)
TargetCentroid = np.mean(TargetHom[:3, :], axis=1)
nPoints = SourceHom.shape[1]
CenteredSource = SourceHom[:3, :] - np.tile(SourceCentroid, (nPoints, 1)).transpose()
CenteredTarget = TargetHom[:3, :] - np.tile(TargetCentroid, (nPoints, 1)).transpose()
CovMatrix = np.matmul(CenteredTarget, np.transpose(CenteredSource)) / nPoints
if np.isnan(CovMatrix).any():
print('nPoints:', nPoints)
print(SourceHom.shape)
print(TargetHom.shape)
raise RuntimeError('There are NANs in the input.')
U, D, Vh = np.linalg.svd(CovMatrix, full_matrices=True)
d = (np.linalg.det(U) * np.linalg.det(Vh)) < 0.0
if d:
D[-1] = -D[-1]
U[:, -1] = -U[:, -1]
Rotation = np.matmul(U, Vh).T # Transpose is the one that works
varP = np.var(SourceHom[:3, :], axis=1).sum()
ScaleFact = 1/varP * np.sum(D) # scale factor
Scales = np.array([ScaleFact, ScaleFact, ScaleFact])
ScaleMatrix = np.diag(Scales)
Translation = TargetHom[:3, :].mean(axis=1) - SourceHom[:3, :].mean(axis=1).dot(ScaleFact*Rotation)
OutTransform = np.identity(4)
OutTransform[:3, :3] = ScaleMatrix @ Rotation
OutTransform[:3, 3] = Translation
# # Check
# Diff = TargetHom - np.matmul(OutTransform, SourceHom)
# Residual = np.linalg.norm(Diff[:3, :], axis=0)
return Scales, Rotation, Translation, OutTransform