Create Distance Map From Seeds#

Synopsis#

Create a distance map from given seeds

Results#

Code#

C++#

#include "itkFastMarchingImageToNodePairContainerAdaptor.h"
#include "itkFastMarchingImageFilterBase.h"
#include "itkFastMarchingThresholdStoppingCriterion.h"
#include "itkImageFileWriter.h"

int
main(int argc, char * argv[])
{
  if (argc != 2)
  {
    std::cerr << "Usage:" << std::endl;
    std::cerr << argv[0] << " <OutputFileName>" << std::endl;

    return EXIT_FAILURE;
  }

  // create a fastmarching object
  using PixelType = float;
  constexpr unsigned int Dimension = 2;

  using FloatImageType = itk::Image<PixelType, Dimension>;

  using CriterionType = itk::FastMarchingThresholdStoppingCriterion<FloatImageType, FloatImageType>;

  using FastMarchingType = itk::FastMarchingImageFilterBase<FloatImageType, FloatImageType>;

  auto criterion = CriterionType::New();
  criterion->SetThreshold(100.);

  auto marcher = FastMarchingType::New();
  marcher->SetStoppingCriterion(criterion);

  // specify the size of the output image
  FloatImageType::SizeType size = { { 64, 64 } };
  marcher->SetOutputSize(size);

  // setup a speed image of ones
  auto speedImage = FloatImageType::New();

  FloatImageType::RegionType region;
  region.SetSize(size);

  speedImage->SetLargestPossibleRegion(region);
  speedImage->SetBufferedRegion(region);
  speedImage->Allocate();
  speedImage->FillBuffer(1.0);

  // setup a 'alive image'
  auto AliveImage = FloatImageType::New();
  AliveImage->SetLargestPossibleRegion(region);
  AliveImage->SetBufferedRegion(region);
  AliveImage->Allocate();
  AliveImage->FillBuffer(0.0);

  FloatImageType::OffsetType offset0 = { { 28, 35 } };

  FloatImageType::IndexType index;
  index.Fill(0);
  index += offset0;

  AliveImage->SetPixel(index, 1.0);

  // setup a 'trial image'
  auto TrialImage = FloatImageType::New();
  TrialImage->SetLargestPossibleRegion(region);
  TrialImage->SetBufferedRegion(region);
  TrialImage->Allocate();
  TrialImage->FillBuffer(0.0);

  index[0] += 1;
  TrialImage->SetPixel(index, 1.0);

  index[0] -= 1;
  index[1] += 1;
  TrialImage->SetPixel(index, 1.0);

  index[0] -= 1;
  index[1] -= 1;
  TrialImage->SetPixel(index, 1.0);

  index[0] += 1;
  index[1] -= 1;
  TrialImage->SetPixel(index, 1.0);

  marcher->SetInput(speedImage);

  using AdaptorType = itk::FastMarchingImageToNodePairContainerAdaptor<FloatImageType, FloatImageType, FloatImageType>;

  auto adaptor = AdaptorType::New();

  adaptor->SetAliveImage(AliveImage.GetPointer());
  adaptor->SetAliveValue(0.0);

  adaptor->SetTrialImage(TrialImage.GetPointer());
  adaptor->SetTrialValue(1.0);
  adaptor->Update();

  marcher->SetAlivePoints(adaptor->GetAlivePoints());
  marcher->SetTrialPoints(adaptor->GetTrialPoints());

  try
  {
    itk::WriteImage(marcher->GetOutput(), argv[1]);
  }
  catch (const itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInput, typename TOutput>
class FastMarchingImageFilterBase : public itk::FastMarchingBase<TInput, TOutput>

Apply the Fast Marching method to solve an Eikonal equation on an image.

The speed function can be specified as a speed image or a speed constant. The speed image is set using the method SetInput(). If the speed image is nullptr, a constant speed function is used and is specified using method the SetSpeedConstant().

If the speed function is constant and of value one, fast marching results is an approximate distance function from the initial alive points.

There are two ways to specify the output image information (LargestPossibleRegion, Spacing, Origin):

  • it is copied directly from the input speed image

  • it is specified by the user. Default values are used if the user does not specify all the information.

The output information is computed as follows.

If the speed image is nullptr or if the OverrideOutputInformation is set to true, the output information is set from user specified parameters. These parameters can be specified using methods

  • FastMarchingImageFilterBase::SetOutputRegion(),

  • FastMarchingImageFilterBase::SetOutputSpacing(),

  • FastMarchingImageFilterBase::SetOutputDirection(),

  • FastMarchingImageFilterBase::SetOutputOrigin().

Else the output information is copied from the input speed image.

Implementation of this class is based on Chapter 8 of “Level Set Methods and Fast Marching Methods”, J.A. Sethian, Cambridge Press, Second edition, 1999.

For an alternative implementation, see itk::FastMarchingImageFilter.

See

FastMarchingImageFilter

See

ImageFastMarchingTraits

See

ImageFastMarchingTraits2

Template Parameters
  • TTraits: traits

Subclassed by itk::FastMarchingExtensionImageFilterBase< TInput, TOutput, TAuxValue, VAuxDimension >, itk::FastMarchingUpwindGradientImageFilterBase< TInput, TOutput >

See itk::FastMarchingImageFilterBase for additional documentation.