Approximate Distance Map of Binary Image#

Synopsis#

Compute the 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 "itkApproximateSignedDistanceMapImageFilter.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 ApproximateSignedDistanceMapImageFilterType =
    itk::ApproximateSignedDistanceMapImageFilter<UnsignedCharImageType, FloatImageType>;
  ApproximateSignedDistanceMapImageFilterType::Pointer approximateSignedDistanceMapImageFilter =
    ApproximateSignedDistanceMapImageFilterType::New();
  approximateSignedDistanceMapImageFilter->SetInput(image);
  approximateSignedDistanceMapImageFilter->SetInsideValue(255);
  approximateSignedDistanceMapImageFilter->SetOutsideValue(0);

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

  std::stringstream desc;
  desc << "Approximate Signed Distance";
  viewer.AddImage(approximateSignedDistanceMapImageFilter->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 ApproximateSignedDistanceMapImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>

Create a map of the approximate signed distance from the boundaries of a binary image.

The ApproximateSignedDistanceMapImageFilter takes as input a binary image and produces a signed distance map. Each pixel value in the output contains the approximate distance from that pixel to the nearest “object” in the binary image. This filter differs from the DanielssonDistanceMapImageFilter in that it calculates the distance to the “object edge” for pixels within the object.

Negative values in the output indicate that the pixel at that position is within an object in the input image. The absolute value of a negative pixel represents the approximate distance to the nearest object boundary pixel.

WARNING: This filter requires that the output type be floating-point. Otherwise internal calculations will not be performed to the appropriate precision, resulting in completely incorrect (read: zero-valued) output.

The distances computed by this filter are Chamfer distances, which are only an approximation to Euclidian distances, and are not as exact approximations as those calculated by the DanielssonDistanceMapImageFilter. On the other hand, this filter is faster.

This filter requires that an “inside value” and “outside value” be set as parameters. The “inside value” is the intensity value of the binary image which corresponds to objects, and the “outside value” is the intensity of the background. (A typical binary image often represents objects as black (0) and background as white (usually 255), or vice-versa.) Note that this filter is slightly faster if the inside value is less than the outside value. Otherwise an extra iteration through the image is required.

This filter uses the FastChamferDistanceImageFilter and the IsoContourDistanceImageFilter internally to perform the distance calculations.

See

DanielssonDistanceMapImageFilter

See

SignedDanielssonDistanceMapImageFilter

See

SignedMaurerDistanceMapImageFilter

See

FastChamferDistanceImageFilter

See

IsoContourDistanceImageFilter

Author

Zach Pincus

ITK Sphinx Examples:

See itk::ApproximateSignedDistanceMapImageFilter for additional documentation.