Create a Index#

Synopsis#

Create a itk::Index, which represents a pixel location in an itk::Image.

Results#

Output::

[0, 0] [1, 2]

Code#

C++#

#include "itkIndex.h"

int
main()
{
  constexpr unsigned int Dimension = 2;

  using IndexType = itk::Index<Dimension>;

  IndexType index;

  // Method 1
  // set both index[0] and index[1] to the same value (in this case, 0).
  index.Fill(0);
  std::cout << index << std::endl;

  // Method 2
  // set each component of the index individually.
  index[0] = 1;
  index[1] = 2;

  std::cout << index << std::endl;

  return EXIT_SUCCESS;
}

Python#

#!/usr/bin/env python

import itk

Dimension = 2

index = itk.Index[Dimension]()

# Method 1
# set both index[0] and index[1] to the same value (in this case, 0).
index.Fill(0)
print(index)

# Method 2
# set each component of the index individually.
index[0] = 1
index[1] = 2
print(index)

Classes demonstrated#

template<unsigned int VDimension = 2>
struct Index

Represent a n-dimensional index in a n-dimensional image.

Index is a templated class to represent a multi-dimensional index, i.e. (i,j,k,…). Index is templated over the dimension of the index. ITK assumes the first element of an index is the fastest moving index.

For efficiency sake, Index does not define a default constructor, a copy constructor, or an operator=. We rely on the compiler to provide efficient bitwise copies.

Index is an “aggregate” class. Its data is public (m_InternalArray) allowing for fast and convenient instantiations/assignments.

The following syntax for assigning an aggregate type like this is allowed/suggested:

Index<3> var{{ 256, 256, 20 }}; // Also prevent narrowing conversions Index<3> var = {{ 256, 256, 20 }};

The doubled braces {{ and }} are required to prevent gcc -Wall (and perhaps other compilers) from complaining about a partly bracketed initializer.

As an aggregate type that is intended to provide highest performance characteristics, this class is not appropriate to inherit from, so setting this struct as final.

ITK Sphinx Examples:

See itk::Index for additional documentation.