How to create LabelToRGBFunctor

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

im simpleITK, they convert a colormap into a functor:

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


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

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.

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

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

 /** 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;

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?

Hi @benjaminhon,

Here is an example:

 #!/usr/bin/env python

import sys
import itk

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

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

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

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

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

RGBImageType = itk.Image[itk.RGBPixel[PixelType], Dimension]
overlayFilter = itk.LabelMapOverlayImageFilter[LabelMapType, ImageType, RGBImageType].New()

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

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?

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

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?

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

def LabelToRGBImageFilter(input, colors=[]):
    inputType = type(input)
    labelToRGBImageFilter = itk.LabelToRGBImageFilter[inputType, itk.Image[itk.RGBPixel[itk.UC], 3]].New(input)
    if len(colors) > 0:
        for color in colors:
    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()
    if len(colors) > 0:
        for color in colors:
    return labelOverlayImageFilter

after that, when trying to reset the colors

for color in [(0,0,0), (255,0,0),(0,255,0),(0,0,255)]:
1 Like