SimpleITK
SimpleITK is a simplified, open-source interface to the Insight Segmentation and Registration Toolkit (ITK) for image registration and segmentation. It supports multiple programming languages, including Python, and offers a high-level, procedural interface to ITK's extensive algorithms. It aims to make image analysis more accessible for rapid prototyping, education, and various applications, especially in medical imaging. The current version is 2.5.3, with regular releases.
Warnings
- breaking SimpleITK 2.0 removed the `Transform::AddTransform` method. Composite transformations are now handled by a dedicated `CompositeTransform` class, requiring explicit creation of this class instead of adding transforms to a generic `Transform` object.
- breaking With SimpleITK 2.0, filter `Execute` methods no longer accept parameters as arguments. All filter parameters must be set via their respective `SetXYZ` methods on the filter object before calling `Execute`.
- gotcha When converting between a SimpleITK `Image` object and a NumPy array (e.g., using `sitk.GetArrayFromImage` or `sitk.GetImageFromArray`), the axis order is reversed. SimpleITK uses (x, y, z) indexing, while NumPy typically uses (z, y, x) (depth, rows, columns).
- gotcha On Windows, importing SimpleITK may fail with `ImportError: DLL load failed while importing _SimpleITK: The specified module could not be found`. This often indicates missing Microsoft Visual C++ Redistributable packages.
- gotcha SimpleITK does not include a built-in image viewer. While `sitk.Show()` can launch an external viewer (like ImageJ/Fiji), users might expect immediate visualization without additional software or configuration.
- breaking SimpleITK 2.0 upgraded its underlying ITK dependency to version 5 (ITKv5). This major update brought numerous changes to ITK's internal workings and behaviors, which can indirectly affect SimpleITK users, especially those working with advanced features or custom ITK integrations.
- gotcha Some versions of SimpleITK, particularly when dealing with specific datasets, may encounter issues with non-orthonormal direction cosines, leading to errors like 'ITK only supports orthonormal direction cosines. No orthonormal definition found.' This can be problematic for certain medical imaging data.
Install
-
pip install SimpleITK -
conda install -c conda-forge simpleitk
Imports
- SimpleITK
import SimpleITK as sitk
Quickstart
import SimpleITK as sitk
# Create a 2D image (e.g., 64x64 pixels, 16-bit integer)
image_size = [64, 64]
image = sitk.Image(image_size, sitk.sitkInt16)
# Set some pixel values (e.g., a square in the middle)
# Note: SimpleITK indexing is (x,y,z), while numpy conversion often reverses axes.
image[10:50, 10:50] = 100
# Apply a Gaussian smoothing filter
gaussian_filter = sitk.SmoothingRecursiveGaussianImageFilter()
gaussian_filter.SetSigma(2.0) # Set the standard deviation of the Gaussian
smoothed_image = gaussian_filter.Execute(image)
# Get a pixel value at a specific index
pixel_value = smoothed_image.GetPixel((32, 32))
print(f"Pixel value at (32,32) after smoothing: {pixel_value}")
# Save the image (e.g., to a MetaImage file format)
sitk.WriteImage(smoothed_image, "smoothed_image.mha")
print("Saved smoothed_image.mha")
# To visualize the image, you would typically use an external viewer
# For example, if ImageJ/Fiji is installed and configured:
# sitk.Show(smoothed_image, 'Smoothed Image')