Iterate Image Starting At Seed

Warning

Fix Problem Contains problems not fixed from original wiki.

Synopsis

Iterate over an image starting at a seed and following a rule for connectivity decisions.

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 "itkImageFileWriter.h"
#include "itkFloodFilledImageFunctionConditionalIterator.h"
#include "itkBinaryThresholdImageFunction.h"
#include "itkImageFileWriter.h"

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

static void
CreateImage(ImageType::Pointer image);

int
main(int /*argc*/, char * /*argv*/[])
{
  ImageType::Pointer image = ImageType::New();
  CreateImage(image);

  using FunctionType = itk::BinaryThresholdImageFunction<ImageType, double>;
  FunctionType::Pointer function = FunctionType::New();
  function->SetInputImage(image);
  function->ThresholdAbove(100); // we are looking to capture 255

  using IteratorType = itk::FloodFilledImageFunctionConditionalIterator<ImageType, FunctionType>;

  itk::Index<2> seed;
  seed[0] = 25;
  seed[1] = 25;

  std::vector<itk::Index<2>> seeds;
  seeds.push_back(seed);

  IteratorType it(image, function, seeds);
  it.GoToBegin();

  while (!it.IsAtEnd())
  {
    std::cout << it.GetIndex() << std::endl;
    ++it;
  }

  return EXIT_SUCCESS;
}

void
CreateImage(ImageType::Pointer 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);

  // The line doesn't work at the moment, because it needs 8-connectivity.

  // Make a line
  for (unsigned int i = 20; i < 50; ++i)
  {
    itk::Index<2> pixelIndex;
    pixelIndex.Fill(i);

    image->SetPixel(pixelIndex, 255);
  }

  // Make a square
  //   for(unsigned int r = 20; r < 50; r++)
  //     {
  //     for(unsigned int c = 20; c < 50; c++)
  //       {
  //       itk::Index<2> pixelIndex;
  //       pixelIndex[0] = r;
  //       pixelIndex[1] = c;
  //
  //       image->SetPixel(pixelIndex, 255);
  //       }
  //     }
}

Classes demonstrated