CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-itk

Insight Toolkit for N-dimensional image processing, segmentation, and registration in medical and scientific applications

Pending
Overview
Eval results
Files

resampling.mddocs/reference/filtering/

Resampling and Warping

Resample images with transforms, change resolution and spacing, interpolate, and apply spatial warping.

Capabilities

Resampling with Transforms

def resample_image_filter(input, transform=None, interpolator=None, size=None, output_spacing=None, output_origin=None, output_direction=None, default_pixel_value=0):
    """
    Resample image with transform and interpolation.
    
    Parameters:
    - input: itk.Image - Input image
    - transform: itk.Transform, optional - Geometric transform to apply
    - interpolator: Interpolator, optional - Interpolation method
    - size: itk.Size, optional - Output image size
    - output_spacing: tuple, optional - Output pixel spacing
    - output_origin: tuple, optional - Output origin
    - output_direction: itk.Matrix, optional - Output direction matrix
    - default_pixel_value: pixel_type - Value for pixels outside input domain
    
    Returns:
    itk.Image - Resampled image
    """

class ResampleImageFilter:
    """
    Resample an image via a coordinate transform.
    
    Template parameters:
    - input_image_type: Input image type
    - output_image_type: Output image type
    """
    
    def SetTransform(self, transform):
        """Set the coordinate transformation."""
    
    def SetInterpolator(self, interpolator):
        """Set the interpolator function."""
    
    def SetSize(self, size):
        """Set the output image size."""
    
    def SetOutputSpacing(self, spacing):
        """Set the output pixel spacing."""
    
    def SetOutputOrigin(self, origin):
        """Set the output origin."""
    
    def SetOutputDirection(self, direction):
        """Set the output direction matrix."""
    
    def SetDefaultPixelValue(self, value):
        """Set the pixel value for regions outside input."""

Displacement Field Warping

def warp_image_filter(input, displacement_field, interpolator=None, output_spacing=None, output_origin=None, output_direction=None, default_pixel_value=0):
    """
    Warp image using displacement field.
    
    Parameters:
    - input: itk.Image - Input image to warp
    - displacement_field: itk.Image - Displacement field (VectorImage)
    - interpolator: Interpolator, optional - Interpolation method
    - output_spacing: tuple, optional - Output pixel spacing
    - output_origin: tuple, optional - Output origin
    - output_direction: itk.Matrix, optional - Output direction matrix
    - default_pixel_value: pixel_type - Value for pixels outside input domain
    
    Returns:
    itk.Image - Warped image
    """

class WarpImageFilter:
    """
    Warp an image using a displacement field.
    
    Template parameters:
    - input_image_type: Input image type
    - output_image_type: Output image type
    - displacement_field_type: Displacement field type
    """
    
    def SetDisplacementField(self, field):
        """Set the displacement field."""
    
    def SetInterpolator(self, interpolator):
        """Set the interpolator function."""
    
    def SetOutputSpacing(self, spacing):
        """Set the output pixel spacing."""
    
    def SetOutputOrigin(self, origin):
        """Set the output origin."""
    
    def SetEdgePaddingValue(self, value):
        """Set padding value for edge pixels."""

Changing Image Information

def change_information_image_filter(input, output_spacing=None, output_origin=None, output_direction=None, center_image=False):
    """
    Change image metadata without modifying pixel data.
    
    Parameters:
    - input: itk.Image - Input image
    - output_spacing: tuple, optional - New pixel spacing
    - output_origin: tuple, optional - New origin
    - output_direction: itk.Matrix, optional - New direction matrix
    - center_image: bool - Center the image
    
    Returns:
    itk.Image - Image with modified metadata
    """

