ITKVTKGlue Module in Python Wheel

I am trying to use ITK and VTK together in python. I installed everything through pip. Quickly trying I get the following error:

$ conda create -n proc python=3
$ source activate proc
$ pip install itk
$ conda install -c clinicalgraphics vtk
$ python
>>> import itk
>>> image_type = itk.Image[itk.UC, 3]
>>> itk_vtk_converter = itk.ImageToVTKImageFilter[image_type].New()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/babesler/anaconda3/envs/imapro/lib/python3.6/site-packages/itkLazy.py", line 40, in __getattribute__
value = types.ModuleType.__getattribute__(self, attr)
AttributeError: module 'itk' has no attribute 'ImageToVTKImageFilter'

From the online example I can see that:

This requires Module_ITKVtkGlue to be turned on in ITK’s CMake configuration.

It is nice to build be able to prototype ITK/VTK in python without having to build from scratch.

I’m wondering if it’s possible to build a Python wheel with Module_ITKVtkGlue? Is it possible to do the same thing as is done with itk-bonemorphometry? What would be involved in this?

3 Likes

Welcome @Besler!

Yes, good point – it would be much easier to use ITK and VTK together if a package for ITKVTKGlue was available.

We could try the approach used in the itk-bonemorphometry module,
documented here.

Complications: we need to build against VTK and use its libraries at runtime. But, I think we may be able to do this if we:

  • Use the conda clinicalgraphics vtk packages to provide a pre-built VTK.
  • Use weak linking on Linux and macOS when linking the VTK libraries. Then, make sure the VTK libraries have been loaded by calling import vtk when ITKVTKGlue gets imported. This could work around finding the VTK libraries at runtime.
  • Repackage the generated wheels in conda-forge like we do with itk.

We will have to watch out for compiler ABI incompatibilities, but we will give it a try. I will take at getting it started next week.

@jcfr @cory.quammen

3 Likes

Thank you for the warm welcome!

I hate to make work for you. Although my skills are limited, I will follow closely so I can contribute next time. If you have any tasks I can help with or feel like holding my hand, do not hesitate to let me know.

I will read through the docs in more detail this week.

1 Like

Hi Bryce,

Just to follow-up with current status, no action required yet; I have started the work on the module package here. Once ready I will move to the InsightSoftwareConsortium GitHub organization. Current progress:

  • Imported into an independent module
  • import vtk on package loading

Next steps:

  • Build against vtk package
  • Testing

Cheers,
Matt

3 Likes

Thanks @matt.mccormick, that’s useful!

If VTKGlue will have its own package, maybe its useful to add there a simple image viewer executable.

I have used this piece of code in the IsotropicWavelets module to visualize 2D, and 3D images for tests with just ITK and VTK.

The interface is simple, I run it with:
runViewImage /path/image.tif
it also accepts an optional string for the title of window. Nothing fancy, but way faster and convenient (at least for me) than using ImageJ or any other external software. Maybe python users with the ITKVtkGlue module might find it useful for fast visualization.

You can see three of them (3D images) open with black background at the right of the attached screenshot.

That’s a great idea @phcerdan! I write my own visualizers in VTK. I have a pain dealing with image orientation alone which is why I appreciate the VTK/ITK glue.

Does this code handle image orientation properly?

1 Like

Hi @Besler, not sure about orientation to be honest, I just remember having to flip the camera, because the origins in VTK and ITK are different. I am creating a github repository with just the viewer, so you can give it a go.

@Besler find the image viewer here: https://github.com/phcerdan/ITKVTKImageViewer

It is not an external module, so, you just download it, use cmake point to ITK (with ITKVTKGlue Module ON) and to VTK, and compile it. There are more detailed instructions in the repository. Let me know if you find time to test it!

Cheers,
Pablo

@phcerdan That’s excellent! Yes, we could add a few more simple viewers to the module. There is already a simple 2D viewer called QuickView by @bill.lorensen. We could extend it or add a few more. And we could add Python wrapping for the classes.

@Besler, currently VTK does not support direction/orientation information for images, but there is interest in adding it from @cory.quammen @lassoan and others.

Adding orientation to VTK images would be sorely needed for all VTK-based medical imaging applications (it would make them simpler and faster), but VTK core developers are not much interested, so it’s completely up to the community to come up with a solution and get it accepted. See a summary of the problem and proposed approach to solve it:

https://www.slicer.org/wiki/Documentation/Labs/VTK-Orientation

If you know VTK well and would be interested in working towards this then let us know.

2 Likes

Hi @lassoan, the link you provided is private. I have requested access, but not sure if that’s intended.

Thanks for letting me know. I’ve updated the link above.