Prune Binary Image#

Synopsis#

Prune a binary image.

Results#

../../../../_images/PruneBinaryImageQuickView.png

Output In VTK Window#

Code#

C++#

#include "itkImage.h"
#include "itkBinaryPruningImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkBinaryBallStructuringElement.h"

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

template <typename TImage>
void
CreateImage(TImage * const image);

int
main(int argc, char * argv[])
{
  //   std::cerr << "Usage: " << std::endl;
  //   std::cerr << argv[0] << " InputImageFile OutputImageFile [iteration]" << std::endl;

  using ImageType = itk::Image<unsigned char, 2>;
  ImageType::Pointer image;
  std::string        outputFilename = "Output.png";
  unsigned int       iteration = 1;

  if (argc >= 4)
  {
    image = itk::ReadImage<ImageType>(argv[1]);

    std::stringstream ssIteration(argv[2]);
    ssIteration >> iteration;

    outputFilename = argv[3];
  }
  else
  {
    image = ImageType::New();
    CreateImage(image.GetPointer());
  }

  std::cout << "Iterations: " << iteration << std::endl;

  using BinaryPruningImageFilterType = itk::BinaryPruningImageFilter<ImageType, ImageType>;
  auto pruneFilter = BinaryPruningImageFilterType::New();
  pruneFilter->SetInput(image);
  pruneFilter->SetIteration(iteration);
  pruneFilter->GetOutput();

#ifdef ENABLE_QUICKVIEW
  QuickView viewer;
  viewer.AddImage(image.GetPointer());
  viewer.AddImage(pruneFilter->GetOutput());
  viewer.Visualize();
#endif

  itk::WriteImage(pruneFilter->GetOutput(), outputFilename);

  return EXIT_SUCCESS;
}

template <typename TImage>
void
CreateImage(TImage * const image)
{
  // This function creates a 2D image consisting of a black background,
  // a large square of a non-zero pixel value, and a single "erroneous" pixel
  // near the square.
  typename TImage::IndexType corner = { { 0, 0 } };

  typename TImage::SizeType size = { { 200, 200 } };

  typename TImage::RegionType region(corner, size);

  image->SetRegions(region);
  image->Allocate(true);

  // Make a square
  for (int r = 40; r < 100; ++r)
  {
    for (int c = 40; c < 100; ++c)
    {
      typename TImage::IndexType pixelIndex = { { r, c } };
      image->SetPixel(pixelIndex, 50);
    }
  }

  typename TImage::IndexType pixelIndex = { { 102, 102 } };

  image->SetPixel(pixelIndex, 50);
}

Classes demonstrated#

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

This filter removes “spurs” of less than a certain length in the input image.

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

The input is assumed to be a binary image.

This filter is a sequential pruning algorithm and known to be computational time dependable of the image size. The algorithm is the N-dimensional version of that given for two dimensions in:

Rafael C. Gonzales and Richard E. Woods. Digital Image Processing. Addison Wesley, 491-494, (1993).

See

MorphologyImageFilter

See

BinaryErodeImageFilter

See

BinaryDilateImageFilter

See

BinaryThinningImageFilter

ITK Sphinx Examples:

See itk::BinaryPruningImageFilter for additional documentation.