trw.transforms.affine

Module Contents

Functions

affine_transformation_translation(t: Sequence[float]) → torch.Tensor

Defines an affine translation for 2D or 3D data

affine_transformation_scale(s: Sequence[float]) → torch.Tensor

Defines an affine scaling transformation (2D or 3D)

affine_transformation_rotation2d(angle_radian: float) → torch.Tensor

Defines a 2D rotation transform

affine_transformation_rotation_3d_x(angle_radian: float) → torch.Tensor

Rotation in 3D around the x axis

affine_transformation_rotation_3d_y(angle_radian: float) → torch.Tensor

Rotation in 3D around the y axis

affine_transformation_get_spacing(pst: torch.Tensor) → torch.Tensor

Return the spacing (expansion factor) of the transformation per dimension XY[Z]

affine_transformation_get_origin(pst: torch.Tensor) → torch.Tensor

Return the origin of the transformation per dimension XY[Z]

affine_transformation_rotation_3d_z(angle_radian: float) → torch.Tensor

Rotation in 3D around the y axis

apply_homogeneous_affine_transform(transform: torch.Tensor, position: torch.Tensor)

Apply an homogeneous affine transform (4x4 for 3D or 3x3 for 2D) to a position

apply_homogeneous_affine_transform_zyx(transform: torch.Tensor, position_zyx: torch.Tensor)

Apply an homogeneous affine transform (4x4 for 3D or 3x3 for 2D) to a position

to_voxel_space_transform(matrix: torch.Tensor, image_shape: trw.basic_typing.ShapeCX) → torch.Tensor

Express the affine transformation in image space coordinate in range (-1, 1)

affine_transform(images: trw.basic_typing.TorchTensorNCX, affine_matrices: torch.Tensor, interpolation: str = 'bilinear', padding_mode: str = 'border', align_corners: bool = None) → trw.basic_typing.TorchTensorNCX

Transform a series of images with a series of affine transformations

trw.transforms.affine.affine_transformation_translation(t: Sequence[float]) torch.Tensor

Defines an affine translation for 2D or 3D data

For a 3D transformation, returns a 4x4 matrix:

1 0 0 X |
M = | 0 1 0 Y |
0 0 1 Z |
0 0 0 1 |
Parameters

t – a (X, Y, Z) or (X, Y) tuple

Returns

a transformation matrix

trw.transforms.affine.affine_transformation_scale(s: Sequence[float]) torch.Tensor

Defines an affine scaling transformation (2D or 3D)

For a 3D transformation, returns 4x4 matrix:

Sx 0 0 0 |
M = | 0 Sy 0 0 |
0 0 Sz 0 |
0 0 0 1 |
Parameters

s – a (Sx, Sy, Sz) or (Sx, Sy) tuple

Returns

a transformation matrix

trw.transforms.affine.affine_transformation_rotation2d(angle_radian: float) torch.Tensor

Defines a 2D rotation transform :param angle_radian: the rotation angle in radian

Returns

a 3x3 transformation matrix

trw.transforms.affine.affine_transformation_rotation_3d_x(angle_radian: float) torch.Tensor

Rotation in 3D around the x axis

Parameters

angle_radian

Returns

4x4 torch.Tensor

trw.transforms.affine.affine_transformation_rotation_3d_y(angle_radian: float) torch.Tensor

Rotation in 3D around the y axis

Parameters

angle_radian

Returns

4x4 torch.Tensor

trw.transforms.affine.affine_transformation_get_spacing(pst: torch.Tensor) torch.Tensor

Return the spacing (expansion factor) of the transformation per dimension XY[Z]

Parameters

pst – a 3x3 or 4x4 transformation matrix

Returns

XY[Z] spacing

trw.transforms.affine.affine_transformation_get_origin(pst: torch.Tensor) torch.Tensor

Return the origin of the transformation per dimension XY[Z]

Parameters

pst – a 3x3 or 4x4 transformation matrix

Returns

XY[Z] origin

trw.transforms.affine.affine_transformation_rotation_3d_z(angle_radian: float) torch.Tensor

Rotation in 3D around the y axis

Parameters

angle_radian

Returns

4x4 torch.Tensor

trw.transforms.affine.apply_homogeneous_affine_transform(transform: torch.Tensor, position: torch.Tensor)

Apply an homogeneous affine transform (4x4 for 3D or 3x3 for 2D) to a position

Parameters
  • transform – an homogeneous affine transformation

  • position – XY(Z) position

Returns

a transformed position XY(Z)

trw.transforms.affine.apply_homogeneous_affine_transform_zyx(transform: torch.Tensor, position_zyx: torch.Tensor)

Apply an homogeneous affine transform (4x4 for 3D or 3x3 for 2D) to a position

Parameters
  • transform – an homogeneous affine transformation

  • position_zyx – (Z)YX position

Returns

a transformed position (Z)YX

trw.transforms.affine.to_voxel_space_transform(matrix: torch.Tensor, image_shape: trw.basic_typing.ShapeCX) torch.Tensor

Express the affine transformation in image space coordinate in range (-1, 1)

Parameters
  • matrix – a transformation matrix for 2D or 3D transformation

  • image_shape – the transformation matrix will be mapped to the image space coordinate system (i.e., the matrix is expressed as “voxel”). Should be [C, D, H, W] or [C, H, W] matrix (no N component)

Returns

a 2x3 or 3x4 transform

See:

this is often used with trw.transforms.affine_transform or torch.nn.functional.affine_grid

trw.transforms.affine.affine_transform(images: trw.basic_typing.TorchTensorNCX, affine_matrices: torch.Tensor, interpolation: str = 'bilinear', padding_mode: str = 'border', align_corners: bool = None) trw.basic_typing.TorchTensorNCX

Transform a series of images with a series of affine transformations

Parameters
  • images – 3D or 2D images with shape [N, C, D, H, W] or [N, C, H, W] respectively

  • affine_matrices – a list of size N of 3x4 or 2x3 matrices (see trw.transforms.to_voxel_space_transform

  • interpolation – the interpolation method. Can be nearest or bilinear

  • padding_mode – the padding to be used for resampled voxels outside the image. Can be 'zeros' | 'border' | 'reflection'

  • align_corners – Geometrically, we consider the pixels of the input as squares rather than points.

Returns

images transformed