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

type-system.mddocs/reference/

Type System

ITK's template-based type system with type codes for pixels, images, and explicit template instantiation.

Capabilities

Primitive Type Constants

# Floating Point Types
F: itkCType  # float (32-bit) - equivalent to np.float32
D: itkCType  # double (64-bit) - equivalent to np.float64
LD: itkCType  # long double (platform-dependent, typically 80 or 128-bit)

# Unsigned Integer Types
UC: itkCType  # unsigned char (8-bit) - equivalent to np.uint8
US: itkCType  # unsigned short (16-bit) - equivalent to np.uint16
UI: itkCType  # unsigned int (32-bit) - equivalent to np.uint32
UL: itkCType  # unsigned long (32 or 64-bit, platform-dependent)
ULL: itkCType  # unsigned long long (64-bit) - equivalent to np.uint64

# Signed Integer Types
SC: itkCType  # signed char (8-bit) - equivalent to np.int8
SS: itkCType  # signed short (16-bit) - equivalent to np.int16
SI: itkCType  # signed int (32-bit) - equivalent to np.int32
SL: itkCType  # signed long (32 or 64-bit, platform-dependent)
SLL: itkCType  # signed long long (64-bit) - equivalent to np.int64

# Boolean Type
B: itkCType  # bool - equivalent to np.bool_

# Type Aliases
ST: itkCType  # SizeValueType (UL or ULL on Windows)
IT: itkCType  # IdentifierType (UL or ULL on Windows)
OT: itkCType  # OffsetType (SL or SLL on Windows)

Template Instantiation

# Image types - Image[pixel_type, dimension]
ImageTypeF2D = Image[F, 2]  # 2D float image
ImageTypeUC3D = Image[UC, 3]  # 3D unsigned char image
ImageTypeD4D = Image[D, 4]  # 4D double image

# VectorImage types - VectorImage[component_type, dimension]
VectorImageTypeF2D = VectorImage[F, 2]  # 2D image with float vectors
VectorImageTypeD3D = VectorImage[D, 3]  # 3D image with double vectors

# Vector pixel types - Vector[component_type, dimension]
VectorTypeF3 = Vector[F, 3]  # 3D float vector
VectorTypeD2 = Vector[D, 2]  # 2D double vector

# RGB pixel types - RGBPixel[component_type]
RGBPixelTypeUC = RGBPixel[UC]  # 8-bit RGB
RGBPixelTypeUS = RGBPixel[US]  # 16-bit RGB

# RGBA pixel types - RGBAPixel[component_type]
RGBAPixelTypeUC = RGBAPixel[UC]  # 8-bit RGBA

# Filter types
MedianFilterType = MedianImageFilter[ImageTypeF2D, ImageTypeF2D]
GaussianFilterType = DiscreteGaussianImageFilter[ImageTypeUC3D, ImageTypeF3D]

# Transform types - Transform[scalar_type, dimension]
AffineTransformType = AffineTransform[D, 3]  # 3D double affine
TranslationTransformType = TranslationTransform[D, 2]  # 2D double translation

Type Introspection Functions

def template(cl):
    """
    Return the template information for a class or object.
    
    Parameters:
    - cl: class or object - ITK class or instance
    
    Returns:
    tuple - (itkTemplate, template_parameters)
    """

def class_(obj):
    """
    Return a class from an object (handles ITK-specific class retrieval).
    
    Parameters:
    - obj: object - ITK object instance
    
    Returns:
    type - Class type
    """

def ctype(s):
    """
    Return the itkCType corresponding to a C type string.
    
    Parameters:
    - s: str - C type name (e.g., "float", "unsigned char")
    
    Returns:
    itkCType instance
    """

def python_type(object_ref):
    """
    Return the Python type name of an ITK object.
    
    Parameters:
    - object_ref: object - ITK object
    
    Returns:
    str - String representation (e.g., "itk.Image[itk.F,3]")
    """

Type Conversion Helpers

def down_cast(obj):
    """
    Down-cast an itk.LightObject to its most specialized type.
    
    Parameters:
    - obj: itk.LightObject - Base ITK object
    
    Returns:
    object - Most specialized ITK object type
    """

