Random Select Pixel From Region Without Replacing

Synopsis

Randomly select pixels from a region of an image without replacement.

Results

Output:

[1, 2]
[1, 1]
[0, 2]
[2, 2]
[2, 1]
[2, 0]
[0, 0]
[0, 1]
[1, 0]

Code

C++

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageRandomNonRepeatingConstIteratorWithIndex.h"

int
main(int, char *[])
{
  using ImageType = itk::Image<unsigned char, 2>;
  ImageType::Pointer image = ImageType::New();

  ImageType::SizeType regionSize;
  regionSize.Fill(3);

  ImageType::IndexType regionIndex;
  regionIndex.Fill(0);

  ImageType::RegionType region(regionIndex, regionSize);

  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(0);

  itk::ImageRandomNonRepeatingConstIteratorWithIndex<ImageType> imageIterator(image, image->GetLargestPossibleRegion());
  imageIterator.SetNumberOfSamples(region.GetNumberOfPixels());

  imageIterator.GoToBegin();
  while (!imageIterator.IsAtEnd())
  {
    std::cout << imageIterator.GetIndex() << std::endl;

    ++imageIterator;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated