Negative index ?

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.

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.

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

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.

You can accomplish that with something like this:

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.

1 Like

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

1 Like