Skip to content

Commit

Permalink
use affine for registration, then apply rigid part
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrofelder committed Aug 20, 2024
1 parent 711e87c commit 7c588aa
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions examples/tadpole/prep_lowres.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
cmd = "antsRegistration_affine_SyN.sh "
cmd += f"--moving-mask {mask_path.as_posix()} "
cmd += f"--fixed-mask {target_mask_path.as_posix()} "
cmd += "--linear-type rigid " # Use rigid registration
cmd += "--linear-type affine " # Use similarity registration
cmd += "--skip-nonlinear " # Skip non-linear registration
cmd += "--clobber " # Overwrite existing files
cmd += f"{image_n4_path.as_posix()} " # moving image
Expand All @@ -204,13 +204,35 @@
)

# Load the computed rigid transformation
rigid_transform_path = file_path_with_suffix(
output_prefix, "0GenericAffine.mat"
)
transform_path = file_path_with_suffix(output_prefix, "0GenericAffine.mat")
assert (
rigid_transform_path.exists()
), f"Could not find the rigid transformation at {rigid_transform_path}."
transform_path.exists()
), f"Could not find the rigid transformation at {transform_path}."
# Use the rigid transformation to align the image

transform = ants.read_transform(transform_path)
R = np.array(transform.parameters[:9])
R = R.reshape((3, 3))
t = transform.parameters[9:]

from scipy.linalg import polar

# Perform polar decomposition to extract the rotation matrix
R_rot, _ = polar(R)

# Construct the rigid transformation matrix
print(f"Rotation: {R_rot}")
parameters = np.concatenate([R_rot.flatten(), t])
print(parameters)

# fudge the transform type due to numerical errors
# maybe assert np.allclose(np.eye(3, np.matmul(matrix, matrix.T), 1e-7)
# and then fudge?
rigid_transform = ants.new_ants_transform(transform_type="AffineTransform")
rigid_transform.set_parameters(parameters)
rigid_transform_path = file_path_with_suffix(output_prefix, "0Rigid3D.mat")
ants.write_transform(rigid_transform, rigid_transform_path.as_posix())

aligned_image = ants.apply_transforms(
fixed=target_image,
moving=image_n4,
Expand Down

0 comments on commit 7c588aa

Please sign in to comment.