Using LabelMap in Python

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()
["(<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!

1 Like