Convert between RAS and numpy array indices

First, download MRHead data from Sample Data module

import numpy as np
volumeArray = slicer.util.array("MRHead")  # Get the image data in numpy array format
volumeArray[60, 127, 150] = 0  # Make one voxel black, numpy array is in KJI, not IJK.
volumeNode = slicer.mrmlScene.GetFirstNodeByName("MRHead")
volumeNode.Modified()  # I think this is needed to let Slicer know the image was modified.
ijkToRasMatrix = vtk.vtkMatrix4x4()
volumeNode.GetIJKToRASMatrix(ijkToRasMatrix)
ijkPoint = np.array([150, 127, 60])  # This is IJK, opposite order compared to KJI.
rasPoint = ijkToRasMatrix.MultiplyFloatPoint(np.append(ijkPoint, 1))
print(rasPoint)

If you hover your mouse over the new black point in the image, Data Probe should show the same coordinates that were printed at the end of this script.

2 Likes