Convert ITK Data Structures to NumPy Arrays

We previously announced that the ITK Python wrapping is now available in the Python Package Index (PyPI) [1].

Starting to develop Python scripts using ITK filters is one command line away:

Python is a scripting language that allows fast development and is widely used in the scientific community.

You can use ITK Python to read and write all the common image formats as well as many others. Combined with the power and speed of the hundreds of ITK filters, it is fast and easy to create and adapt complex image processing pipelines. The example below illustrates how to create a simple pipeline that reads an image, processes it with a median filter, and saves the result.

Sometimes, such as when one wants to display an image in an IPython notebook, it is necessary to directly access the raw content of an image. It is possible through the ITK image object to get the image values pixel by pixel, but manually iterating over the entire image would result in slow processing. Instead, ITK provides a bridge to convert ITK data to NumPy arrays. The conversion can be performed in both directions. It is therefore possible to combine the speed and power of ITK to process images, and the speed and versatility of NumPy to accomplish specific operations that are not available in ITK.

The ITK NumPy bridge converts ITK images, but also vnl vectors and vnl matrices to NumPy arrays. The data can either be copied into a new object or a view on the data can be created. The view allows access and modification of the data without the need to duplicate its memory. Depending on the situation, a view or copy is preferred.

The functions to convert ITK images to/from NumPy arrays/views are:

All the functions to convert to and from vnl vectors and vnl matrices are presented in the ITK Python Quick Start Guide [2] (Hint: It is easy to guess their names).

A short example illustrating how to use ITK Python and the ITK NumPy bridge is available here [3].

Have fun using ITK!

[1] https://blog.kitware.com/itk-is-on-pypi-pip-install-itk-is-here

[2] https://itkpythonpackage.readthedocs.io/en/latest/Quick_start_guide.html

[3] https://github.com/fbudin69500/ConvertITKDatatoNumPyArrayBlogPost

19 Responses to Convert ITK Data Structures to NumPy Arrays

  1. Pingback: Fernando Arencibia de Blas

    • Pingback: Francois Budin

      • Pingback: Fernando Arencibia de Blas

        • Pingback: Francois Budin

          • Pingback: Matt McCormick

  2. Pingback: Dominique

    • Pingback: Francois Budin

      • Pingback: Fernando Arencibia de Blas

        • Pingback: Matt McCormick

      • Pingback: Dominique

        • Pingback: Fernando Arencibia de Blas

        • Pingback: Francois Budin

          • Pingback: Fernando Arencibia de Blas

          • Pingback: Francois Budin

          • Pingback: Matt McCormick

  3. Pingback: Aron Helser

  4. Pingback: Jean-Christophe Fillion-Robin

  5. Pingback: Matheus Palhares Viana

  6. Pingback: Matt McCormick

Questions or comments are always welcome!

X