# Convert between RAS and numpy array indices

**URL:** <https://discourse.slicer.org/t/convert-between-ras-and-numpy-array-indices/19408>\
**Category:** Development\
**Created:** [August 30, 2021, 12:05am UTC](https://discourse.slicer.org/t/convert-between-ras-and-numpy-array-indices/19408 "2021-08-30T00:05:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![M.Odaba](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@M.Odaba](https://discourse.slicer.org/u/M.Odaba)\
**Post date:** [August 30, 2021, 12:05am UTC](https://discourse.slicer.org/t/convert-between-ras-and-numpy-array-indices/19408/1 "2021-08-30T00:05:46Z")

</div>

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](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository/volumes.html?highlight=coordinate#get-volume-voxel-coordinates-from-markup-fiducial-ras-coordinates), but have not been able to figure out the right code.

Thanks,  
Mostafa

---

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [August 31, 2021, 8:04pm UTC](https://discourse.slicer.org/t/convert-between-ras-and-numpy-array-indices/19408/2 "2021-08-31T20:04:39Z")

</div>

First, download MRHead data from Sample Data module

```auto
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.
