Iterative Hole Filling

Synopsis

Fill hole or cavity in one itk::Image

Results

Input image

Input image

Output image

Output image

Code

C++

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkVotingBinaryIterativeHoleFillingImageFilter.h"

int
main(int argc, char * argv[])
{
  if (argc != 6)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << " <InputFileName> <OutputFileName> <radius> <majority threshold> <number of iterations>";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }

  const char * inputFileName = argv[1];
  const char * outputFileName = argv[2];

  int          r = std::stoi(argv[3]);
  int          majorityThreshold = std::stoi(argv[4]);
  unsigned int numberOfIterations = std::stoi(argv[5]);

  constexpr unsigned int Dimension = 2;

  using PixelType = unsigned char;
  using ImageType = itk::Image<PixelType, Dimension>;

  using ReaderType = itk::ImageFileReader<ImageType>;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(inputFileName);

  using FilterType = itk::VotingBinaryIterativeHoleFillingImageFilter<ImageType>;
  FilterType::InputSizeType radius;
  radius.Fill(r);

  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(reader->GetOutput());
  filter->SetRadius(radius);
  filter->SetMajorityThreshold(majorityThreshold);
  filter->SetBackgroundValue(itk::NumericTraits<PixelType>::Zero);
  filter->SetForegroundValue(itk::NumericTraits<PixelType>::max());
  filter->SetMaximumNumberOfIterations(numberOfIterations);

  using WriterType = itk::ImageFileWriter<ImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputFileName);
  writer->SetInput(filter->GetOutput());
  try
  {
    writer->Update();
  }
  catch (itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated

template<typename TImage>
class VotingBinaryIterativeHoleFillingImageFilter : public itk::ImageToImageFilter<TImage, TImage>

Fills in holes and cavities by iteratively applying a voting operation.

This filter uses internally the VotingBinaryHoleFillingImageFilter, and runs it iteratively until no pixels are being changed or until it reaches the maximum number of iterations. The purpose of the filter is to fill in holes of medium size (tens of pixels in radius). In principle the number of iterations is related to the size of the holes to be filled in. The larger the holes, the more iteration must be run with this filter in order to fill in the full hole. The size of the neighborhood is also related to the curvature of the hole borders and therefore the hole size. Note that as a collateral effect this filter may also fill in cavities in the external side of structures.

This filter is templated over a single image type because the output image type must be the same as the input image type. This is required in order to make the iterations possible, since the output image of one iteration is taken as the input image for the next iteration.

See

Image

See

VotingBinaryImageFilter

See

VotingBinaryHoleFillingImageFilter

See

Neighborhood

See

NeighborhoodOperator

See

NeighborhoodIterator

See itk::VotingBinaryIterativeHoleFillingImageFilter for additional documentation.