The itk
Python package is gaining support for NumPy arrays and NumPy-like arrays. And, NumPy is adding support for additional NumPy-like arrays, “duck arrays”, like Dask arrays, Zarr arrays, xarray, CuPy arrays, … we have an opportunity to improve the image processing experience across the scientific Python ecosystem by following the principle of compatible, NumPy-based interfaces.
We can add critical spatial support by extending the ndarray
interface with three spatial image attributes:
-
origin
: the physical distance in the coordinate system to the center of the “lower left” pixel.
-
spacing
: the N-dimensional physical spacing between pixels.
-
direction
: an orientation matrix consisting of direction cosines.
This would make
- Resampling
- Registration
- Multi-resolution
- …
image processing elegant and effective in Python.
Soon, itk.Image
will be a partial duck array. And, ITK image filters will support passing and returning NumPy duck arrays. When NumPy duck arrays are passed, if hasattr(image, 'spacing'): [...]
can be used to check and use spacing information, if present. The returned object from an itk
filter will have origin
, spacing
, and direction
attributes set accordingly, whether an itk.Image
type or a spatial image type, i.e. has origin
, spacing
, and direction
attributes, derived from numpy.ndarray
.
After a few issues are implemented, we will have the basics covered, and we can create a pre-release to test and refine.
It is absolutely wonderful to have folks from the broader community like @matthew.brett, @jni, @lassoan, @ctrueden, @pieper participating in the discussion. Ideally, additional tools in the community incrementally support a NumPy-based spatial image interface – an interface approach is nice because it is not tied to an implementation, can be adopted incrementally, and is optional. Ideally, scikit-image would utilize spatial attributes, if present, as more spatial functionality is added to scikit-image, SimpleITK Python starts to support these attributes, pyimagej transfers these attributes when converting an ImgLib2 image to NumPy with imglyb, 3D Slicer supports these attributes when working with volume nodes, and nibabel provides the interface. We also discussed the issue of preserving spatial metatdata in the imageio
package and its interface.