FYI, Last weekend, @matt.mccormick merged the itk::Experimental::IndexRange
template class that I proposed, making it available for ITK 5.0
IndexRange
is a modern C++ range of iterators. It should ease iterating over a sequence of consecutive Ndimensional indices, as it can be used as a rangeexpression of a C++11 rangebased forloop:
for (const Index<Dimension>& index : indexRange)
// Use the 'index' here...!
The order in which the indices are iterated corresponds to the order in which pixels that are stored in a regular itk::Image
.
There are two IndexRange
type aliases:

ZeroBasedIndexRange<Dimension>
has its begin iterator at index zero (thatâs [0, 0] in 2D, or [0, 0, 0] in 3D). The range is specified byitk::Size<Dimension>
, which is typically the Ndimensional size of an image. 
ImageRegionIndexRange<Dimension>
can begin at any arbitrary index, as it can be specified by anitk::ImageRegion<Dimension>
. (itk::ImageRegion<Dimension>
consists of an Ndimensional index and an Ndimensional size.)
The iterators of IndexRange
are bidirectional, as they support both forward iteration (++it
) and backward iteration (it
). When using ZeroBasedIndexRange
, it appears fastest to iterate backward, from the end to the begin of the range:
const ZeroBasedIndexRange<Dimension> range{ imageSize };
const auto begin = range.begin();
auto it = range.end();
while (it != begin)
{
it;
const auto& index = *it;
// Use the 'index' here...!
}
However, I think that in most cases, the rangebased forloop is fast enough