Usage Examples

Using Type Constants

import itk
from itk import F, D, UC, US, UI

# Create images with different pixel types
image_float = itk.imread('input.png', F)  # Float pixels
image_uchar = itk.imread('input.png', UC)  # Unsigned char pixels
image_ushort = itk.imread('input.png', US)  # Unsigned short pixels

print(f"Float image type: {itk.python_type(image_float)}")
print(f"UChar image type: {itk.python_type(image_uchar)}")

Explicit Template Instantiation

import itk

# Define image types explicitly
InputImageType = itk.Image[itk.F, 2]
OutputImageType = itk.Image[itk.F, 2]

# Create filter with explicit types
FilterType = itk.MedianImageFilter[InputImageType, OutputImageType]
filter = FilterType.New()

# Set input
image = itk.imread('input.png', itk.F)
filter.SetInput(image)
filter.SetRadius(3)

# Execute
filter.Update()
result = filter.GetOutput()

Automatic Type Deduction

import itk

# ITK can deduce types automatically
image = itk.imread('input.png', itk.F)

# Type is deduced from input
filter = itk.MedianImageFilter.New(image)
filter.SetRadius(3)
filter.Update()
result = filter.GetOutput()

Working with Vector Images

import itk
import numpy as np

# Create vector image
array_rgb = np.random.randint(0, 255, size=(100, 100, 3), dtype=np.uint8)
rgb_image = itk.image_from_array(array_rgb, is_vector=True)

# The type is VectorImage[UC, 2] with 3 components per pixel
print(f"Image type: {itk.python_type(rgb_image)}")
print(f"Components per pixel: {rgb_image.GetNumberOfComponentsPerPixel()}")

Type Conversion Between Images

import itk

# Load as unsigned char
image_uc = itk.imread('input.png', itk.UC)

# Convert to float
InputType = itk.Image[itk.UC, 2]
OutputType = itk.Image[itk.F, 2]
caster = itk.CastImageFilter[InputType, OutputType].New()
caster.SetInput(image_uc)
caster.Update()
image_float = caster.GetOutput()

# Or use convenience function
image_float = itk.cast_image_filter(image_uc, ttype=(itk.Image[itk.F, 2],))

Template Introspection

import itk

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

# Get template information
template_info = itk.template(image)
print(f"Template: {template_info}")

# Get Python type string
type_str = itk.python_type(image)
print(f"Type: {type_str}")

# Get class
image_class = itk.class_(image)
print(f"Class: {image_class}")

Checking Available Dimensions

import itk

# Check available dimensions (typically [2, 3])
print(f"Available dimensions: {itk.DIMS}")

# Create images in available dimensions
for dim in itk.DIMS:
    ImageType = itk.Image[itk.F, dim]
    print(f"{dim}D Image type: {ImageType}")

Complex Pixel Types

import itk

# Symmetric tensor
TensorType = itk.SymmetricSecondRankTensor[itk.F, 3]
TensorImageType = itk.Image[TensorType, 3]

# Diffusion tensor
DTIType = itk.DiffusionTensor3D[itk.F]
DTIImageType = itk.Image[DTIType, 3]

# Complex numbers
ComplexType = itk.complex[itk.F]
ComplexImageType = itk.Image[ComplexType, 2]

Type Matching with NumPy

import itk
import numpy as np

# NumPy dtype to ITK type mapping
dtype_map = {
    np.float32: itk.F,
    np.float64: itk.D,
    np.uint8: itk.UC,
    np.uint16: itk.US,
    np.uint32: itk.UI,
    np.int8: itk.SC,
    np.int16: itk.SS,
    np.int32: itk.SI,
}

# Create NumPy array
array = np.random.rand(100, 100).astype(np.float32)

# Convert to ITK with matching type
itk_type = dtype_map[array.dtype.type]
image = itk.image_from_array(array)

print(f"NumPy dtype: {array.dtype}")
print(f"ITK pixel type: {itk_type}")
print(f"Image type: {itk.python_type(image)}")

Install with Tessl CLI

npx tessl i tessl/pypi-itk

docs

index.md

tile.json