# Get image intensity histogram of a segment

**URL:** https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277
**Category:** Development
**Created:** [April 23, 2020, 9:36pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277 "2020-04-23T21:36:28Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Benjamin\_Coiffard](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benjamin_coiffard/32/5335_2.png) [@Benjamin\_Coiffard](https://discourse.slicer.org/u/Benjamin_Coiffard)
#### Post date: [April 23, 2020, 9:36pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/1 "2020-04-23T21:36:28Z")

</div>

I also need to export voxel intensities of a segmentation to analyse the histogram offline with my software. I applied this script in Python. What next? Where may I find the results?

---

<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: [April 24, 2020, 12:22am UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/2 "2020-04-24T00:22:46Z")

</div>

You can get the histogram of a segment in 6 lines of Python code as shown **[here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-histogram-of-a-segmented-region)**. Just copy paste the example code into Slicer’s Python console to try it.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/4/24e85549325158434201593fd038a04879f81941.png)

---

<div class="post-metadata">

### Author: ![Benjamin\_Coiffard](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benjamin_coiffard/32/5335_2.png) [@Benjamin\_Coiffard](https://discourse.slicer.org/u/Benjamin_Coiffard)
#### Post date: [May 4, 2020, 12:18pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/3 "2020-05-04T12:18:52Z")

</div>

Hi Andras,  
Thank you very much for answering.  
Your example doesn’t work but most likely because it has to be adapted to my node names? Here a copy of an example with the data names. Could you adapt the script to my situation? It will allow me to understand how it works and then being capable to fit the script to my other situations.  
I noticed that it is now possible to export the histogram in the Parenchyma Analysis module of CIP but the data are only available from -1024 to -350 HU and I need the whole data.  
Thank you very much for your help.  
I really appreciate it  
Regards  
Benjamin

 ![Example](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/9/4998a773e9ad1a4e4fd965730c38f71b4cf05ec4.jpeg)

---

<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 4, 2020, 2:15pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/4 "2020-05-04T14:15:21Z")

</div>

The link above points to a complete example, including generating sample data, to make sure that you can start from something that works and you can modify it step-by-step to do exactly what you need. If you work with your own data then replace “Generate input data” section and just set `masterVolumeNode` and `segmentationNode` to your nodes (e.g., `segmentationNode=getNode('Segmentation')`).

---

<div class="post-metadata">

### Author: ![Benjamin\_Coiffard](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benjamin_coiffard/32/5335_2.png) [@Benjamin\_Coiffard](https://discourse.slicer.org/u/Benjamin_Coiffard)
#### Post date: [May 4, 2020, 4:14pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/5 "2020-05-04T16:14:30Z")

</div>

It works !!! Thanks so much +++++  
Sorry for my ignorance, I’m just a beginner in coding…  
Much appreciate  
Have a nice day  
B

---

<div class="post-metadata">

### Author: ![martflar](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@martflar](https://discourse.slicer.org/u/martflar)
#### Post date: [January 17, 2023, 6:41pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/6 "2023-01-17T18:41:40Z")

</div>

Hi,  
Thanks for the really good example, I have an other problem.  
I am trying to make a histogram of a segment made using the Grow From Seeds module, but i can´t make it work.

This is the output format from the function i see in 3D slicer  
self.extentGrowthRatio = 0.1  
masterImageExtent = (0, 447, 0, 447, 0, 111)  
labelsEffectiveExtent = (100, 267, 61, 189, 35, 76)  
labelsExpandedExtent = [84, 283, 49, 201, 31, 80]

It would be great if someone could help with some code!

---

<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: [January 17, 2023, 6:49pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/7 "2023-01-17T18:49:39Z")

</div>

> [@martflar](#):
>
> I am trying to make a histogram of a segment made using the Grow From Seeds module, but i can´t make it work.

It does not matter what segmentation tools you used for creating your segmentation. After you finished segmentation using `Grow from seeds`, click `Apply` and proceed the same way as you would do after using any other tool.

---

<div class="post-metadata">

### Author: ![martflar](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@martflar](https://discourse.slicer.org/u/martflar)
#### Post date: [January 17, 2023, 7:12pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/8 "2023-01-17T19:12:07Z")

</div>

Thanks, Andreas, but I am still struggeling (sorry, real noobie here)  
For my imput data i use

segmentationNode=getNode(‘Segmentation’)  
masterVolumeNode=getNode(‘Segmentation’)

But i have no ideas on how to find the segmentID from the Grow From Seeds function  
I really appreciate such a quick answer on an old thread! 😄

---

<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: [January 17, 2023, 7:28pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/9 "2023-01-17T19:28:50Z")

</div>

> [@martflar](#):
>
> masterVolumeNode=getNode(‘Segmentation’)

Use the grayscale volume as masterVolumeNode.

> [@martflar](#):
>
> i have no ideas on how to find the segmentID

You get the segment ID when you create the segment, you can store that and use later when you need it.

You can also get all the segment IDs by calling:

```
segmentationNode.GetSegmentation().GetSegmentIDs()

```

If you want to get the segment ID from the segment name that you see in the application GUI:

```
segmentId = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(segmentName)

```

---

<div class="post-metadata">

### Author: ![martflar](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@martflar](https://discourse.slicer.org/u/martflar)
#### Post date: [January 17, 2023, 8:35pm UTC](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/10 "2023-01-17T20:35:21Z")

</div>

Thank you very much! Now it works! This was very helpful 😄
