# Negative index ?

**URL:** https://discourse.itk.org/t/negative-index/7333
**Category:** Beginner Questions
**Created:** [November 29, 2024, 5:03pm UTC](https://discourse.itk.org/t/negative-index/7333 "2024-11-29T17:03:10Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Oodini](https://discourse.itk.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.itk.org/u/Oodini)
#### Post date: [November 29, 2024, 5:03pm UTC](https://discourse.itk.org/t/negative-index/7333/1 "2024-11-29T17:03:10Z")

</div>

Hello,

I’d like to set the position (0,0) at the center of an image. That would imply using negative indexes.

Is it possible (that could be: the `[]` operator may be overloaded) ?

Thanks.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 29, 2024, 5:18pm UTC](https://discourse.itk.org/t/negative-index/7333/2 "2024-11-29T17:18:24Z")

</div>

ITK does support negative indices - indices are signed, sizes are unsigned. But that is not necessary - you can set `origin` to negative values to have your image centered on the origin of the physical coordinate system.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 29, 2024, 5:18pm UTC](https://discourse.itk.org/t/negative-index/7333/3 "2024-11-29T17:18:55Z")

</div>

Origin corresponds to the position of voxel with index (0,0,0).

---

<div class="post-metadata">

### Author: ![Oodini](https://discourse.itk.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.itk.org/u/Oodini)
#### Post date: [November 29, 2024, 6:33pm UTC](https://discourse.itk.org/t/negative-index/7333/4 "2024-11-29T18:33:06Z")

</div>

It isn’t related to physical coordinate system.  
I want to run an algorithm from the center, and it would be simple to understand in the code if the origin (in pixel) is at the center.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 29, 2024, 7:46pm UTC](https://discourse.itk.org/t/negative-index/7333/5 "2024-11-29T19:46:41Z")

</div>

You can accomplish that with something like this:

```cpp
auto image0 = ImageType::New();
auto origRegion = image->GetLargestPossibleRegion();
image0.SetRegions(origRegion);
image0.CopyInformation(image);
image0.SetPixelContainer(image->GetPixelContainer) // shallow copy pixel buffer

// now adjust it so center index is 0
ImageType::IndexType centerIndex = ...; // half the size
auto centerPoint = image->TransformIndexToPhysicalPoint(centerIndex);
auto region = origRegion;
centerIndex *= -1;
region.SetIndex(centerIndex);
image0->SetRegions(region);
image0->SetOrigin(centerPoint);

```

For some operations (e.g. halving the size, inverting center index coordinates) you will probably have to do it in a for loop over dimension, the above will probably not compile.

---

<div class="post-metadata">

### Author: ![Oodini](https://discourse.itk.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.itk.org/u/Oodini)
#### Post date: [December 2, 2024, 9:55am UTC](https://discourse.itk.org/t/negative-index/7333/6 "2024-12-02T09:55:20Z")

</div>

Well…  
As I am beginning with ITK, maybe I’ll avoid this hack for now. Thanks.

---

<div class="post-metadata">

### Author: ![roberrttt](https://discourse.itk.org/user_avatar/discourse.itk.org/roberrttt/32/4431_2.png) [@roberrttt](https://discourse.itk.org/u/roberrttt)
#### Post date: [December 17, 2024, 4:09am UTC](https://discourse.itk.org/t/negative-index/7333/7 "2024-12-17T04:09:27Z")

</div>

This sounds amazing can you pls share more details about this?
