How to get segment details for a specific frame

Hi,
I am trying to get the number of segmentations done for a frame using python. I am trying this out using the following code

segmentationNodeName = “Segmentation”
segmentationNode = slicer.mrmlScene.GetFirstNodeByName(segmentationNodeName)
segVal = segmentationNode.GetSegmentation()

And what’s the problem?

@muratmaga I want to get segment details for each frame , when ever I make getSegmentation call I get the total no of segmentation , and not the segment correspond to the frame which I want, How can I get segment details of a particular frame of my choice

I am not sure what you mean by that? You mean you want the segmentation from a specific slice?

Yes I want to find specific segment for a slice

There is an example of getting the segmentation as a specific point in the script repository. You can probably rework that;

https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-segments-visible-at-a-selected-position

Can I rephrase my question

consider I have a nrrd file with 100 frames
Now I segment each frame/slice
After segmenting each frame/slice , how to get a particular frame/slice segment details or mask

If you look at the script examples I shared above, you can probably put together something using slicer.util.arrayFromSegmentBinaryLabelmap, then you can extract the 2D mask using your slice number.

That will give you the whole

volumeNode = getNode('MRHead')
segmentationNode = getNode('Segmentation')
segmentId = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName('Segment_1')

# Get segment as numpy array
segmentArray = slicer.util.arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId, volumeNode)

Thanks for replying , the thing here is that consider I dont know details of segment id, Actually I am trying to integrate a tracker model after using one model for segmentation, so for tracker model when we move from one slice to another , the last slice segmented (mask) will be utilised to get a segment for the current slice

For this scenario kindly provide some sample code which would be helpful

Using the approach described by @muratmaga, you get the array containing all slices, is there some reason you can’t choose the slice you want out of that? Perhaps it would help if you gave a higher level overview of what you are trying to accomplish. You now mention a tracker for the first time, for example. Slicer IGT has many tools for working with real time tool tracking, and depending on what it is you are trying to accomplish, may make it unnecessary for you to find, for example, the slice corresponding to a tracked location.

Before you start into it, you might need to understand how segmentation works in slicer. Segmentation is not slice by slice, but is in context of the whole volume. So if you have one segment (Segment_1) with only two slices, say one is at the very top, and the one at the very bottom, they will have the same exact ID, which is Segment_1. So perhaps experiment with segmenting with sample data and see how you can extract what you want to extract before you do the tracking.

In this case how can I get the mask of previous frame/slice and utilize it on the next frame/slice . As I could see the same segment id is being used for each slice

If you know the position you want the mask (from your description sounds like you do), you can use method I described above.

I dont know whether I am asking the question right, but what I want is that consider we segment a frame/slice , model gives a mask. When I move to a next frame/slice I want to utilize the mask to generate a new mask for that frame/slice. But I am not able to find the code to get the mask . As I could see the segmentation details are maintained in a same segment ID . Kindly help me out with this solution. The main aim here is to do a multi object segmentation