Add Offset To Index

Synopsis

Add an offset to a pixel index

Results

Output:
index: [5, 5]
offset: [1, 1]
index + offset: [6, 6]
index: [5, 5]
offset: [-1, 1]
index + offset: [4, 6]

Code

C++

#include "itkIndex.h"
#include "itkOffset.h"

#include <iostream>

int
main(int, char *[])
{
  constexpr unsigned int Dimension = 2;

  itk::Index<Dimension> index;
  index.Fill(5);

  itk::Offset<Dimension> offset;
  offset.Fill(1);

  std::cout << "index: " << index << std::endl;
  std::cout << "offset: " << offset << std::endl;
  std::cout << "index + offset: " << index + offset << std::endl;
  std::cout << std::endl;

  offset[0] = -1;

  std::cout << "index: " << index << std::endl;
  std::cout << "offset: " << offset << std::endl;
  std::cout << "index + offset: " << index + offset << std::endl;
  std::cout << std::endl;

  return EXIT_SUCCESS;
}

Python

#!/usr/bin/env python
import itk

from distutils.version import StrictVersion as VS
if VS(itk.Version.GetITKVersion()) < VS("4.9.0"):
    print("ITK 4.9.0 is required.")
    sys.exit(1)

Dimension = 2

index = itk.Index[Dimension]()
index.Fill(5)

offset = itk.Offset[Dimension]()
offset.Fill(1)

newIndex = index + offset

print("index: " + str([int(index[0]), int(index[1])]))
print("offset: " + str([int(offset[0]), int(offset[1])]))
print("index + offset: " + str([int(newIndex[0]), int(newIndex[1])]))
print("")

offset[0] = -1
newIndex = index + offset

print("index: " + str([int(index[0]), int(index[1])]))
print("offset: " + str([int(offset[0]), int(offset[1])]))
print("index + offset: " + str([int(newIndex[0]), int(newIndex[1])]))
print("")

Classes demonstrated