Signed 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 "itkSignedDanielssonDistanceMapImageFilter.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 SignedDanielssonDistanceMapImageFilterType =
    itk::SignedDanielssonDistanceMapImageFilter<UnsignedCharImageType, FloatImageType>;
  SignedDanielssonDistanceMapImageFilterType::Pointer distanceMapImageFilter =
    SignedDanielssonDistanceMapImageFilterType::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 Danielsson 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, typename TVoronoiImage = TInputImage>
class SignedDanielssonDistanceMapImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>

This filter computes the signed distance map of the input image as an approximation with pixel accuracy to the Euclidean distance.

This class is parameterized over the type of the input image and the type of the output image.

For purposes of evaluating the signed distance map, the input is assumed to be binary composed of pixels with value 0 and non-zero.

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

As a convention, the distance is evaluated from the boundary of the ON pixels.

The filter returns

  • A signed distance map with the approximation to the euclidean distance.

  • A voronoi partition. (See itkDanielssonDistanceMapImageFilter)

  • A vector map containing the component of the vector relating the current pixel with the closest point of the closest object to this pixel. Given that the components of the distance are computed in “pixels”, the vector is represented by an itk::Offset. That is, physical coordinates are not used. (See itkDanielssonDistanceMapImageFilter)

This filter internally uses the DanielssonDistanceMap filter. This filter is N-dimensional.

See

itkDanielssonDistanceMapImageFilter

ITK Sphinx Examples:

See itk::SignedDanielssonDistanceMapImageFilter for additional documentation.