Wrap ITK image pointers with pybind11 and mix with the itk python package

Hi there, I am wrapping to python a library that I maintain SGEXT using pybind11. I have wrapped the algorithms in the library, but I also wanted to wrap high level functions which use ITK, not only internally, but in the interface as inputs or outputs.

I was checking how hard would it be to just wrap itk::SmartPointer<TImage> with pybind11, which is the only thing in the interface that is usually needed. This is the returning type of the filters functions an_itk_filter.GetOutput(), and the input type of an_itk_filter.SetInput(img_ptr).

It turns out that wasn’t that hard!
I added functions in my wrapping to export and import from numpy and use the regular itk python functions itk.GetArrayFromImage and itk.GetImageFromArray to interact with the itk package.

This is work in progress, right now I am copying the data, I would also like to offer a view version, where no data is copied. But it is encouraging being able to mix both frameworks in python with no much effort.
I am sure this might be useful for others, please find details in the following commit where the ITK wrapping is added using pybind11.

2 Likes

Amazing work @phcerdan! Quite elegant. :sun_with_face:

1 Like