Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion biahub/estimate_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from biahub.register import (
convert_transform_to_ants,
convert_transform_to_numpy,
get_3D_fliplr_matrix,
get_3D_rescaling_matrix,
get_3D_rotation_matrix,
)
Expand Down Expand Up @@ -455,6 +456,7 @@ def user_assisted_registration(
target_channel_voxel_size: tuple[float, float, float],
similarity: bool = False,
pre_affine_90degree_rotation: int = 0,
pre_affine_fliplr: bool = False,
) -> list[ArrayLike]:
"""
Perform user-assisted registration of two volumetric image channels.
Expand All @@ -481,6 +483,9 @@ def user_assisted_registration(
if False, use an Euclidean transform (rotation, translation).
pre_affine_90degree_rotation : int
Number of 90-degree rotations to apply to the source channel before registration.
pre_affine_fliplr : bool
If True, apply left-right flip to the source channel before registration.
Note: Flip is applied first, then rotation.

Returns
-------
Expand Down Expand Up @@ -569,7 +574,17 @@ def user_assisted_registration(
90 * pre_affine_90degree_rotation,
(target_channel_Z, target_channel_Y, target_channel_X),
)
compound_affine = scaling_affine @ rotate90_affine

# Apply flip transformation if requested (flip happens first)
if pre_affine_fliplr:
fliplr_affine = get_3D_fliplr_matrix(
(source_channel_Z, source_channel_Y, source_channel_X),
(target_channel_Z, target_channel_Y, target_channel_X),
)
else:
fliplr_affine = np.eye(4)

compound_affine = scaling_affine @ rotate90_affine @ fliplr_affine
tx_manual = convert_transform_to_ants(compound_affine).invert()

source_zxy_pre_reg = tx_manual.apply_to_image(source_zyx_ants, reference=target_zyx_ants)
Expand Down Expand Up @@ -2126,6 +2141,7 @@ def estimate_registration(
else False
),
pre_affine_90degree_rotation=settings.manual_registration_settings.affine_90degree_rotation,
pre_affine_fliplr=settings.manual_registration_settings.affine_fliplr,
)

else:
Expand Down
34 changes: 34 additions & 0 deletions biahub/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,40 @@ def get_3D_rotation_matrix(
return rotation_matrix


def get_3D_fliplr_matrix(start_shape_zyx: tuple, end_shape_zyx: tuple = None) -> np.ndarray:
"""
Get 3D left-right flip transformation matrix.

Parameters
----------
start_shape_zyx : tuple
Shape of the source volume (Z, Y, X).
end_shape_zyx : tuple, optional
Shape of the target volume (Z, Y, X). If None, uses start_shape_zyx.

Returns
-------
np.ndarray
4x4 transformation matrix for left-right flip.
"""
center_X_start = start_shape_zyx[-1] / 2
if end_shape_zyx is None:
center_X_end = center_X_start
else:
center_X_end = end_shape_zyx[-1] / 2

# Flip matrix: reflects across X axis and translates to maintain center
flip_matrix = np.array(
[
[1, 0, 0, 0], # Z unchanged
[0, 1, 0, 0], # Y unchanged
[0, 0, -1, 2 * center_X_end], # X flipped and translated
[0, 0, 0, 1], # Homogeneous coordinate
]
)
return flip_matrix


def convert_transform_to_ants(T_numpy: np.ndarray):
"""Homogeneous 3D transformation matrix from numpy to ants

Expand Down
1 change: 1 addition & 0 deletions biahub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class AntsRegistrationSettings(MyBaseModel):
class ManualRegistrationSettings(MyBaseModel):
time_index: int = 0
affine_90degree_rotation: int = 0
affine_fliplr: bool = False


class EstimateRegistrationSettings(MyBaseModel):
Expand Down