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 "itkImageRandomNonRepeatingConstIteratorWithIndex.h"

int
main()
{
  using ImageType = itk::Image<unsigned char, 2>;
  auto 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#

template<typename TImage>
class ImageRandomNonRepeatingConstIteratorWithIndex : public itk::ImageConstIteratorWithIndex<TImage>

A multi-dimensional image iterator that visits a random set of pixels within an image region. All pixels in the image will be visited before any are repeated. A priority image may be passed to the iterator which will cause it to select certain sets of pixels (those with lower priority values) before others.

This class was contributed by Rupert Brooks, McGill Centre for Intelligent Machines, Montreal, Canada. It is heavily based on the ImageRandomIterator class.

ImageRandomNonRepeatingConstIteratorWithIndex is a multi-dimensional iterator class that is templated over image type. ImageRandomNonRepeatingConstIteratorWithIndex is constrained to walk only within the specified region. When first instantiated, it creates (and stores) a random permutation of the image pixels. It then visits each pixel in the order specified by the permutation. Thus, iterator++ followed by iterator will end up leaving the iterator pointing at the same pixel. Furthermore, iterating from beginning to end will cover each pixel in the region exactly once.

This iterator can be passed an image the same size as the region, which specifies a priority for the pixels. Within areas of this priority image that have the same value, the pixel selection will be random. Otherwise the pixel selection will be in the order of the priority image. In the extreme, this allows the order of the pixel selection to be completely specified.

ImageRandomNonRepeatingConstIteratorWithIndex assumes a particular layout of the image data. The is arranged in a 1D array as if it were [][][][slice][row][col] with Index[0] = col, Index[1] = row, Index[2] = slice, etc.

MORE INFORMATION

For a complete description of the ITK Image Iterators and their API, please see the Iterators chapter in the ITK Software Guide. The ITK Software Guide is available in print and as a free .pdf download from https://www.itk.org.

Author

Rupert Brooks, McGill Centre for Intelligent Machines. Canada

See

ImageConstIterator

See

ConditionalConstIterator

See

ConstNeighborhoodIterator

See

ConstShapedNeighborhoodIterator

See

ConstSliceIterator

See

CorrespondenceDataStructureIterator

See

FloodFilledFunctionConditionalConstIterator

See

FloodFilledImageFunctionConditionalConstIterator

See

FloodFilledImageFunctionConditionalIterator

See

FloodFilledSpatialFunctionConditionalConstIterator

See

FloodFilledSpatialFunctionConditionalIterator

See

ImageConstIterator

See

ImageConstIteratorWithIndex

See

ImageIterator

See

ImageIteratorWithIndex

See

ImageLinearConstIteratorWithIndex

See

ImageLinearIteratorWithIndex

See

ImageRandomNonRepeatingConstIteratorWithIndex

See

ImageRandomIteratorWithIndex

See

ImageRegionConstIterator

See

ImageRegionConstIteratorWithIndex

See

ImageRegionExclusionConstIteratorWithIndex

See

ImageRegionExclusionIteratorWithIndex

See

ImageRegionIterator

See

ImageRegionIteratorWithIndex

See

ImageRegionReverseConstIterator

See

ImageRegionReverseIterator

See

ImageReverseConstIterator

See

ImageReverseIterator

See

ImageSliceConstIteratorWithIndex

See

ImageSliceIteratorWithIndex

See

NeighborhoodIterator

See

PathConstIterator

See

PathIterator

See

ShapedNeighborhoodIterator

See

SliceIterator

See

ImageConstIteratorWithIndex

ITK Sphinx Examples:

Subclassed by itk::ImageRandomNonRepeatingIteratorWithIndex< TImage >

See itk::ImageRandomNonRepeatingConstIteratorWithIndex for additional documentation.