# Overlay Several Labels On Top Of An Image

**URL:** <https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096>\
**Category:** Beginner Questions\
**Created:** [July 5, 2018, 8:16pm UTC](https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096 "2018-07-05T20:16:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dre](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@Dre](https://discourse.itk.org/u/Dre)\
**Post date:** [July 5, 2018, 8:16pm UTC](https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096/1 "2018-07-05T20:16:22Z")

</div>

Hello,

I am trying to apply several .nrrd labels (binary segmentation masks) on top of an .nrrd image (a CT scan). Am I able to achieve this task thanks to this example :  
[https://itk.org/ITKExamples/src/Filtering/ImageFusion/OverlayLabelMapOnTopOfAnImage/Documentation.html](https://itk.org/ITKExamples/src/Filtering/ImageFusion/OverlayLabelMapOnTopOfAnImage/Documentation.html)

However, I am unable to customize the color of the masks. I couldn’t find any color properties in the class LabelMapOverlayImageFilter.

```
    InputFileName = CT_Scan.nrrd'
    labelFileName = ['mask1.nrrd',mask2.nrrd',mask3.nrrd']
    outputFileName = 'output.nii'

for i in range (len(labelFileName)):
    PixelType = itk.ctype('unsigned char')
    Dimension = 3

    ImageType = itk.Image[PixelType, Dimension]

    reader = itk.ImageFileReader[ImageType].New()
    reader.SetFileName(inputFileName)

    LabelType = itk.ctype('unsigned long')
    LabelObjectType = itk.StatisticsLabelObject[LabelType, Dimension]
    LabelMapType = itk.LabelMap[LabelObjectType]

    labelReader = itk.ImageFileReader[ImageType].New()
    labelReader.SetFileName(labelFileName[i])

    converter = itk.LabelImageToLabelMapFilter[ImageType, LabelMapType].New()
    converter.SetInput(labelReader)

    RGBImageType = itk.Image[itk.RGBPixel[PixelType], Dimension]
    overlayFilter = itk.LabelMapOverlayImageFilter[LabelMapType, ImageType, RGBImageType].New()
    overlayFilter.SetInput(converter.GetOutput())
    overlayFilter.SetFeatureImage(reader.GetOutput())

    overlayFilter.SetOpacity(1)

    itk.imwrite(overlayFilter.GetOutput(), outputFileName)
    inputFileName = outputFileName

```

Moreover, the output image seems to have some noise.

 ![noise](https://discourse.itk.org/uploads/default/original/1X/5581963695b5b96deb6256d68b847b313e9f680d.jpg)

Here is the original image :

 ![original](https://discourse.itk.org/uploads/default/original/1X/230cb83aead38b7d9be0e07889c93073339be956.png)

Thank you !

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [July 5, 2018, 8:44pm UTC](https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096/2 "2018-07-05T20:44:30Z")

</div>

Hi @Dre,

Welcome to ITK!

> Moreover, the output image seems to have some noise.

It looks like the original image has a higher dynamic range than supported by the `unsigned char` pixel type used to read the image, so we are seeing more variations over this small range and wrap-around when the values exceed the 0-255 range. One option is to read in the image with a different pixel type with a larger range, like `short`, or to rescale the intensity to 0-255 with an `itk.RescaleIntensityImageFilter`.

> However, I am unable to customize the color of the masks

The `itk.LabelMapOverlayImageFilter` does not support custom colors, but we may be able to add them with other classes. Can you share your input images to create an example?

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [July 5, 2018, 8:48pm UTC](https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096/3 "2018-07-05T20:48:23Z")

</div>

PS. Note that the image can be read in with its native pixel type and dimension with

```auto
image = itk.imread(inputFileName)

```

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [July 5, 2018, 8:50pm UTC](https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096/4 "2018-07-05T20:50:58Z")

</div>

The regular [LabelOverlayImageFilter](https://www.itk.org/Doxygen/html/classitk_1_1LabelOverlayImageFilter.html) does support custom color maps. This is a similar filter that works with labeled images and not LabelMaps of LabelObjects.

---

<div class="post-metadata">

**Author:** ![Dre](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@Dre](https://discourse.itk.org/u/Dre)\
**Post date:** [July 5, 2018, 9:19pm UTC](https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096/5 "2018-07-05T21:19:14Z")

</div>

Thank you for you replies !

@matt.mccormick I tried to read the image with `imread` but I can’t get it to work : `overlayFilter.SetFeatureImage(reader.GetOutput()) AttributeError: 'itkImageSS3' object has no attribute 'GetOutput'`

I also tried modifying the pixel type from `unsigned char` to `short`, but I got another error

```
     Traceback (most recent call last):
      File "/home/dre/anaconda3/lib/python3.6/site-packages/itkTemplate.py", line 266, in __getitem__
        return(self. __template__ [tuple(cleanParameters)])
    KeyError: (<class 'itkImagePython.itkImageSS3'>, <class 'ITKLabelMapBasePython.itkLabelMap3'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dre/anaconda3/lib/python3.6/site-packages/itkTemplate.py", line 270, in __getitem__
    return(self. __template__ [tuple(cleanParameters)])
KeyError: (<class 'itkImagePython.itkImageSS3'>, <class 'ITKLabelMapBasePython.itkLabelMap3'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dre/Stage/LongitudinalTumorTracking/Python_Backend/segFilterv1.py", line 63, in <module>
    converter = itk.LabelImageToLabelMapFilter[ImageType, LabelMapType].New()
  File "/home/dre/anaconda3/lib/python3.6/site-packages/itkTemplate.py", line 274, in __getitem__
    (str(parameters), self. __name__ ))
KeyError: "itkTemplate : No template (<class 'itkImagePython.itkImageSS3'>, <class 'ITKLabelMapBasePython.itkLabelMap3'>) for the itk::LabelImageToLabelMapFilter class" 

```

Here are the files:  
[mask2.nrrd](https://discourse.itk.org/uploads/default/original/1X/681ed23b2449ba24b9ac1737fb669d840f70e608.nrrd) (50.4 KB)  
[mask1.nrrd](https://discourse.itk.org/uploads/default/original/1X/152c96bb59700b8e067efdc3fab338898f76f059.nrrd) (50.2 KB)

Thanks !

---

<div class="post-metadata">

**Author:** ![Dre](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@Dre](https://discourse.itk.org/u/Dre)\
**Post date:** [July 5, 2018, 9:20pm UTC](https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096/6 "2018-07-05T21:20:33Z")

</div>

Here is the 3rd mask:

[mask3.nrrd](https://discourse.itk.org/uploads/default/original/1X/83b1188cefb5d51a05b0c796327115b38d60baa4.nrrd) (50.6 KB)

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [July 5, 2018, 9:46pm UTC](https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096/7 "2018-07-05T21:46:51Z")

</div>

> [@Dre](#):
>
> overlayFilter.SetFeatureImage(reader.GetOutput()) AttributeError: ‘itkImageSS3’ object has no attribute ‘GetOutput’

Here we would just call

```auto
image = itk.imread(inputFileName)
overlayFilter.SetFeatureImage(image) 

```

because

```auto
image = reader.GetOutput()

```

> [@Dre](#):
>
> Traceback (most recent call last): File “/home/dre/Stage/LongitudinalTumorTracking/Python\_Backend/segFilterv1.py”, line 63, in \<module\> converter = itk.LabelImageToLabelMapFilter[ImageType, LabelMapType].New() File “/home/dre/anaconda3/lib/python3.6/site-packages/itkTemplate.py”, line 274, in **getitem** (str(parameters), self. **name** )) KeyError: “itkTemplate : No template (\<class ‘itkImagePython.itkImageSS3’\>, \<class ‘ITKLabelMapBasePython.itkLabelMap3’\>) for the itk::LabelImageToLabelMapFilter class”

This means that the `LabelImageToLabelMapFilter` was not built for the `signed short` pixel type.

We can see what types it was built for [with the `.GetTypes()` method](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch3.html#x39-440003.7):

```auto
 
In [1]: import itk

In [2]: itk.LabelImageToLabelMapFilter.GetTypes()
<itkTemplate itk::LabelImageToLabelMapFilter>
Options:
  [<class 'itkImagePython.itkImageUC2'>, <class 'ITKLabelMapBasePython.itkLabelMap2'>]
  [<class 'itkImagePython.itkImageUC3'>, <class 'ITKLabelMapBasePython.itkLabelMap3'>]
  [<class 'itkImagePython.itkImageUS2'>, <class 'ITKLabelMapBasePython.itkLabelMap2'>]
  [<class 'itkImagePython.itkImageUS3'>, <class 'ITKLabelMapBasePython.itkLabelMap3'>]

```

Since label images are usually unsigned integers, it is only built for these types.

One path is to rescale the feature image:

```auto
featureImage = itk.imread(inputFileName)
rescaler = itk.RescaleIntensityImageFilter[type(featureImage), ImageType].New()
rescaler.SetInput(featureImage)
overlayFilter.SetInput(rescaler.GetOutput())

```

> [@Dre](#):
>
> Here are the files:  
> [mask2.nrrd](https://discourse.itk.org/uploads/default/original/1X/681ed23b2449ba24b9ac1737fb669d840f70e608.nrrd) (50.4 KB)

Thanks! Can we also share the feature image?

---

<div class="post-metadata">

**Author:** ![Dre](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@Dre](https://discourse.itk.org/u/Dre)\
**Post date:** [July 6, 2018, 3:01pm UTC](https://discourse.itk.org/t/overlay-several-labels-on-top-of-an-image/1096/8 "2018-07-06T15:01:24Z")

</div>

I’ve tried to rescale the image this way as `overlayFilter` is expecting an argument of type `itkLabelMap3 or itkImageSourceLM3`

```
featureImage = itk.imread(inputFileName)
rescaler = itk.RescaleIntensityImageFilter[type(featureImage), ImageType].New()
rescaler.SetInput(featureImage)
converter.SetInput(labelReader)
overlayFilter.SetInput(converter.GetOutput())
overlayFilter.SetFeatureImage(rescaler.GetOutput())

```

However, I got this error :

```
Traceback (most recent call last):
  File "segFilterv1.py", line 55, in <module>
    rescaler = itk.RescaleIntensityImageFilter[type(featureImage), ImageType].New()
  File "/home/dre/anaconda3/lib/python3.6/site-packages/itkTemplate.py", line 274, in __getitem__
    (str(parameters), self. __name__ ))
KeyError: "itkTemplate : No template (<class 'itkImagePython.itkImageRGBUC3'>, <class 'itkImagePython.itkImageUC3'>) for the itk::RescaleIntensityImageFilter class"

```

The feature image was too large to upload.  
The password is ‘itk’:  
[https://ufile.io/3vwpa](https://ufile.io/3vwpa)
