# How to get the RAS coordinates of the pixel in a slice view by using python code

**URL:** <https://discourse.slicer.org/t/how-to-get-the-ras-coordinates-of-the-pixel-in-a-slice-view-by-using-python-code/14668>\
**Category:** Support\
**Created:** [November 18, 2020, 1:47am UTC](https://discourse.slicer.org/t/how-to-get-the-ras-coordinates-of-the-pixel-in-a-slice-view-by-using-python-code/14668 "2020-11-18T01:47:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Changing](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@Changing](https://discourse.slicer.org/u/Changing)\
**Post date:** [November 18, 2020, 1:47am UTC](https://discourse.slicer.org/t/how-to-get-the-ras-coordinates-of-the-pixel-in-a-slice-view-by-using-python-code/14668/1 "2020-11-18T01:47:36Z")

</div>

Hello,I am using the SlicerHeart module.There is a valve view function there.It can rotate the slice view(‘red’,‘green’,‘yellow’) to any angle.  
My goal is as followed:I want to add fiducial in the rotating slice view in a pixel(the position of the pixel will be showed as numpy array) by programming.  
My question is as followed:How can I get the RAS coordinates of the pixel in any angle slice view by python code.Or is there other way to achieve my goal.

---

<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:** [November 19, 2020, 3:12am UTC](https://discourse.slicer.org/t/how-to-get-the-ras-coordinates-of-the-pixel-in-a-slice-view-by-using-python-code/14668/2 "2020-11-19T03:12:02Z")

</div>

What would you like to achieve? Segment annulus curve? Leaflets?

---

<div class="post-metadata">

**Author:** ![Changing](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@Changing](https://discourse.slicer.org/u/Changing)\
**Post date:** [November 19, 2020, 3:29am UTC](https://discourse.slicer.org/t/how-to-get-the-ras-coordinates-of-the-pixel-in-a-slice-view-by-using-python-code/14668/3 "2020-11-19T03:29:00Z")

</div>

Yes.I want to use deep learning method to identify the mitral annulus point in each slice view.I haven’t finished that.Maybe I think it is possible,although the accuracy is low.The problem is if I know the location of the point in a slice view(the location will be shown as [posx,posy] in the numpy array), how can I get the RAS coordinate of this point to add the fiducial automatically.Thanks for your reply.

---

<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:** [November 19, 2020, 4:04am UTC](https://discourse.slicer.org/t/how-to-get-the-ras-coordinates-of-the-pixel-in-a-slice-view-by-using-python-code/14668/4 "2020-11-19T04:04:49Z")

</div>

Markup point positions are defined in RAS coordinates, so you are all good. You can get control point positions using [slicer.util.arrayFromMarkupsControlPoints](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.arrayFromMarkupsControlPoints) or curve points using [slicer.util.arrayFromMarkupsCurvePoints](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.arrayFromMarkupsCurvePoints).

Note that defining the annulus curve takes about 1-2 minutes with the current module and it takes about 10-20 seconds if you use a more optimized module (we will publicly release such a module soon). Once you have the annulus contour in one frame, you can track it along the cardiac cycle using Sequence Registration. So, annulus contouring and tracking has a quite good solution already with traditional approaches. I would recommend to use deep learning for more challenging problems, such as leaflet segmentation.

---

<div class="post-metadata">

**Author:** ![Changing](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@Changing](https://discourse.slicer.org/u/Changing)\
**Post date:** [November 19, 2020, 7:45am UTC](https://discourse.slicer.org/t/how-to-get-the-ras-coordinates-of-the-pixel-in-a-slice-view-by-using-python-code/14668/5 "2020-11-19T07:45:45Z")

</div>

May be something wrong.I try explaining as followed:The deep learning method can give the location of the mitral annulus point in an image.So I want to add fiducial in the corresponding position.My thought is to use the ScriptRepository examples( Get reformatted image from a slice viewer as numpy array ) to get the slice view as numpy array and use the deep learning result model to identify it.After that, I add fiducial in the location of the corresponding pixel automatically by python.How can I know the RAS coordinate of this pixel.Is it possible?Or there is another way to achieve that?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/a/4a3fd2d14f591d1e74a8230d8f9903529baa1587.jpeg)

Also,I am trying using deep learning for leaflet segmentation based on 3D slicer.But all things are just in the beginning.So if I have other problems I will post here.

Thanks for your help and this discussion forum.

---

<div class="post-metadata">

**Author:** ![Changing](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@Changing](https://discourse.slicer.org/u/Changing)\
**Post date:** [November 19, 2020, 8:56am UTC](https://discourse.slicer.org/t/how-to-get-the-ras-coordinates-of-the-pixel-in-a-slice-view-by-using-python-code/14668/6 "2020-11-19T08:56:12Z")

</div>

I have achieve my goal according to the ScriptRepository examples.For example, add the fiducial in the middle position of green slice view code is as followed:

> import numpy as np  
> sliceNodeID = ‘vtkMRMLSliceNodeGreen’  
> markupsNode = getNode(‘F’)  
> #Get image data from slice view  
> sliceNode = slicer.mrmlScene.GetNodeByID(sliceNodeID)  
> appLogic = slicer.app.applicationLogic()  
> sliceLogic = appLogic.GetSliceLogic(sliceNode)  
> sliceLayerLogic = sliceLogic.GetBackgroundLayer()  
> reslice = sliceLayerLogic.GetReslice()  
> reslicedImage = vtk.vtkImageData()  
> reslicedImage.DeepCopy(reslice.GetOutput())  
> #Create new volume node using resliced image  
> volumeNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLScalarVolumeNode”)  
> volumeNode.SetIJKToRASMatrix(sliceNode.GetXYToRAS())  
> volumeNode.SetAndObserveImageData(reslicedImage)  
> volumeNode.CreateDefaultDisplayNodes()  
> volumeNode.CreateDefaultStorageNode()  
> #Get voxels as a numpy array  
> volumeArray = slicer.util.arrayFromVolume(volumeNode)  
> print(voxels.shape)  
> #the middle pixel position  
> point\_Ijk = [voxels.shape[1]/2, voxels.shape[2]/2, 0]  
> #Get physical coordinates from voxel coordinates  
> volumeIjkToRas = vtk.vtkMatrix4x4()  
> volumeNode.GetIJKToRASMatrix(volumeIjkToRas)  
> point\_VolumeRas = [0, 0, 0, 1]  
> volumeIjkToRas.MultiplyPoint(np.append(point\_Ijk,1.0), point\_VolumeRas)  
> #If volume node is transformed, apply that transform to get volume’s RAS coordinates  
> transformVolumeRasToRas = vtk.vtkGeneralTransform()  
> slicer.vtkMRMLTransformNode.GetTransformBetweenNodes(volumeNode.GetParentTransformNode(), None, transformVolumeRasToRas)  
> point\_Ras = transformVolumeRasToRas.TransformPoint(point\_VolumeRas[0:3])  
> #Add a markup at the computed position and print its coordinates  
> markupsNode.AddFiducial(point\_Ras[0], point\_Ras[1], point\_Ras[2], “test”)  
> print(point\_Ras)
