Maurer Distance Map of Binary Image

Synopsis

Compute a distance map from objects in a binary image.

Results

Note

Help Wanted Implementation of Results for sphinx examples containing this message. Reconfiguration of CMakeList.txt may be necessary. Write An Example <https://itk.org/ITKExamples/Documentation/Contribute/WriteANewExample.html>

Code

C++

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkSignedMaurerDistanceMapImageFilter.h"

#include "itksys/SystemTools.hxx"
#include <sstream>

#ifdef ENABLE_QUICKVIEW
#  include "QuickView.h"
#endif

using UnsignedCharImageType = itk::Image<unsigned char, 2>;
using FloatImageType = itk::Image<float, 2>;

static void
CreateImage(UnsignedCharImageType::Pointer image);

int
main(int argc, char * argv[])
{
  auto image = UnsignedCharImageType::New();
  if (argc < 2)
  {
    CreateImage(image);
  }
  else
  {
    image = itk::ReadImage<UnsignedCharImageType>(argv[1]);
  }

  using SignedMaurerDistanceMapImageFilterType =
    itk::SignedMaurerDistanceMapImageFilter<UnsignedCharImageType, FloatImageType>;
  SignedMaurerDistanceMapImageFilterType::Pointer distanceMapImageFilter =
    SignedMaurerDistanceMapImageFilterType::New();
  distanceMapImageFilter->SetInput(image);

#ifdef ENABLE_QUICKVIEW
  QuickView viewer;
  viewer.AddImage(
    image.GetPointer(), true, argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated image");

  std::stringstream desc;
  desc << "Signed Maurer Distance";
  viewer.AddImage(distanceMapImageFilter->GetOutput(), true, desc.str());

  viewer.Visualize();
#endif

  return EXIT_SUCCESS;
}


void
CreateImage(UnsignedCharImageType::Pointer image)
{
  // Create an image
  itk::Index<2> start;
  start.Fill(0);

  itk::Size<2> size;
  size.Fill(100);

  itk::ImageRegion<2> region(start, size);
  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(0);

  // Create a line of white pixels
  for (unsigned int i = 40; i < 60; ++i)
  {
    itk::Index<2> pixel;
    pixel.Fill(i);
    image->SetPixel(pixel, 255);
  }
}

Classes demonstrated

template<typename TInputImage, typename TOutputImage>
class SignedMaurerDistanceMapImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>

This filter calculates the Euclidean distance transform of a binary image in linear time for arbitrary dimensions.

The inside is considered as having negative distances. Outside is treated as having positive distances. To change the convention, use the InsideIsPositive(bool) function.

Inputs and Outputs

This is an image-to-image filter. The dimensionality is arbitrary. The only dimensionality constraint is that the input and output images be of the same dimensions and size. To maintain integer arithmetic within the filter, the default output is the signed squared distance. This implies that the input image should be of type “unsigned int” or “int” whereas the output image is of type “int”. Obviously, if the user wishes to utilize the image spacing or to have a filter with the Euclidean distance (as opposed to the squared distance), output image types of float or double should be used.

Reference: C. R. Maurer, Jr., R. Qi, and V. Raghavan, “A Linear Time Algorithm for Computing Exact Euclidean Distance Transforms of Binary Images in Arbitrary Dimensions”, IEEE - Transactions on Pattern Analysis and Machine Intelligence, 25(2): 265-270, 2003.

Parameters

Set/GetBackgroundValue specifies the background of the value of the input binary image. Normally this is zero and, as such, zero is the default value. Other than that, the usage is completely analogous to the itk::DanielssonDistanceImageFilter class except it does not return the Voronoi map.

ITK Sphinx Examples:

See itk::SignedMaurerDistanceMapImageFilter for additional documentation.