Color Boundaries of Labeled Regions#

Synopsis#

Color the boundaries of labeled regions in an image.

Results#

image.png

image.png#

output.png

output.png#

Code#

C++#

#include "itkBinaryImageToLabelMapFilter.h"
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
#include "itkLabelMapToLabelImageFilter.h"
#include "itkLabelMapContourOverlayImageFilter.h"
#include "itkRGBPixel.h"

namespace
{
using ImageType = itk::Image<unsigned char, 2>;
}

static void
CreateImage(ImageType::Pointer image);

int
main()
{
  auto image = ImageType::New();
  CreateImage(image);

  using BinaryImageToLabelMapFilterType = itk::BinaryImageToLabelMapFilter<ImageType>;
  auto binaryImageToLabelMapFilter = BinaryImageToLabelMapFilterType::New();
  binaryImageToLabelMapFilter->SetInput(image);
  binaryImageToLabelMapFilter->Update();

  using RGBPixelType = itk::RGBPixel<unsigned char>;
  using RGBImageType = itk::Image<RGBPixelType>;

  using LabelMapContourOverlayImageFilterType =
    itk::LabelMapContourOverlayImageFilter<BinaryImageToLabelMapFilterType::OutputImageType, ImageType, RGBImageType>;
  LabelMapContourOverlayImageFilterType::Pointer labelMapContourOverlayImageFilter =
    LabelMapContourOverlayImageFilterType::New();
  labelMapContourOverlayImageFilter->SetInput(binaryImageToLabelMapFilter->GetOutput());
  labelMapContourOverlayImageFilter->SetFeatureImage(image);
  labelMapContourOverlayImageFilter->SetOpacity(.5);
  labelMapContourOverlayImageFilter->Update();

  itk::WriteImage(labelMapContourOverlayImageFilter->GetOutput(), "output.png");

  return EXIT_SUCCESS;
}

void
CreateImage(ImageType::Pointer image)
{
  // Create a black image with a white square
  ImageType::IndexType start;
  start.Fill(0);

  ImageType::SizeType size;
  size.Fill(100);

  ImageType::RegionType region;
  region.SetSize(size);
  region.SetIndex(start);
  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(0);

  itk::ImageRegionIterator<ImageType> imageIterator(image, image->GetLargestPossibleRegion());

  // Make two squares
  while (!imageIterator.IsAtEnd())
  {
    if ((imageIterator.GetIndex()[0] > 5 && imageIterator.GetIndex()[0] < 20) &&
        (imageIterator.GetIndex()[1] > 5 && imageIterator.GetIndex()[1] < 20))
    {
      imageIterator.Set(255);
    }

    if ((imageIterator.GetIndex()[0] > 50 && imageIterator.GetIndex()[0] < 70) &&
        (imageIterator.GetIndex()[1] > 50 && imageIterator.GetIndex()[1] < 70))
    {
      imageIterator.Set(255);
    }
    ++imageIterator;
  }

  itk::WriteImage(image, "image.png");
}

Classes demonstrated#

template<typename TLabelMap, typename TFeatureImage, typename TOutputImage = Image<RGBPixel<typename TFeatureImage::PixelType>, TFeatureImage::ImageDimension>>
class LabelMapContourOverlayImageFilter : public itk::LabelMapFilter<TLabelMap, TOutputImage>

Apply a colormap to the contours (outlines) of each object in a label map and superimpose it on top of the feature image.

The feature image is typically the image from which the labeling was produced. Use the SetInput function to set the LabelMap, and the SetFeatureImage function to set the feature image.

Apply a colormap to a label map and put it on top of the input image. The set of colors is a good selection of distinct colors. The opacity of the label map can be defined by the user. A background label produce a gray pixel with the same intensity than the input one.

This implementation was taken from the Insight Journal paper:

https://www.insight-journal.org/browse/publication/176
Author

Gaetan Lehmann. Biologie du Developpement et de la Reproduction, INRA de Jouy-en-Josas, France.

See

LabelMapOverlayImageFilter, LabelOverlayImageFilter, LabelOverlayFunctor

See

LabelMapToBinaryImageFilter, LabelMapToLabelImageFilter,

ITK Sphinx Examples:

See itk::LabelMapContourOverlayImageFilter for additional documentation.