Convert between RAS and numpy array indices

Hi Iassoan,

Would you please explain how to convert the coordinates to world coordinate system (X, Y, Z)? Would you be able to provide the script that convert these coordinates (coordinates = np.where(labelArray==labelValue)) which I think ther are in IJK format, to RAS (X, Y, Z)?
I know that vtk.vtkMatrix4x4() will do the job and I’ve already looked into some examples such as Volumes — 3D Slicer documentation, but have not been able to figure out the right code.

Thanks,
Mostafa

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