# How to get segment value through ras coordinate

**URL:** https://discourse.slicer.org/t/how-to-get-segment-value-through-ras-coordinate/29687
**Category:** Development
**Created:** [May 27, 2023, 7:30am UTC](https://discourse.slicer.org/t/how-to-get-segment-value-through-ras-coordinate/29687 "2023-05-27T07:30:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jay1987](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jay1987/32/16183_2.png) [@jay1987](https://discourse.slicer.org/u/jay1987)
#### Post date: [May 27, 2023, 7:30am UTC](https://discourse.slicer.org/t/how-to-get-segment-value-through-ras-coordinate/29687/1 "2023-05-27T07:30:42Z")

</div>

hi,Community  
My train of thought to solve the problem is this:  
1.get ras coordinate (ras\_x,ras\_y,ras\_z) through crosshair node  
2.get segmentationNode’s rastoijk matrix use code below

```auto
commonGeometryString = segmentationNode.GetSegmentation().DetermineCommonLabelmapGeometry(slicer.vtkSegmentation.EXTENT_UNION_OF_SEGMENTS, None)
commonGeometryImage = slicer.vtkOrientedImageData()
slicer.vtkSegmentationConverter.DeserializeImageGeometry(commonGeometryString, commonGeometryImage, False) 
ijkToRas = vtk.vtkMatrix4x4()
commonGeometryImage.GetImageToWorldMatrix(ijkToRas)
rastoijk = vtk.vtkMatrix4x4()
vtk.vtkMatrix4x4.Invert(ijkToRas, rastoijk)

```

3.change ras point to ijk point

> point\_Ijk = [0, 0, 0, 1]  
> rastoijk.MultiplyPoint(np.append(ras,1.0), point\_Ijk)  
> point\_Ijk = [int(round(c)) for c in point\_Ijk[0:3] ]

4.change the 0th segment into numpy array

```auto
segmentid = util.GetNthSegmentID(segment_node,0)
segmentArray = util.arrayFromSegment(segmentationNode,segmentid).T

```

5.get segment value through ijk point  
final\_value = segmentArray[point\_Ijk[0],point\_Ijk[1],point\_Ijk[2]]

The final result is wrong, it seems that there is always a matrix transformation difference between the correct result, but I don’t know where the error occurred

---

<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: [May 28, 2023, 12:38am UTC](https://discourse.slicer.org/t/how-to-get-segment-value-through-ras-coordinate/29687/2 "2023-05-28T00:38:46Z")

</div>

[This example in the script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-segments-visible-at-a-selected-position) shows how to get list of segments visible at a specific position.

---

<div class="post-metadata">

### Author: ![jay1987](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jay1987/32/16183_2.png) [@jay1987](https://discourse.slicer.org/u/jay1987)
#### Post date: [May 28, 2023, 2:48am UTC](https://discourse.slicer.org/t/how-to-get-segment-value-through-ras-coordinate/29687/3 "2023-05-28T02:48:09Z")

</div>

thank you lassoan ,it’s work!
