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?

Thanks

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;

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?

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

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:
        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

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