ITK (Insight Toolkit) is an open-source, cross-platform toolkit for N-dimensional scientific image analysis, including processing, segmentation, and registration. The `itk-filtering` package bundles Python bindings for many ITK filtering modules. The current stable version is 5.4.5, with frequent maintenance releases for the 5.x series and active beta development for the upcoming 6.0 major version.
pip install itk-filteringVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create a simple ITK image programmatically, apply a 2D Median filter, and inspect the output. It highlights the templated nature of ITK images and filters, and shows how to convert between ITK image objects and NumPy arrays for interoperability.
Review the ITK 6.0 release notes and migration guides once available. Test your code against ITK 6.0 beta releases. Adjust Python API calls as necessary, particularly for class constructors or function signatures that might have changed due to C++ template updates.
Always be mindful of image pixel types (e.g., `itk.UC` for unsigned char, `itk.F` for float) and dimensionality (e.g., `2`, `3`). Use `itk.CastImageFilter` to explicitly convert between types when needed. Often, ITK's Pythonic wrappers (`itk.median_image_filter()`) handle templating implicitly, but understanding the underlying C++ types is key for debugging.
Prefer in-place operations or filters that operate on regions when possible. Leverage ITK's streaming capabilities for very large images if your pipeline supports it. Use `itk.GetArrayViewFromImage` for zero-copy access to image data in NumPy where appropriate, but be careful not to modify the view if the ITK object is meant to be immutable.
Ensure `itk` is installed by running `pip install itk` or `pip install itk-filtering` to get the core library along with the filtering components.
Check the exact name and capitalization of the filter in the ITK documentation. For templated filters, ensure you are using the correct Pythonic factory function (e.g., `itk.median_image_filter()`) or explicitly templating the class (`itk.MedianImageFilter[ImageType, ImageType].New()`). Sometimes, a filter might be in a less common module that requires a separate `import` (e.g., `import itk.bridge.vtk` for VTK integration), though this is less common for core filtering.
Use `itk.CastImageFilter` to explicitly convert your image to the required pixel type before applying the filter. For example, `itk.CastImageFilter[itk.UC, itk.F].New().SetInput(input_image).Update().GetOutput()` converts an unsigned char image to float.