Skip to content

Instantly share code, notes, and snippets.

@gattia
Last active January 28, 2022 18:31
Show Gist options
  • Save gattia/3408708b1c3b03bd3502fd7528ad4bac to your computer and use it in GitHub Desktop.
Save gattia/3408708b1c3b03bd3502fd7528ad4bac to your computer and use it in GitHub Desktop.
import numpy as np
from pycpd import RigidRegistration
# Based off of this example: https://github.com/siavashk/pycpd/blob/master/examples/bunny_rigid_3D.py
X = np.loadtxt('data/bunny_target.txt')
# synthetic data, equaivalent to X + 1
Y = np.loadtxt('data/bunny_source.txt')
reg = RigidRegistration(**{'X': X, 'Y': Y})
reg.register()
# NEED TO CREATE A TRANFORMATION MATRIX:
transform = np.eye(4)
# The below transform information comes from here:
# https://github.com/siavashk/pycpd/blob/b608a35702131d66163ba0796322239b39927985/pycpd/rigid_registration.py#L101
# You should be able to apply the same techniques (inverse transform) to the affine if you
# translat the transform matrix from here:
# https://github.com/siavashk/pycpd/blob/b608a35702131d66163ba0796322239b39927985/pycpd/affine_registration.py#L73
# which is VERY simila - you can't use this method for the deformable registration
transform[:3,:3] = reg.R * reg.s
transform[3, :3] = reg.t
# We can apply this to the original Y to ensure it does as we expect
# However, we must add a column of 1s to the Y becuase we are using 4x4 transform matrix (wiht translation)
Y4 = np.concatenate((Y, np.ones((Y.shape[0], 1))), axis=-1)
TY4 = np.dot(Y4, transform)
assert np.max(TY4[:,:3] - X) < 1e-3
# Alright, so we have the right transformation. Now we need the inverse
inv_transform = np.linalg.inv(transform)
# To apply to TY4 and get back Y
Y_ = np.dot(TY4, inv_transform)
assert np.max(Y_[:,:3] - Y) < 1e-5
# To apply to X to align it with Y
X4 = np.concatenate((X, np.ones((X.shape[0], 1))), axis=-1)
TX4 = np.dot(X4, transform)
assert np.max(TX4[:,:3] - Y) < 1e-3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment