SimpleITK is a simplified interface to the Insight Toolkit (ITK) for image registration and segmentation
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Transform classes in SimpleITK represent geometric transformations used for image registration, resampling, and spatial manipulation. The transform hierarchy includes rigid transforms, affine transforms, B-spline deformable transforms, and displacement fields.
Common interface for all geometric transformations.
class Transform:
"""
Base class for all geometric transformations.
Transforms map points from input space to output space and are used
for image registration and resampling operations.
"""
def GetDimension(self) -> int:
"""
Get spatial dimension of transform.
Returns:
Dimension (2 or 3)
"""
def GetParameters(self) -> tuple[float, ...]:
"""
Get transform parameters.
Parameters are transform-specific (e.g., angles, translations).
Returns:
Tuple of parameter values
"""
def SetParameters(self, parameters: tuple[float, ...]) -> None:
"""
Set transform parameters.
Args:
parameters: Parameter values to set
"""
def GetFixedParameters(self) -> tuple[float, ...]:
"""
Get fixed parameters.
Fixed parameters typically define the center of rotation or
other properties that don't change during optimization.
Returns:
Tuple of fixed parameter values
"""
def SetFixedParameters(self, parameters: tuple[float, ...]) -> None:
"""
Set fixed parameters.
Args:
parameters: Fixed parameter values
"""
def TransformPoint(self, point: tuple[float, ...]) -> tuple[float, ...]:
"""
Transform a point from input to output space.
Args:
point: Input point coordinates
Returns:
Transformed point coordinates
"""
def TransformVector(self, vector: tuple[float, ...], point: tuple[float, ...] = None) -> tuple[float, ...]:
"""
Transform a vector.
Args:
vector: Input vector
point: Point where vector is applied (for non-linear transforms)
Returns:
Transformed vector
"""
def GetInverse(self):
"""
Get inverse transform.
Not all transforms have analytical inverses.
Returns:
Inverse transform
Raises:
RuntimeError: If transform is not invertible
"""
def GetITKBase(self):
"""
Get underlying ITK transform object.
Returns:
ITK transform pointer
"""No transformation (identity mapping).
class IdentityTransform(Transform):
"""Identity transformation (no change)."""
def __init__(self, dimension: int):
"""
Create identity transform.
Args:
dimension: Spatial dimension (2 or 3)
"""Usage example:
import SimpleITK as sitk
# 3D identity transform
transform = sitk.IdentityTransform(3)
point = (10.0, 20.0, 30.0)
result = transform.TransformPoint(point) # Returns (10.0, 20.0, 30.0)Pure translation without rotation or scaling.
class TranslationTransform(Transform):
"""Pure translation transformation."""
def __init__(self, dimension: int):
"""
Create translation transform.
Args:
dimension: Spatial dimension (2 or 3)
"""
def SetOffset(self, offset: tuple[float, ...]) -> None:
"""
Set translation offset.
Args:
offset: Translation in each dimension
"""
def GetOffset(self) -> tuple[float, ...]:
"""
Get translation offset.
Returns:
Translation vector
"""Usage example:
import SimpleITK as sitk
# Create 3D translation
translation = sitk.TranslationTransform(3)
translation.SetOffset((5.0, -3.0, 10.0))
# Apply to point
point = (100.0, 50.0, 25.0)
transformed = translation.TransformPoint(point) # (105.0, 47.0, 35.0)Scaling transformation.
class ScaleTransform(Transform):
"""Scaling transformation."""
def __init__(self, dimension: int):
"""
Create scale transform.
Args:
dimension: Spatial dimension (2 or 3)
"""
def SetScale(self, scale: tuple[float, ...]) -> None:
"""
Set scale factors.
Args:
scale: Scale factor for each dimension
"""
def SetCenter(self, center: tuple[float, ...]) -> None:
"""
Set center of scaling.
Args:
center: Center point coordinates
"""2D rigid transformation using rotation angle and translation.
class Euler2DTransform(Transform):
"""2D rigid transform (rotation + translation)."""
def __init__(self):
"""Create 2D Euler transform."""
def SetAngle(self, angle: float) -> None:
"""
Set rotation angle.
Args:
angle: Rotation angle in radians
"""
def GetAngle(self) -> float:
"""
Get rotation angle.
Returns:
Angle in radians
"""
def SetTranslation(self, translation: tuple[float, float]) -> None:
"""
Set translation vector.
Args:
translation: Translation (tx, ty)
"""
def GetTranslation(self) -> tuple[float, float]:
"""
Get translation vector.
Returns:
Translation (tx, ty)
"""
def SetCenter(self, center: tuple[float, float]) -> None:
"""
Set center of rotation.
Args:
center: Center point (cx, cy)
"""
def GetCenter(self) -> tuple[float, float]:
"""
Get center of rotation.
Returns:
Center point
"""Usage example:
import SimpleITK as sitk
import math
# Create 2D rigid transform
rigid = sitk.Euler2DTransform()
rigid.SetCenter((128.0, 128.0)) # Rotate around image center
rigid.SetAngle(math.pi / 4) # 45 degrees
rigid.SetTranslation((10.0, -5.0))
# Apply to image
image = sitk.ReadImage('input.png')
resampled = sitk.Resample(image, rigid)3D rigid transformation using Euler angles and translation.
class Euler3DTransform(Transform):
"""3D rigid transform using Euler angles (rotation + translation)."""
def __init__(self):
"""Create 3D Euler transform."""
def SetRotation(self, angleX: float, angleY: float, angleZ: float) -> None:
"""
Set rotation using Euler angles.
Args:
angleX: Rotation around X axis (radians)
angleY: Rotation around Y axis (radians)
angleZ: Rotation around Z axis (radians)
"""
def SetTranslation(self, translation: tuple[float, float, float]) -> None:
"""
Set translation vector.
Args:
translation: Translation (tx, ty, tz)
"""
def SetCenter(self, center: tuple[float, float, float]) -> None:
"""
Set center of rotation.
Args:
center: Center point (cx, cy, cz)
"""
def GetMatrix(self) -> tuple[float, ...]:
"""
Get rotation matrix.
Returns:
3x3 rotation matrix (flattened, row-major)
"""Usage example:
import SimpleITK as sitk
# Create 3D rigid transform
rigid = sitk.Euler3DTransform()
rigid.SetCenter((64.0, 64.0, 32.0))
rigid.SetRotation(0.1, 0.2, 0.0) # Rotations in radians
rigid.SetTranslation((5.0, -3.0, 2.0))
# Use in registration
registration = sitk.ImageRegistrationMethod()
registration.SetInitialTransform(rigid)3D rotation using quaternion representation (no translation).
class VersorTransform(Transform):
"""3D rotation using quaternions."""
def __init__(self):
"""Create versor (quaternion rotation) transform."""
def SetRotation(self, axis: tuple[float, float, float], angle: float) -> None:
"""
Set rotation from axis and angle.
Args:
axis: Rotation axis (unit vector)
angle: Rotation angle in radians
"""
def SetCenter(self, center: tuple[float, float, float]) -> None:
"""
Set center of rotation.
Args:
center: Center point
"""3D rigid transform using quaternions (rotation + translation).
class VersorRigid3DTransform(Transform):
"""3D rigid transform using quaternions."""
def __init__(self):
"""Create versor rigid transform."""
def SetRotation(self, axis: tuple[float, float, float], angle: float) -> None:
"""
Set rotation from axis and angle.
Args:
axis: Rotation axis (unit vector)
angle: Rotation angle in radians
"""
def SetTranslation(self, translation: tuple[float, float, float]) -> None:
"""
Set translation vector.
Args:
translation: Translation vector
"""
def SetCenter(self, center: tuple[float, float, float]) -> None:
"""
Set center of rotation.
Args:
center: Center point
"""Similarity transformation (rotation + uniform scaling + translation).
class Similarity2DTransform(Transform):
"""2D similarity transform (rotation + uniform scale + translation)."""
def __init__(self):
"""Create 2D similarity transform."""
def SetScale(self, scale: float) -> None:
"""
Set uniform scale factor.
Args:
scale: Scale factor (applied uniformly)
"""
def SetAngle(self, angle: float) -> None:
"""
Set rotation angle.
Args:
angle: Rotation angle in radians
"""
def SetTranslation(self, translation: tuple[float, float]) -> None:
"""Set translation vector."""
def SetCenter(self, center: tuple[float, float]) -> None:
"""Set center of rotation and scaling."""
class Similarity3DTransform(Transform):
"""3D similarity transform (rotation + uniform scale + translation)."""
def __init__(self):
"""Create 3D similarity transform."""
def SetScale(self, scale: float) -> None:
"""Set uniform scale factor."""
def SetRotation(self, axis: tuple[float, float, float], angle: float) -> None:
"""Set rotation from axis and angle."""
def SetTranslation(self, translation: tuple[float, float, float]) -> None:
"""Set translation vector."""
def SetCenter(self, center: tuple[float, float, float]) -> None:
"""Set center of rotation and scaling."""General affine transformation (rotation, scaling, shearing, translation).
class AffineTransform(Transform):
"""General affine transformation."""
def __init__(self, dimension: int):
"""
Create affine transform.
Args:
dimension: Spatial dimension (2 or 3)
"""
def SetMatrix(self, matrix: tuple[float, ...]) -> None:
"""
Set linear transformation matrix.
For 2D: 2x2 matrix (4 values, row-major)
For 3D: 3x3 matrix (9 values, row-major)
Args:
matrix: Matrix elements in row-major order
"""
def GetMatrix(self) -> tuple[float, ...]:
"""
Get linear transformation matrix.
Returns:
Matrix elements in row-major order
"""
def SetTranslation(self, translation: tuple[float, ...]) -> None:
"""
Set translation vector.
Args:
translation: Translation in each dimension
"""
def GetTranslation(self) -> tuple[float, ...]:
"""
Get translation vector.
Returns:
Translation vector
"""
def SetCenter(self, center: tuple[float, ...]) -> None:
"""
Set center for matrix transformations.
Args:
center: Center point
"""
def GetCenter(self) -> tuple[float, ...]:
"""
Get center point.
Returns:
Center coordinates
"""Usage example:
import SimpleITK as sitk
# Create 3D affine transform
affine = sitk.AffineTransform(3)
# Set rotation matrix (90 degrees around Z axis)
affine.SetMatrix((
0, -1, 0,
1, 0, 0,
0, 0, 1
))
affine.SetTranslation((10.0, 20.0, 0.0))
affine.SetCenter((64.0, 64.0, 32.0))
# Apply to image
transformed = sitk.Resample(image, affine)B-spline based deformable transformation using control points.
class BSplineTransform(Transform):
"""B-spline deformable transformation."""
def __init__(self, dimension: int, order: int = 3):
"""
Create B-spline transform.
Args:
dimension: Spatial dimension (2 or 3)
order: B-spline order (typically 3 for cubic)
"""
def __init__(self, image, order: int = 3):
"""
Create B-spline transform from image defining domain.
Args:
image: Image defining transform domain
order: B-spline order
"""
def SetTransformDomainOrigin(self, origin: tuple[float, ...]) -> None:
"""
Set origin of transform domain.
Args:
origin: Domain origin in physical space
"""
def SetTransformDomainPhysicalDimensions(self, dimensions: tuple[float, ...]) -> None:
"""
Set physical dimensions of transform domain.
Args:
dimensions: Physical size in each dimension
"""
def SetTransformDomainMeshSize(self, meshSize: tuple[int, ...]) -> None:
"""
Set control point grid dimensions.
Args:
meshSize: Number of mesh elements in each dimension
"""
def SetTransformDomainDirection(self, direction: tuple[float, ...]) -> None:
"""
Set transform domain direction.
Args:
direction: Direction cosine matrix (flattened)
"""
def GetTransformDomainOrigin(self) -> tuple[float, ...]:
"""Get transform domain origin."""
def GetTransformDomainPhysicalDimensions(self) -> tuple[float, ...]:
"""Get transform domain physical dimensions."""
def GetTransformDomainMeshSize(self) -> tuple[int, ...]:
"""Get control point mesh size."""
def GetCoefficientImages(self) -> list:
"""
Get coefficient images (one per dimension).
Returns:
List of coefficient images
"""Usage example:
import SimpleITK as sitk
# Create B-spline transform for image
fixed_image = sitk.ReadImage('fixed.nii')
transform_domain = fixed_image
bspline = sitk.BSplineTransform(transform_domain, 3)
bspline.SetTransformDomainMeshSize((8, 8, 8))
# Initialize with no deformation (identity)
params = [0.0] * bspline.GetNumberOfParameters()
bspline.SetParameters(params)
# Use in registration
registration = sitk.ImageRegistrationMethod()
registration.SetInitialTransform(bspline, inPlace=True)Dense displacement field transformation.
class DisplacementFieldTransform(Transform):
"""Dense displacement field transformation."""
def __init__(self, dimension: int):
"""
Create displacement field transform.
Args:
dimension: Spatial dimension (2 or 3)
"""
def SetDisplacementField(self, field) -> None:
"""
Set displacement field image.
Args:
field: Vector image with displacement at each pixel
"""
def GetDisplacementField(self):
"""
Get displacement field image.
Returns:
Vector image with displacements
"""
def SetSmoothingGaussianOnUpdate(self, sigma: float) -> None:
"""
Enable Gaussian smoothing after updates.
Args:
sigma: Smoothing standard deviation
"""Usage example:
import SimpleITK as sitk
# Create from demons registration output
demons = sitk.DemonsRegistrationFilter()
demons.SetNumberOfIterations(50)
displacement = demons.Execute(fixed, moving)
# Create transform from displacement field
transform = sitk.DisplacementFieldTransform(3)
transform.SetDisplacementField(displacement)
# Warp image
warped = sitk.Resample(moving, fixed, transform)Composition of multiple transforms applied in sequence.
class CompositeTransform(Transform):
"""Composition of multiple transforms."""
def __init__(self, dimension: int):
"""
Create composite transform.
Args:
dimension: Spatial dimension (2 or 3)
"""
def AddTransform(self, transform) -> None:
"""
Add transform to composition.
Transforms are applied in the order added (first added is applied first).
Args:
transform: Transform to add
"""
def RemoveTransform(self) -> None:
"""Remove last transform from composition."""
def GetNthTransform(self, n: int):
"""
Get transform at index.
Args:
n: Transform index (0-based)
Returns:
Transform at index
"""
def GetNumberOfTransforms(self) -> int:
"""
Get number of transforms in composition.
Returns:
Number of transforms
"""
def FlattenTransform(self):
"""
Flatten composite into single equivalent transform where possible.
Returns:
Flattened transform
"""Usage example:
import SimpleITK as sitk
# Create composite of multiple transforms
composite = sitk.CompositeTransform(3)
# Add translation
translation = sitk.TranslationTransform(3)
translation.SetOffset((10.0, 5.0, 0.0))
composite.AddTransform(translation)
# Add rotation
rotation = sitk.Euler3DTransform()
rotation.SetCenter((64.0, 64.0, 32.0))
rotation.SetRotation(0.0, 0.0, 0.1)
composite.AddTransform(rotation)
# Transforms are applied in order: first translation, then rotation
result = composite.TransformPoint((50.0, 50.0, 32.0))3D rigid transformation (rotation using versor) plus isotropic scaling.
class ScaleVersor3DTransform(Transform):
"""3D rigid transform with isotropic scaling using quaternion rotation."""
def __init__(self):
"""Create scale-versor 3D transform."""
def SetRotation(self, axis: tuple[float, float, float], angle: float) -> None:
"""
Set rotation using axis-angle representation.
Args:
axis: Rotation axis (normalized)
angle: Rotation angle in radians
"""
def SetScale(self, scale: float) -> None:
"""
Set isotropic scale factor.
Args:
scale: Scale factor (1.0 = no scaling)
"""
def SetTranslation(self, translation: tuple[float, float, float]) -> None:
"""
Set translation vector.
Args:
translation: Translation in x, y, z
"""
def SetCenter(self, center: tuple[float, float, float]) -> None:
"""
Set center of rotation and scaling.
Args:
center: Center point coordinates
"""3D rigid transformation plus anisotropic scaling and skewing.
class ScaleSkewVersor3DTransform(Transform):
"""3D rigid transform with anisotropic scaling and skewing."""
def __init__(self):
"""Create scale-skew-versor 3D transform."""
def SetRotation(self, axis: tuple[float, float, float], angle: float) -> None:
"""Set rotation using axis-angle representation."""
def SetScale(self, scale: tuple[float, float, float]) -> None:
"""
Set anisotropic scale factors.
Args:
scale: Scale factors for each axis
"""
def SetSkew(self, skew: tuple[float, ...]) -> None:
"""
Set skew parameters.
Args:
skew: Skew parameters (6 values for 3D)
"""
def SetTranslation(self, translation: tuple[float, float, float]) -> None:
"""Set translation vector."""
def SetCenter(self, center: tuple[float, float, float]) -> None:
"""Set center of rotation, scaling, and skewing."""Complete 3D similarity transform composed of center, translation, rotation, scaling, and skewing.
class ComposeScaleSkewVersor3DTransform(Transform):
"""
3D transform composing rotation, scaling, skewing, and translation.
Provides the most general rigid-body-like transformation with
full control over rotation (versor), anisotropic scaling, skewing,
center of transformation, and translation.
"""
def __init__(self):
"""Create compose-scale-skew-versor 3D transform."""
def SetRotation(self, axis: tuple[float, float, float], angle: float) -> None:
"""Set rotation using axis-angle representation."""
def SetScale(self, scale: tuple[float, float, float]) -> None:
"""Set anisotropic scale factors."""
def SetSkew(self, skew: tuple[float, ...]) -> None:
"""Set skew parameters."""
def SetTranslation(self, translation: tuple[float, float, float]) -> None:
"""Set translation vector."""
def SetCenter(self, center: tuple[float, float, float]) -> None:
"""Set center point."""def ReadTransform(fileName: str):
"""
Read transform from file.
Supports ITK transform file format (.tfm, .txt).
Args:
fileName: Path to transform file
Returns:
Transform object
"""
def WriteTransform(transform, fileName: str) -> None:
"""
Write transform to file.
Args:
transform: Transform to write
fileName: Output file path
"""Usage example:
import SimpleITK as sitk
# Save transform
transform = sitk.Euler3DTransform()
transform.SetRotation(0.1, 0.2, 0.0)
sitk.WriteTransform(transform, 'transform.tfm')
# Load transform
loaded = sitk.ReadTransform('transform.tfm')import SimpleITK as sitk
# Compute image center
image = sitk.ReadImage('input.nii')
size = image.GetSize()
spacing = image.GetSpacing()
origin = image.GetOrigin()
center = [origin[i] + (size[i] - 1) * spacing[i] / 2.0 for i in range(3)]
# Center the transform
transform = sitk.Euler3DTransform()
transform.SetCenter(center)import SimpleITK as sitk
# Method 1: Use CompositeTransform
composite = sitk.CompositeTransform(3)
composite.AddTransform(transform1)
composite.AddTransform(transform2)
# Method 2: Get result from registration
registration = sitk.ImageRegistrationMethod()
# ... configure registration ...
final_transform = registration.Execute(fixed, moving)
# final_transform is composite of initial + optimizedimport SimpleITK as sitk
# Get inverse (if available)
forward = sitk.AffineTransform(3)
# ... configure forward transform ...
inverse = forward.GetInverse()
# Verify inversion
point = (10.0, 20.0, 30.0)
transformed = forward.TransformPoint(point)
recovered = inverse.TransformPoint(transformed)
# recovered should be close to original pointInstall with Tessl CLI
npx tessl i tessl/pypi-simpleitk