Insight Toolkit for N-dimensional image processing, segmentation, and registration in medical and scientific applications
—
Spatial transformations for image registration, resampling, and geometric corrections including linear transforms, rigid transforms, and deformable transforms.
class IdentityTransform:
"""
Identity transform (no transformation).
Template parameter:
- scalar_type: type (typically double)
- dimension: int
"""
class TranslationTransform:
"""
Pure translation transform.
Template parameter:
- scalar_type: type (typically double)
- dimension: int
"""
def SetOffset(self, offset):
"""
Set the translation offset.
Parameters:
- offset: array-like - Translation vector
"""
def GetOffset(self):
"""Get the translation offset."""class AffineTransform:
"""
General affine transformation (translation, rotation, scaling, shearing).
Template parameter:
- scalar_type: type (typically double)
- dimension: int
"""
def SetMatrix(self, matrix):
"""
Set the matrix component.
Parameters:
- matrix: itk.Matrix - Linear transformation matrix
"""
def GetMatrix(self):
"""Get the matrix component."""
def SetOffset(self, offset):
"""Set the offset (translation) component."""
def GetOffset(self):
"""Get the offset component."""
def SetCenter(self, center):
"""Set the center of rotation."""
def GetCenter(self):
"""Get the center of rotation."""
def SetTranslation(self, translation):
"""Set translation component."""
def GetInverse(self):
"""Get the inverse transform."""
class ScaleTransform:
"""
Scaling transform.
Template parameter:
- scalar_type: type
- dimension: int
"""
def SetScale(self, scale):
"""
Set the scale factors.
Parameters:
- scale: array-like - Scale factor for each dimension
"""class Rigid2DTransform:
"""
2D rigid transform (rotation + translation).
Template parameter:
- scalar_type: type (typically double)
"""
def SetAngle(self, angle):
"""
Set the rotation angle.
Parameters:
- angle: float - Rotation angle in radians
"""
def GetAngle(self):
"""Get the rotation angle."""
def SetTranslation(self, translation):
"""Set the translation vector."""
class Rigid3DTransform:
"""
3D rigid transform (rotation + translation).
Template parameter:
- scalar_type: type (typically double)
"""
class VersorTransform:
"""
3D rotation using versor (unit quaternion).
Template parameter:
- scalar_type: type (typically double)
"""
def SetRotation(self, versor):
"""
Set rotation using versor.
Parameters:
- versor: itk.Versor - Unit quaternion representing rotation
"""
def SetRotation(self, axis, angle):
"""
Set rotation using axis and angle.
Parameters:
- axis: array-like - Rotation axis (unit vector)
- angle: float - Rotation angle in radians
"""
class VersorRigid3DTransform:
"""
3D rigid transform using versor for rotation.
Template parameter:
- scalar_type: type (typically double)
"""
def SetRotation(self, versor):
"""Set rotation using versor."""
def SetTranslation(self, translation):
"""Set translation vector."""
def SetCenter(self, center):
"""Set center of rotation."""class Euler2DTransform:
"""
2D Euler angle rotation + translation.
Template parameter:
- scalar_type: type (typically double)
"""
def SetAngle(self, angle):
"""Set rotation angle in radians."""
def SetTranslation(self, translation):
"""Set translation vector."""
class Euler3DTransform:
"""
3D rotation using Euler angles + translation.
Template parameter:
- scalar_type: type (typically double)
"""
def SetRotation(self, angle_x, angle_y, angle_z):
"""
Set rotation using Euler angles.
Parameters:
- angle_x: float - Rotation around X axis (radians)
- angle_y: float - Rotation around Y axis (radians)
- angle_z: float - Rotation around Z axis (radians)
"""
def SetTranslation(self, translation):
"""Set translation vector."""
def SetCenter(self, center):
"""Set center of rotation."""class Similarity2DTransform:
"""
2D similarity transform (rotation + uniform scaling + translation).
Template parameter:
- scalar_type: type (typically double)
"""
def SetAngle(self, angle):
"""Set rotation angle."""
def SetScale(self, scale):
"""Set uniform scale factor."""
def SetTranslation(self, translation):
"""Set translation vector."""
class Similarity3DTransform:
"""
3D similarity transform.
Template parameter:
- scalar_type: type (typically double)
"""
def SetScale(self, scale):
"""Set uniform scale factor."""
def SetTranslation(self, translation):
"""Set translation vector."""class BSplineTransform:
"""
B-spline deformable transformation.
Template parameters:
- scalar_type: type (typically double)
- dimension: int
- spline_order: int (typically 3)
"""
def SetTransformDomainOrigin(self, origin):
"""Set the origin of the transform domain."""
def SetTransformDomainPhysicalDimensions(self, dimensions):
"""Set the physical dimensions of the transform domain."""
def SetTransformDomainMeshSize(self, mesh_size):
"""Set the mesh size (number of grid points - 1)."""
def SetTransformDomainDirection(self, direction):
"""Set the direction matrix."""
def SetParameters(self, parameters):
"""Set the B-spline coefficient parameters."""
def GetParameters(self):
"""Get the B-spline coefficient parameters."""
class DisplacementFieldTransform:
"""
Transform defined by a dense displacement field.
Template parameters:
- scalar_type: type (typically double)
- dimension: int
"""
def SetDisplacementField(self, field):
"""
Set the displacement field.
Parameters:
- field: itk.Image - Displacement field must be a vector image.
For 2D: itk.Image[itk.Vector[itk.D, 2], 2] or itk.VectorImage[itk.D, 2]
For 3D: itk.Image[itk.Vector[itk.D, 3], 3] or itk.VectorImage[itk.D, 3]
Note:
The displacement field image type must match the transform's template parameters.
Use itk.Image[itk.Vector[scalar_type, dimension], dimension] to create compatible
displacement fields.
"""
def GetDisplacementField(self):
"""Get the displacement field."""
def SetInverseDisplacementField(self, field):
"""Set the inverse displacement field."""class CompositeTransform:
"""
Composition of multiple transforms applied in sequence.
Template parameters:
- scalar_type: type (typically double)
- dimension: int
"""
def AddTransform(self, transform):
"""
Add a transform to the composition.
Parameters:
- transform: itk.Transform - Transform to add
"""
def GetNumberOfTransforms(self):
"""Get the number of transforms in the composition."""
def GetNthTransform(self, n):
"""
Get the nth transform.
Parameters:
- n: int - Index of transform
Returns:
itk.Transform
"""
def RemoveTransform(self):
"""Remove the last transform."""
def ClearTransformQueue(self):
"""Remove all transforms."""import itk
import numpy as np
# Create 2D translation
translation = itk.TranslationTransform[itk.D, 2].New()
translation.SetOffset([10.0, 20.0])
# Create 2D rotation
rotation = itk.Euler2DTransform[itk.D].New()
rotation.SetAngle(np.pi / 4) # 45 degrees
rotation.SetCenter([256, 256])
# Create affine transform
affine = itk.AffineTransform[itk.D, 2].New()
matrix = itk.matrix_from_array(np.array([[2.0, 0.0], [0.0, 1.0]]))
affine.SetMatrix(matrix)
affine.SetOffset([5.0, 10.0])
# Apply to image
image = itk.imread('input.png', itk.F)
transformed = itk.resample_image_filter(image, transform=affine)
itk.imwrite(transformed, 'transformed.png')import itk
# Create multiple transforms
translation = itk.TranslationTransform[itk.D, 2].New()
translation.SetOffset([10.0, 5.0])
rotation = itk.Euler2DTransform[itk.D].New()
rotation.SetAngle(0.785)
rotation.SetCenter([256, 256])
# Compose them
composite = itk.CompositeTransform[itk.D, 2].New()
composite.AddTransform(translation) # Applied first
composite.AddTransform(rotation) # Applied second
# Use composite transform
image = itk.imread('input.png', itk.F)
result = itk.resample_image_filter(image, transform=composite)
itk.imwrite(result, 'composite.png')import itk
image = itk.imread('input.png', itk.F)
# Create B-spline transform
bspline = itk.BSplineTransform[itk.D, 2, 3].New()
# Set transform domain to match image
bspline.SetTransformDomainOrigin(image.GetOrigin())
bspline.SetTransformDomainPhysicalDimensions([
image.GetSpacing()[i] * (image.GetLargestPossibleRegion().GetSize()[i] - 1)
for i in range(2)
])
bspline.SetTransformDomainMeshSize([10, 10]) # 10x10 grid
bspline.SetTransformDomainDirection(image.GetDirection())
# Initialize parameters (set to zero for identity)
num_params = bspline.GetNumberOfParameters()
params = itk.OptimizerParameters[itk.D](num_params)
params.Fill(0.0)
bspline.SetParameters(params)
# Use in registration or resampling
transformed = itk.resample_image_filter(image, transform=bspline)import itk
# Create a transform
transform = itk.Euler3DTransform[itk.D].New()
transform.SetRotation(0.1, 0.2, 0.3)
transform.SetTranslation([10.0, 20.0, 30.0])
# Write to file
itk.transformwrite(transform, 'my_transform.h5')
# Read from file
loaded_transforms = itk.transformread('my_transform.h5')
loaded_transform = loaded_transforms[0]
# Apply to image
image = itk.imread('input.nii.gz', itk.F)
transformed = itk.resample_image_filter(image, transform=loaded_transform)
itk.imwrite(transformed, 'transformed.nii.gz')Install with Tessl CLI
npx tessl i tessl/pypi-itk@5.4.1docs
guides
reference