# How to get segment details for a specific frame

**URL:** <https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009>\
**Category:** Support\
**Tags:** python\
**Created:** [November 4, 2024, 6:51am UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009 "2024-11-04T06:51:17Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![maniron](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@maniron](https://discourse.slicer.org/u/maniron)\
**Post date:** [November 4, 2024, 6:51am UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/1 "2024-11-04T06:51:17Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 4, 2024, 6:44pm UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/2 "2024-11-04T18:44:04Z")

</div>

> [@maniron](#):
>
> segmentationNodeName = “Segmentation”  
> segmentationNode = slicer.mrmlScene.GetFirstNodeByName(segmentationNodeName)  
> segVal = segmentationNode.GetSegmentation()

And what’s the problem?

---

<div class="post-metadata">

**Author:** ![maniron](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@maniron](https://discourse.slicer.org/u/maniron)\
**Post date:** [November 5, 2024, 5:07am UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/3 "2024-11-05T05:07:18Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 5, 2024, 3:43pm UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/4 "2024-11-05T15:43:34Z")

</div>

> [@maniron](#):
>
> not the segment correspond to the frame which I want,

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

---

<div class="post-metadata">

**Author:** ![maniron](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@maniron](https://discourse.slicer.org/u/maniron)\
**Post date:** [November 5, 2024, 3:51pm UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/5 "2024-11-05T15:51:48Z")

</div>

Yes I want to find specific segment for a slice

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 5, 2024, 3:54pm UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/6 "2024-11-05T15:54:45Z")

</div>

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](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-segments-visible-at-a-selected-position)

---

<div class="post-metadata">

**Author:** ![maniron](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@maniron](https://discourse.slicer.org/u/maniron)\
**Post date:** [November 6, 2024, 5:07am UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/7 "2024-11-06T05:07:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 6, 2024, 5:33am UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/8 "2024-11-06T05:33:55Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![maniron](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@maniron](https://discourse.slicer.org/u/maniron)\
**Post date:** [November 6, 2024, 5:37am UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/9 "2024-11-06T05:37:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [November 6, 2024, 6:46pm UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/10 "2024-11-06T18:46:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 6, 2024, 10:22pm UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/11 "2024-11-06T22:22:15Z")

</div>

> [@maniron](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![maniron](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@maniron](https://discourse.slicer.org/u/maniron)\
**Post date:** [November 7, 2024, 7:02am UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/12 "2024-11-07T07:02:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 7, 2024, 5:45pm UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/13 "2024-11-07T17:45:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maniron](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@maniron](https://discourse.slicer.org/u/maniron)\
**Post date:** [November 12, 2024, 5:33am UTC](https://discourse.slicer.org/t/how-to-get-segment-details-for-a-specific-frame/40009/14 "2024-11-12T05:33:58Z")

</div>

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
