Overlay Several Labels On Top Of An Image

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

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.

Here is the original image :

Thank you !

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

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

image = itk.imread(inputFileName)
1 Like

The regular LabelOverlayImageFilter does support custom color maps. This is a similar filter that works with labeled images and not LabelMaps of LabelObjects.

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 (50.4 KB)
mask1.nrrd (50.2 KB)

Thanks !

Here is the 3rd mask:

mask3.nrrd (50.6 KB)

Here we would just call

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

because

image = reader.GetOutput()

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:

 
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:

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

Thanks! Can we also share the feature image?

1 Like

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