Normalizing and unnorm don't work for SimilarityTransform and higher
david-hoffman opened this issue · comments
dphtools/dphtools/utils/registration.py
Line 431 in 84ec9f0
self.scale_x = self.scale_y = 1 / np.array((anisotropic_scale, anisotropic_scale)) |
Something is wrong with anisotropic scaling when rotations are involved.
dphtools/dphtools/utils/registration.py
Lines 255 to 299 in 84ec9f0
def norm_data(self): | |
"""Normalize data to mean 0 and unit variance.""" | |
# calculate mean displacement | |
logger.debug("Normalizing data") | |
self.ty = self.Y.mean(0, keepdims=True) | |
self.tx = self.X.mean(0, keepdims=True) | |
logger.debug("tx = {}, ty = {}".format(self.tx, self.ty)) | |
# move point clouds | |
self._Y_orig = self.Y | |
self._X_orig = self.X | |
self.Y = self.Y - self.ty | |
self.X = self.X - self.tx | |
# calculate scale | |
self.calc_init_scale() | |
logger.debug("scale_x = {}, scale_y = {}".format(self.scale_x, self.scale_y)) | |
# apply scale | |
Sx = np.diag(self.scale_x) | |
Sy = np.diag(self.scale_y) | |
Sx_1 = np.diag(1 / self.scale_x) | |
Sy_1 = np.diag(1 / self.scale_y) | |
self.Y = self.Y @ Sy | |
self.X = self.X @ Sx | |
self.translation = (self.ty @ self.B.T + self.translation - self.tx) @ Sx | |
logger.debug(f"B = {self.B}") | |
self.B = Sx @ self.B @ Sy_1 | |
logger.debug(f"B = {self.B}") | |
def unnorm_data(self): | |
"""Undo the intial normalization.""" | |
logger.debug("Undoing normalization") | |
logger.debug("tx = {}, ty = {}".format(self.tx, self.ty)) | |
logger.debug("scale_x = {}, scale_y = {}".format(self.scale_x, self.scale_y)) | |
Sx = np.diag(self.scale_x) | |
Sy = np.diag(self.scale_y) | |
Sx_1 = np.diag(1 / self.scale_x) | |
Sy_1 = np.diag(1 / self.scale_y) | |
# the scale matrices are diagonal so S.T == S | |
self.Y = self.Y @ Sy_1 + self.ty | |
self.X = self.X @ Sx_1 + self.tx | |
assert np.allclose(self.Y, self._Y_orig), "Failed to revert" | |
assert np.allclose(self.X, self._X_orig), "Failed to revert" | |
# B doesn't need to be transposed and | |
self.B = Sx_1 @ self.B @ Sy | |
self.translation = -self.ty @ self.B.T + self.translation @ Sx_1 + self.tx |
These lines seem at fault but not sure why. They seem to work for Rigid and translation.