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
Core image data structure and fundamental operations for creating, manipulating, and accessing image data. The Image class is the central data structure in SimpleITK, supporting 2D, 3D, and 4D images with various pixel types and comprehensive metadata handling.
Create images from scratch or from existing data with support for multiple pixel types and dimensions.
class Image:
def __init__(self, width: int, height: int, pixelType: int):
"""Create 2D image with specified dimensions and pixel type"""
def __init__(self, width: int, height: int, depth: int, pixelType: int):
"""Create 3D image with specified dimensions and pixel type"""
def __init__(self, size: tuple, pixelType: int):
"""Create image with size tuple and pixel type"""
def __init__(self, size: tuple, pixelType: int, numberOfComponents: int):
"""Create vector image with multiple components per pixel"""
# Factory functions
def Image(width: int, height: int, pixelType: int) -> Image:
"""Create 2D image"""
def Image(width: int, height: int, depth: int, pixelType: int) -> Image:
"""Create 3D image"""Access and modify image metadata including dimensions, spacing, origin, and direction.
class Image:
def GetSize(self) -> tuple:
"""Get image dimensions as tuple (width, height, [depth])"""
def GetWidth(self) -> int:
"""Get image width"""
def GetHeight(self) -> int:
"""Get image height"""
def GetDepth(self) -> int:
"""Get image depth (1 for 2D images)"""
def GetDimension(self) -> int:
"""Get number of spatial dimensions (2, 3, or 4)"""
def GetNumberOfPixels(self) -> int:
"""Get total number of pixels"""
def GetSpacing(self) -> tuple:
"""Get pixel spacing in physical units"""
def SetSpacing(self, spacing: tuple):
"""Set pixel spacing"""
def GetOrigin(self) -> tuple:
"""Get image origin in physical coordinates"""
def SetOrigin(self, origin: tuple):
"""Set image origin"""
def GetDirection(self) -> tuple:
"""Get direction matrix as flat tuple"""
def SetDirection(self, direction: tuple):
"""Set direction matrix from flat tuple"""
def GetPixelID(self) -> int:
"""Get pixel type identifier"""
def GetPixelIDValue(self) -> int:
"""Get pixel type value"""
def GetPixelIDTypeAsString(self) -> str:
"""Get pixel type as string"""
def GetNumberOfComponentsPerPixel(self) -> int:
"""Get number of components per pixel (1 for scalar, >1 for vector)"""Direct pixel-level access for reading and writing individual pixel values.
class Image:
def GetPixel(self, idx: tuple) -> float:
"""
Get pixel value at index.
Args:
idx: Pixel index as tuple (x, y, [z])
Returns:
Pixel value as float (scalar images) or tuple (vector images)
"""
def SetPixel(self, idx: tuple, value: float):
"""
Set pixel value at index.
Args:
idx: Pixel index as tuple (x, y, [z])
value: New pixel value
"""
def GetPixelAsInt8(self, idx: tuple) -> int: ...
def GetPixelAsUInt8(self, idx: tuple) -> int: ...
def GetPixelAsInt16(self, idx: tuple) -> int: ...
def GetPixelAsUInt16(self, idx: tuple) -> int: ...
def GetPixelAsInt32(self, idx: tuple) -> int: ...
def GetPixelAsUInt32(self, idx: tuple) -> int: ...
def GetPixelAsFloat(self, idx: tuple) -> float: ...
def GetPixelAsDouble(self, idx: tuple) -> float: ...
def SetPixelAsInt8(self, idx: tuple, value: int): ...
def SetPixelAsUInt8(self, idx: tuple, value: int): ...
def SetPixelAsInt16(self, idx: tuple, value: int): ...
def SetPixelAsUInt16(self, idx: tuple, value: int): ...
def SetPixelAsInt32(self, idx: tuple, value: int): ...
def SetPixelAsUInt32(self, idx: tuple, value: int): ...
def SetPixelAsFloat(self, idx: tuple, value: float): ...
def SetPixelAsDouble(self, idx: tuple, value: float): ...Access to image data as contiguous memory buffers for efficient processing.
class Image:
def GetBufferAsInt8(self) -> buffer:
"""Get image buffer as 8-bit signed integers"""
def GetBufferAsUInt8(self) -> buffer:
"""Get image buffer as 8-bit unsigned integers"""
def GetBufferAsInt16(self) -> buffer:
"""Get image buffer as 16-bit signed integers"""
def GetBufferAsUInt16(self) -> buffer:
"""Get image buffer as 16-bit unsigned integers"""
def GetBufferAsInt32(self) -> buffer:
"""Get image buffer as 32-bit signed integers"""
def GetBufferAsUInt32(self) -> buffer:
"""Get image buffer as 32-bit unsigned integers"""
def GetBufferAsInt64(self) -> buffer:
"""Get image buffer as 64-bit signed integers"""
def GetBufferAsUInt64(self) -> buffer:
"""Get image buffer as 64-bit unsigned integers"""
def GetBufferAsFloat(self) -> buffer:
"""Get image buffer as 32-bit floats"""
def GetBufferAsDouble(self) -> buffer:
"""Get image buffer as 64-bit floats"""Copy metadata between images and access detailed image information.
class Image:
def CopyInformation(self, sourceImage: Image):
"""
Copy spacing, origin, direction, and metadata from source image.
Args:
sourceImage: Image to copy information from
"""
def GetMetaData(self, key: str) -> str:
"""
Get metadata value for key.
Args:
key: Metadata key string
Returns:
Metadata value as string
"""
def SetMetaData(self, key: str, value: str):
"""
Set metadata key-value pair.
Args:
key: Metadata key string
value: Metadata value string
"""
def HasMetaDataKey(self, key: str) -> bool:
"""Check if metadata key exists"""
def GetMetaDataKeys(self) -> list:
"""Get list of all metadata keys"""
def EraseMetaData(self, key: str):
"""Remove metadata key"""Efficient conversion between SimpleITK images and NumPy arrays with proper handling of metadata and memory layout.
def GetArrayFromImage(image: Image) -> numpy.ndarray:
"""
Convert SimpleITK image to NumPy array.
Note: Array axes are in reverse order compared to SimpleITK
(z,y,x for 3D images vs x,y,z in SimpleITK)
Args:
image: SimpleITK Image
Returns:
NumPy array with image data
"""
def GetImageFromArray(array: numpy.ndarray, isVector: bool = False) -> Image:
"""
Convert NumPy array to SimpleITK image.
Args:
array: NumPy array with image data
isVector: True if array represents vector image
Returns:
SimpleITK Image
"""
def GetArrayViewFromImage(image: Image) -> numpy.ndarray:
"""
Get NumPy array view of image data (shares memory).
Args:
image: SimpleITK Image
Returns:
NumPy array view of image data
"""
def GetImageViewFromArray(array: numpy.ndarray, isVector: bool = False) -> Image:
"""
Create SimpleITK image view of NumPy array (shares memory).
Args:
array: NumPy array with image data
isVector: True if array represents vector image
Returns:
SimpleITK Image view
"""import SimpleITK as sitk
import numpy as np
# Create a 256x256 8-bit image
image = sitk.Image(256, 256, sitk.sitkUInt8)
# Set physical properties
image.SetSpacing([0.5, 0.5]) # 0.5mm pixel spacing
image.SetOrigin([0.0, 0.0]) # Origin at (0,0)
# Set some pixel values
image.SetPixel([128, 128], 255) # Set center pixel to white
# Get image properties
print(f"Size: {image.GetSize()}")
print(f"Spacing: {image.GetSpacing()}")
print(f"Pixel type: {image.GetPixelIDTypeAsString()}")
# Access pixel values
center_value = image.GetPixel([128, 128])
print(f"Center pixel value: {center_value}")import SimpleITK as sitk
import numpy as np
# Convert NumPy array to SimpleITK image
array = np.random.rand(100, 100).astype(np.float32)
image = sitk.GetImageFromArray(array)
# Set physical properties
image.SetSpacing([1.0, 1.0])
image.SetOrigin([0.0, 0.0])
# Process with SimpleITK
smoothed = sitk.SmoothingRecursiveGaussian(image, 2.0)
# Convert back to NumPy
result_array = sitk.GetArrayFromImage(smoothed)
print(f"Original array shape: {array.shape}")
print(f"Result array shape: {result_array.shape}")import SimpleITK as sitk
# Read image with metadata
image = sitk.ReadImage('medical_image.dcm')
# Access DICOM metadata
if image.HasMetaDataKey('0010|0010'): # Patient Name
patient_name = image.GetMetaData('0010|0010')
print(f"Patient: {patient_name}")
# Add custom metadata
image.SetMetaData('ProcessingDate', '2023-09-10')
image.SetMetaData('Algorithm', 'CustomProcessing')
# List all metadata keys
keys = image.GetMetaDataKeys()
print(f"Metadata keys: {len(keys)} entries")Base class for all image processing filters and operations in SimpleITK.
class ProcessObject:
def __init__(self):
"""Initialize process object"""
def Execute(self) -> Image:
"""Execute the filter/process and return result"""
def SetGlobalDefaultDebug(self, debugFlag: bool):
"""Set global debug flag for all process objects"""
def GetGlobalDefaultDebug(self) -> bool:
"""Get global debug flag"""
def SetGlobalDefaultNumberOfThreads(self, n: int):
"""Set global default number of threads for processing"""
def GetGlobalDefaultNumberOfThreads(self) -> int:
"""Get global default number of threads"""
def GetName(self) -> str:
"""Get name of the process object"""
def SetDebug(self, debugFlag: bool):
"""Enable/disable debug output for this object"""
def GetDebug(self) -> bool:
"""Get debug flag for this object"""
def SetNumberOfThreads(self, n: int):
"""Set number of threads for this object"""
def GetNumberOfThreads(self) -> int:
"""Get number of threads for this object"""Source classes for generating synthetic images and test patterns.
class ImageSource(ProcessObject):
def __init__(self):
"""Base class for image source objects"""
def SetSize(self, size: tuple):
"""Set output image size"""
def GetSize(self) -> tuple:
"""Get output image size"""
def SetSpacing(self, spacing: tuple):
"""Set output image spacing"""
def GetSpacing(self) -> tuple:
"""Get output image spacing"""
def SetOrigin(self, origin: tuple):
"""Set output image origin"""
def GetOrigin(self) -> tuple:
"""Get output image origin"""
def SetDirection(self, direction: tuple):
"""Set output image direction matrix"""
def GetDirection(self) -> tuple:
"""Get output image direction matrix"""
class GaussianSource(ImageSource):
def __init__(self):
"""Generate Gaussian blob images"""
def SetSigma(self, sigma: tuple):
"""Set Gaussian standard deviation for each dimension"""
def GetSigma(self) -> tuple:
"""Get Gaussian standard deviation"""
def SetMean(self, mean: tuple):
"""Set Gaussian mean (center) for each dimension"""
def GetMean(self) -> tuple:
"""Get Gaussian mean"""
def SetScale(self, scale: float):
"""Set amplitude scaling factor"""
def GetScale(self) -> float:
"""Get amplitude scaling factor"""
class GridSource(ImageSource):
def __init__(self):
"""Generate grid pattern images"""
def SetKernelOffset(self, offset: tuple):
"""Set grid offset"""
def SetKernelSpacing(self, spacing: tuple):
"""Set grid spacing"""
def SetSigma(self, sigma: tuple):
"""Set sigma for grid lines"""
class CheckerBoardSource(ImageSource):
def __init__(self):
"""Generate checkerboard pattern images"""
def SetCheckerValue(self, values: tuple):
"""Set values for checkerboard squares (value1, value2)"""Base classes for different types of image filters.
class ImageFilter(ProcessObject):
def __init__(self):
"""Base class for image-to-image filters"""
def SetInput(self, image: Image):
"""Set input image"""
def GetInput(self) -> Image:
"""Get input image"""
class UnaryFunctorImageFilter(ImageFilter):
def __init__(self):
"""Base class for unary function filters (single input image)"""
class BinaryFunctorImageFilter(ProcessObject):
def __init__(self):
"""Base class for binary function filters (two input images)"""
def SetInput1(self, image: Image):
"""Set first input image"""
def SetInput2(self, image: Image):
"""Set second input image"""
def GetInput1(self) -> Image:
"""Get first input image"""
def GetInput2(self) -> Image:
"""Get second input image"""
class TernaryFunctorImageFilter(ProcessObject):
def __init__(self):
"""Base class for ternary function filters (three input images)"""
def SetInput1(self, image: Image):
"""Set first input image"""
def SetInput2(self, image: Image):
"""Set second input image"""
def SetInput3(self, image: Image):
"""Set third input image"""Install with Tessl CLI
npx tessl i tessl/cmake-simpleitk