Overlay Label Map On Top Of An Image

Synopsis

Apply a colormap to a label map and superimpose it on an image

Results

Input image

Input image

Label map

Label image

Output image

Output image

Code

C++

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkLabelObject.h"
#include "itkLabelMap.h"
#include "itkLabelImageToLabelMapFilter.h"
#include "itkLabelMapOverlayImageFilter.h"

int
main(int argc, char * argv[])
{
  if (argc != 4)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << " <InputFileName> <LabelMap> <OutputFileName>";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }

  const char * inputFileName = argv[1];
  const char * labelFileName = argv[2];
  const char * outputFileName = argv[3];

  constexpr unsigned int Dimension = 2;

  using PixelType = unsigned char;
  using ImageType = itk::Image<PixelType, Dimension>;


  using ReaderType = itk::ImageFileReader<ImageType>;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(inputFileName);

  ReaderType::Pointer labelReader = ReaderType::New();
  labelReader->SetFileName(labelFileName);


  using LabelType = PixelType;
  using LabelObjectType = itk::LabelObject<LabelType, Dimension>;
  using LabelMapType = itk::LabelMap<LabelObjectType>;

  using ConverterType = itk::LabelImageToLabelMapFilter<ImageType, LabelMapType>;
  ConverterType::Pointer converter = ConverterType::New();
  converter->SetInput(labelReader->GetOutput());

  using FilterType = itk::LabelMapOverlayImageFilter<LabelMapType, ImageType>;
  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(converter->GetOutput());
  filter->SetFeatureImage(reader->GetOutput());
  filter->SetOpacity(0.5);


  using WriterType = itk::ImageFileWriter<FilterType::OutputImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputFileName);
  writer->SetInput(filter->GetOutput());
  try
  {
    writer->Update();
  }
  catch (itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Python

#!/usr/bin/env python
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)

Classes demonstrated