# Histogram with the Hounsfield scale of segmented

**URL:** https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433
**Category:** Support
**Tags:** segmentation, python
**Created:** [July 8, 2020, 8:52am UTC](https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433 "2020-07-08T08:52:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![cotozakot](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@cotozakot](https://discourse.slicer.org/u/cotozakot)
#### Post date: [July 8, 2020, 8:52am UTC](https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433/1 "2020-07-08T08:52:31Z")

</div>

Operating system: Windows  
Slicer version: 4.11.0  
Expected behavior:  
Actual behavior:  
I have segmented. As for this manually made segmentation, I can make a histogram with the Housfield scale for each single layer. I saw an example ([Get image intensity histogram of a segment](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277/2)) unfortunately I can’t use it in my case.

---

<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: [July 8, 2020, 1:08pm UTC](https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433/2 "2020-07-08T13:08:02Z")

</div>

If you need histogram to find good threshold value for segmentation: In recent Slicer Preview Releases, you can go to Segment Editor, create a segment, click on Threshold effect, and then click-and-drag on the image to define a region where histogram will be computed on. To view the histogram, open “Local histogram” section.

If you need histogram of a region under a segment then the code snippet you referenced above still works perfectly. If you have any difficulty using it then tell exactly what you do, what you expect to happen, and what happens instead.

---

<div class="post-metadata">

### Author: ![cotozakot](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@cotozakot](https://discourse.slicer.org/u/cotozakot)
#### Post date: [July 8, 2020, 1:23pm UTC](https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433/3 "2020-07-08T13:23:49Z")

</div>

I don’t know how to change the code so that the output histogram refers to the segmentation that I did manually. in the code, the segmentation is created automatically, I want to change it to my manually made segmentation.

śr., 8 lip 2020, 15:18 użytkownik Andras Lasso via 3D Slicer Community \<[slicer@discoursemail.com](mailto:slicer@discoursemail.com)\> napisał:

---

<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: [July 8, 2020, 1:50pm UTC](https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433/4 "2020-07-08T13:50:08Z")

</div>

To use the script on your own data: replace “Generate input data” section of the code with `getNode` calls (it gets MRML node from the scene by its name).

---

<div class="post-metadata">

### Author: ![cotozakot](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@cotozakot](https://discourse.slicer.org/u/cotozakot)
#### Post date: [July 8, 2020, 2:39pm UTC](https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433/5 "2020-07-08T14:39:30Z")

</div>

this is my code, unfortunately it doesn’t work. Could you tell me what’s wrong?

```python
# Generate input data
################################################

# Load master volume
import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = getVolumeNode ('6: DE DE_CarotidAngio 1.0 D30f B_80kV')()

# Create segmentation
segmentationNode=getNode('Segmentation') ()
slicer.mrmlScene.AddNode(segmentationNode)
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)

# Create segment
tumorSeed = vtk.vtkSphereSource()
tumorSeed.SetCenter(-6, 30, 28)
tumorSeed.SetRadius(25)
tumorSeed.Update()
segmentationNode.AddSegmentFromClosedSurfaceRepresentation(tumorSeed.GetOutput(), "Segment A", [1.0,0.0,0.0])

# Compute histogram
################################################

labelValue = 1 # label value of first segment
# Get segmentation as labelmap volume node
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, masterVolumeNode)

# Extract all voxels of the segment as numpy array
volumeArray = slicer.util.arrayFromVolume(masterVolumeNode)
labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
segmentVoxels = volumeArray[labelArray==labelValue]

# Compute histogram
import numpy as np
histogram = np.histogram(segmentVoxels, bins=50)

# Plot histogram
################################################

slicer.util.plot(histogram, xColumnIndex = 1)

```

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/0/3090c01402573aae98540a4f8311027a44ea0c51.png)

---

<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: [July 8, 2020, 4:51pm UTC](https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433/6 "2020-07-08T16:51:02Z")

</div>

Replace “Generate input data” section by this:

```python
masterVolumeNode = getNode ('6: DE*')
segmentationNode = getNode('Segmentation')

```

You can choose between segments by setting `labelValue` to 1, 2, or 3.

---

<div class="post-metadata">

### Author: ![Alice\_Durante](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alice_durante/32/67737_2.png) [@Alice\_Durante](https://discourse.slicer.org/u/Alice_Durante)
#### Post date: [January 30, 2024, 11:24am UTC](https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433/7 "2024-01-30T11:24:07Z")

</div>

Hello,  
I used the same script for my histogram and it works, but i noticed something: if i copy and paste the table of hu value and number of voxel that i get with the script, in my matlab script to calculate the mean of hu, mine mean is different form 3D slicer mean. I tried to do the same thing with excel and nothing change. The difference between matlab and excel mean with 3D slicer mean is like 6 units, but for my study they are important. Can you explain that?

(To calculate the mean I multiplied the single value of column A, with the single value of column B, summed the products and divided by the total number of voxels of the segment considered)

---

<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: [January 30, 2024, 4:29pm UTC](https://discourse.slicer.org/t/histogram-with-the-hounsfield-scale-of-segmented/12433/8 "2024-01-30T16:29:49Z")

</div>

I would guess this is due to the histogram binning. The easiest way to find the mean is to just take the average of the list of segment voxel values (e.g. `np.mean(segmentVoxels)` ). That will be independent of any binning or table-making, since it is literally the average of the voxel values of the segment.