class ChangeInformationImageFilter:
    """
    Change image metadata (spacing, origin, direction) without copying pixel data.
    
    Template parameter:
    - image_type: Image type
    """
    
    def SetOutputSpacing(self, spacing):
        """Set the output spacing."""
    
    def SetOutputOrigin(self, origin):
        """Set the output origin."""
    
    def SetOutputDirection(self, direction):
        """Set the output direction matrix."""
    
    def SetCenterImage(self, center):
        """Center the image."""
    
    def ChangeSpacingOn(self):
        """Enable changing spacing."""
    
    def ChangeOriginOn(self):
        """Enable changing origin."""
    
    def ChangeDirectionOn(self):
        """Enable changing direction."""

Shrinking and Expanding

def shrink_image_filter(input, shrink_factors):
    """
    Shrink image by integer factors.
    
    Parameters:
    - input: itk.Image - Input image
    - shrink_factors: int or list - Shrink factor(s) for each dimension
    
    Returns:
    itk.Image - Shrunk image
    """

def expand_image_filter(input, expand_factors):
    """
    Expand image by integer factors using interpolation.
    
    Parameters:
    - input: itk.Image - Input image
    - expand_factors: int or list - Expand factor(s) for each dimension
    
    Returns:
    itk.Image - Expanded image
    """

Usage Examples

Basic Resampling

import itk

image = itk.imread('input.png', itk.F)

# Resample to different size
new_size = [512, 512]
resampled = itk.resample_image_filter(
    image,
    size=new_size,
    output_spacing=[0.5, 0.5]
)
itk.imwrite(resampled, 'resampled.png')

Resampling with Transform

import itk

image = itk.imread('input.png', itk.F)

# Create rotation transform
transform = itk.Euler2DTransform[itk.D].New()
transform.SetAngle(0.785)  # 45 degrees in radians

# Resample with rotation
ImageType = itk.Image[itk.F, 2]
resampler = itk.ResampleImageFilter[ImageType, ImageType].New()
resampler.SetInput(image)
resampler.SetTransform(transform)
resampler.SetSize(image.GetLargestPossibleRegion().GetSize())
resampler.SetOutputSpacing(image.GetSpacing())
resampler.SetOutputOrigin(image.GetOrigin())
resampler.Update()

rotated = resampler.GetOutput()
itk.imwrite(rotated, 'rotated.png')

Warping with Displacement Field

import itk
import numpy as np

image = itk.imread('input.png', itk.F)

# Create synthetic displacement field
size = itk.size(image)
displacement_array = np.zeros((*size[::-1], 2), dtype=np.float32)
# Add some displacement
displacement_array[:, :, 0] = 10.0  # x displacement
displacement_array[:, :, 1] = 5.0   # y displacement

displacement_field = itk.image_from_array(displacement_array, is_vector=True)
displacement_field.CopyInformation(image)

# Warp image
warped = itk.warp_image_filter(image, displacement_field)
itk.imwrite(warped, 'warped.png')

Changing Image Spacing

import itk

image = itk.imread('input.nii.gz', itk.F)

# Change spacing to isotropic
new_spacing = [1.0, 1.0, 1.0]

# Method 1: Just change metadata (no resampling)
info_changed = itk.change_information_image_filter(
    image,
    output_spacing=new_spacing
)

# Method 2: Actually resample to new spacing
original_size = itk.size(image)
original_spacing = itk.spacing(image)

new_size = [
    int(original_size[i] * original_spacing[i] / new_spacing[i])
    for i in range(3)
]

resampled = itk.resample_image_filter(
    image,
    size=new_size,
    output_spacing=new_spacing
)
itk.imwrite(resampled, 'isotropic.nii.gz')

Shrinking and Expanding

import itk

image = itk.imread('input.png', itk.F)

# Shrink by factor of 2 in each dimension
shrunk = itk.shrink_image_filter(image, shrink_factors=2)
itk.imwrite(shrunk, 'shrunk.png')

# Expand by factor of 2
expanded = itk.expand_image_filter(shrunk, expand_factors=2)
itk.imwrite(expanded, 'expanded.png')

Install with Tessl CLI

npx tessl i tessl/pypi-itk

docs

index.md

tile.json