How to get segment value through ras coordinate

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

commonGeometryString = segmentationNode.GetSegmentation().DetermineCommonLabelmapGeometry(slicer.vtkSegmentation.EXTENT_UNION_OF_SEGMENTS, None)
commonGeometryImage = slicer.vtkOrientedImageData()
slicer.vtkSegmentationConverter.DeserializeImageGeometry(commonGeometryString, commonGeometryImage, False) 
ijkToRas = vtk.vtkMatrix4x4()
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

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

This example in the script repository shows how to get list of segments visible at a specific position.

1 Like

thank you lassoan ,it’s work!

1 Like