# Using LabelMap in Python

**URL:** <https://discourse.itk.org/t/using-labelmap-in-python/259>\
**Category:** Beginner Questions\
**Tags:** python\
**Created:** [October 4, 2017, 2:20pm UTC](https://discourse.itk.org/t/using-labelmap-in-python/259 "2017-10-04T14:20:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [October 4, 2017, 2:20pm UTC](https://discourse.itk.org/t/using-labelmap-in-python/259/1 "2017-10-04T14:20:32Z")

</div>

The ITK [example](https://itk.org/ITKExamples/src/Filtering/ImageFusion/OverlayLabelMapOnTopOfAnImage/Documentation.html) 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!

---

<div class="post-metadata">

**Author:** ![benjaminhon](https://discourse.itk.org/user_avatar/discourse.itk.org/benjaminhon/32/83_2.png) [@benjaminhon](https://discourse.itk.org/u/benjaminhon)\
**Post date:** [October 5, 2017, 7:43pm UTC](https://discourse.itk.org/t/using-labelmap-in-python/259/2 "2017-10-05T19:43:18Z")

</div>

Thanks! Much appreciated

---

<div class="post-metadata">

**Author:** ![mafuentes](https://discourse.itk.org/user_avatar/discourse.itk.org/mafuentes/32/647_2.png) [@mafuentes](https://discourse.itk.org/u/mafuentes)\
**Post date:** [October 6, 2017, 1:26pm UTC](https://discourse.itk.org/t/using-labelmap-in-python/259/3 "2017-10-06T13:26:40Z")

</div>

The `LabelMapOverlayImageFilter` object in the [example](https://itk.org/ITKExamples/src/Filtering/ImageFusion/OverlayLabelMapOnTopOfAnImage/Documentation.html) 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()
```

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [October 6, 2017, 1:43pm UTC](https://discourse.itk.org/t/using-labelmap-in-python/259/4 "2017-10-06T13:43:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [November 3, 2017, 8:38pm UTC](https://discourse.itk.org/t/using-labelmap-in-python/259/5 "2017-11-03T20:38:24Z")

</div>

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.
