# Coordinates of pixels in a mesh file

**URL:** <https://discourse.slicer.org/t/coordinates-of-pixels-in-a-mesh-file/28389>\
**Category:** Support\
**Created:** [March 14, 2023, 11:13pm UTC](https://discourse.slicer.org/t/coordinates-of-pixels-in-a-mesh-file/28389 "2023-03-14T23:13:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZSoltani](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zsoltani/32/17166_2.png) [@ZSoltani](https://discourse.slicer.org/u/ZSoltani)\
**Post date:** [March 14, 2023, 11:13pm UTC](https://discourse.slicer.org/t/coordinates-of-pixels-in-a-mesh-file/28389/1 "2023-03-14T23:13:34Z")

</div>

Hello everyone,

I’m trying to get the coordinates of each voxel in my model through the following code:

volumeNode=slicer.util.getNode(“myfile.nrrd”)  
ijkToRas = vtk.vtkMatrix4x4()  
imageData=volumeNode.GetImageData()  
extent = imageData.GetExtent()  
for k in range(extent[4], extent[5]+1):  
for j in range(extent[2], extent[3]+1):  
for i in range(extent[0], extent[1]+1):  
position\_Ijk=[i, j, k, 1]  
position\_Ras=ijkToRas.MultiplyPoint(position\_Ijk)  
print(position\_Ras)

The position\_Ras are in the range of (-61.955299377441435, -57.42440032958985, 2.3572499752044687, 1.0) to (-13.861799377441436, -19.522900329589852, 21.785749975204467, 1.0) with Spacing: [0.3185, 0.3185, 0.3185] and Dimensions: (151, 119, 61).

So far, everything looks fine. However, when I look at my mesh generated using exported stl file in Gmsh, the node coordinates are in the range of (15.73733139, 21.27777481, 2.123630524) to (61.77390289, 56.87292862, 21.68377686).

Does anyone know how I can correlate these two coordinates? I need to specify which voxels are inside of an element in my mesh.

Thank you for your help,  
Zahra

---

<div class="post-metadata">

**Author:** ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)\
**Post date:** [March 15, 2023, 5:53am UTC](https://discourse.slicer.org/t/coordinates-of-pixels-in-a-mesh-file/28389/2 "2023-03-15T05:53:29Z")

</div>

Hello @ZSoltani, the `ijkToRas` matrix does not seem to be set properly (it is only set to default values of the `vtkMatri4x4` at creation). `vtkMRMLVolumeNode` has the `GetIJKToRASMatrix()` that you can use to populate the `ijkToRas` matrix.

If I understand correctly, you are comparing the extent of an image with the extent of a 3D surface model derived from the image. Most likely, the model does not occupy the whole volume of the image, but a subvolume of it, so your values are probably not going to match.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 15, 2023, 3:47pm UTC](https://discourse.slicer.org/t/coordinates-of-pixels-in-a-mesh-file/28389/3 "2023-03-15T15:47:16Z")

</div>

Also note that coordinates in all files (image files, mesh files, etc) are stored in LPS coordinate system by default. In Slicer, coordinates are in RAS coordinate system.

---

<div class="post-metadata">

**Author:** ![ZSoltani](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zsoltani/32/17166_2.png) [@ZSoltani](https://discourse.slicer.org/u/ZSoltani)\
**Post date:** [March 15, 2023, 4:18pm UTC](https://discourse.slicer.org/t/coordinates-of-pixels-in-a-mesh-file/28389/4 "2023-03-15T16:18:02Z")

</div>

Thank you @RafaelPalomar and @lassoan for your replies.

Using volumeArray = slicer.util.array(“my-stl-file.stl”) I can get the same node coordinates as what I get from Gmsh. The only difference is that x and y coordinates have opposite signs in 3Dslicer and Gmsh and z coordinates are exactly the same. Is it a general rule? And if not, how I can transfer between two coordinate systems for a general stl file? Is it like they have different origins?

My second question, is about finding the coordinates of voxels inside the stl model. Sorry I’m new to 3Dslicer, and not sure how I can apply your comment correctly. Could you please explain with a short script how to do that?

Thank you,  
Zahra

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [March 15, 2023, 4:21pm UTC](https://discourse.slicer.org/t/coordinates-of-pixels-in-a-mesh-file/28389/5 "2023-03-15T16:21:25Z")

</div>

Here’s a description of LPS/RAS: [Coordinate systems - Slicer Wiki](https://www.slicer.org/wiki/Coordinate_systems)

Yes, it’s just a matter of knowing the convention of the software you are using and flipping the signs on the first two coordinates to convert.

For your second question these scripts should help: [https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#get-volume-voxel-coordinates-from-markup-control-point-ras-coordinates](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-volume-voxel-coordinates-from-markup-control-point-ras-coordinates)

---

<div class="post-metadata">

**Author:** ![ZSoltani](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zsoltani/32/17166_2.png) [@ZSoltani](https://discourse.slicer.org/u/ZSoltani)\
**Post date:** [March 15, 2023, 4:26pm UTC](https://discourse.slicer.org/t/coordinates-of-pixels-in-a-mesh-file/28389/6 "2023-03-15T16:26:38Z")

</div>

Thank you Steve for your help.
