Using LabelMap in Python

The ITK example to show how to use itk.LabelMapOverlayImageFilter is only written in C++.

This post show how to find types that are available in Python for a specific ITK filter and in the case of itk.LabelMap, who to instantiate an object of the desired type.

The Python wrapping does not provide the implementation of all possible types that would be available in C++, but a limited number of types that are the most useful. This is due to the fact that:

  • The list of types has to be chosen when the Python wheel is created. Hence, not all use cases can be anticipated.
  • Wrapping all possible types would generate an ITK Python wheel that would be way too large.

One way of listing the types that are available for a class is to call the function GetTypesAsList() on the object that we are trying to use. In the case of itk.LabelMap, one can call:

In [18]: itk.LabelMap.GetTypesAsList()
Out[18]: 
["(<class 'itkStatisticsLabelObjectPython.itkStatisticsLabelObjectUL2'>,)",
 "(<class 'itkStatisticsLabelObjectPython.itkStatisticsLabelObjectUL3'>,)"]

We now know that itk.LabelMap has been wrapped for 2 types of label objects.
To instantiate an object of type itk.LabelMap, I can do:

Dimension = 3
PixelType = itk.UL  # I see in the list of itk.LabelMap that the only types available are using `unsigned long` (`itk.UL`)

LabelObjectType = itk.StatisticsLabelObject[PixelType, Dimension]
LabelMapType = itk.LabelMap[LabelObjectType]

I hope this example helps!

2 Likes

Thanks! Much appreciated

The LabelMapOverlayImageFilter object in the example is instantiated only with LabelMapType and ImageType but in Python you need 3 arguments: [LabelMapType, InputImageType, OutputImageType]

OutputPixelType = itk.RGBPixel[itk.UC]
OutputImgType = itk.Image[OutputPixelType, Dimension]

f = itk.LabelMapOverlayImageFilter[LabelMapType, InputImgType, OutputImgType].New()
1 Like

Indeed. There is no default “template” argument in ITK Python.

By the way, in my first answer, I mentioned the method GetTypesAsList() which returns a list of strings containing the types that are available for that object. I wanted to add to this answer. It is also possible to just print the types that are available with GetTypes() and get a list of the possible types as a list of types with the keys() method which can be used programatically.

2 Likes