# How to create LabelToRGBFunctor

**URL:** <https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563>\
**Category:** Beginner Questions\
**Created:** [January 2, 2018, 10:08am UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563 "2018-01-02T10:08:32Z")\
**Posts on this page:** 10\
**Page:** 1

<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:** [January 2, 2018, 10:08am UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/1 "2018-01-02T10:08:33Z")

</div>

for the C++ implementation of LabelMapToRGBImageFilter  
It says to use a functor to the colormap,

im simpleITK, they convert a colormap into a functor:

> <https://github.com/SimpleITK/SimpleITK/blob/next/Code/BasicFilters/src/sitkLabelFunctorUtils.hxx>

However, in the python version how to I create a type of itk::Functor::LabelToRGBFunctor?

Thanks

---

<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:** [January 2, 2018, 2:00pm UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/2 "2018-01-02T14:00:37Z")

</div>

It does not appear that the `LabelToRGBFunctor` class is wrapped yet:

> **[InsightSoftwareConsortium/ITK](https://github.com/InsightSoftwareConsortium/ITK/tree/master/Modules/Filtering/ImageFusion/wrapping)**
>
> Insight Toolkit (ITK) -- Official Repository. Contribute to InsightSoftwareConsortium/ITK development by creating an account on GitHub.

This can be easily done by adding a new wrapping for the class. Please see the ITK Software Guide for more information on how to do this.

---

<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:** [January 2, 2018, 2:40pm UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/3 "2018-01-02T14:40:40Z")

</div>

hmm ya it seems so. how do you set the color map then?

---

<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:** [January 2, 2018, 2:46pm UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/4 "2018-01-02T14:46:55Z")

</div>

It looks like many of the filters in the ImageFusion module provide the following interface:

```auto
 /** Add color to the LUT container */
  void AddColor(ComponentType r, ComponentType g, ComponentType b);

  /** Empty the color LUT container */
  void ResetColors();

  /** Get number of colors in the LUT container */
  unsigned int GetNumberOfColors() const;

```

---

<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:** [January 3, 2018, 10:01am UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/5 "2018-01-03T10:01:30Z")

</div>

hmm,  
I need to set the colours for itk.LabelMapOverlayImageFilter though,  
and it only has SetFunctor, any idea how I can do this?

I can call .GetFunctor() but it returns a  
\<Swig Object of type ‘itk::Functor::LabelToRGBFunctor\< unsigned long,itk::RGBPixel\< unsigned char \> \> \*’ at 0x12e007c30\>

and I have no idea how to edit it in python

also, how do I access the image fusion module?

---

<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:** [January 8, 2018, 8:01pm UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/6 "2018-01-08T20:01:42Z")

</div>

Hi @benjaminhon,

[Here is an example](https://github.com/InsightSoftwareConsortium/ITKExamples/pull/36/files#diff-e1c9a59fb125b070c4669e71da1f13a0):

```auto
 #!/usr/bin/env python

import sys
import itk

if len(sys.argv) != 4:
    print("Usage: " + sys.argv[0] +
          " <InputFileName> <LabelMap> <OutputFileName>")
    sys.exit(1)

inputFileName = sys.argv[1]
labelFileName = sys.argv[2]
outputFileName = sys.argv[3]

PixelType = itk.ctype('unsigned char')
Dimension = 2

ImageType = itk.Image[PixelType, Dimension]

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

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

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

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(0.5)

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

```

---

<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:** [January 12, 2018, 1:33am UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/7 "2018-01-12T01:33:49Z")

</div>

Hi Matt,  
But how would you set the colormap for the labelmapfilter?

I ended up using LabelToRGBImageFilter instead of LabelMapToRGBImageFilter

However, I realise that I can only ResetColors() once, which means I can only set the color at the first time. After that I cannot dynamically change the colours, calling ResetColors() again and AddColor() does not seem to change the image.

I did further tests,

it seems that for OverlayLabelFilter, changing colours dynamically(ResetColors(), AddColor) works, but not for LabelToRGBImageFilter. Maybe this is a bug?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [January 12, 2018, 5:18pm UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/8 "2018-01-12T17:18:49Z")

</div>

What happens if you manually call `Update()` after a call to `ResetColors()` or `AddColor()`?

---

<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:** [January 12, 2018, 6:06pm UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/9 "2018-01-12T18:06:45Z")

</div>

itk.LabelMapOverlayImageFilter currently has a fixed progression of colors.

For LabelToRGBImageFilter or LabelMapToRGBImageFilter, is there a simple, self contained example we could run to reproduce the behavior observed?

---

<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:** [January 15, 2018, 2:08am UTC](https://discourse.itk.org/t/how-to-create-labeltorgbfunctor/563/10 "2018-01-15T02:08:59Z")

</div>

Hi Matt, here are my functions, ill try to make a self contained example. Actually both overlay and labelmap does not seem to allow Reseting the colors and adding new colors.

Hi Dzenan, yep I’ve called Update()

```auto
def LabelToRGBImageFilter(input, colors=[]):
    inputType = type(input)
    labelToRGBImageFilter = itk.LabelToRGBImageFilter[inputType, itk.Image[itk.RGBPixel[itk.UC], 3]].New(input)
    if len(colors) > 0:
        labelToRGBImageFilter.ResetColors()
        for color in colors:
            labelToRGBImageFilter.AddColor(*color)
    labelToRGBImageFilter.Update()
    return labelToRGBImageFilter

def OverlayLabelFilter(featureImage, labelImage, opacity=0.5, colors=[]):
    labelType = type(labelImage)
    featureType = type(featureImage)
    labelOverlayImageFilter = itk.LabelOverlayImageFilter[labelType, featureType,itk.Image[itk.RGBPixel[itk.UC], 3]].New()
    labelOverlayImageFilter.SetInput(featureImage)
    labelOverlayImageFilter.SetLabelImage(labelImage)
    labelOverlayImageFilter.SetOpacity(opacity);
    if len(colors) > 0:
        labelOverlayImageFilter.ResetColors()
        for color in colors:
            labelOverlayImageFilter.AddColor(*color)
    labelOverlayImageFilter.Update()
    return labelOverlayImageFilter

```

after that, when trying to reset the colors

```auto
labelOverlayImageFilter.ResetColors()
for color in [(0,0,0), (255,0,0),(0,255,0),(0,0,255)]:
            labelOverlayImageFilter.AddColor(*color)

```
