# Creating Histograms From Segments 

**URL:** <https://discourse.slicer.org/t/creating-histograms-from-segments/22297>\
**Category:** Support\
**Created:** [March 3, 2022, 9:54pm UTC](https://discourse.slicer.org/t/creating-histograms-from-segments/22297 "2022-03-03T21:54:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benwilk](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@benwilk](https://discourse.slicer.org/u/benwilk)\
**Post date:** [March 3, 2022, 9:54pm UTC](https://discourse.slicer.org/t/creating-histograms-from-segments/22297/1 "2022-03-03T21:54:59Z")

</div>

I’ve been trying to follow the code found [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-histogram-of-a-segmented-region) to create a histogram from my existing MR and existing segmentation. I read on another post that I need to change the volume node and segmentation node so I have the code below, after multiple errors:

```auto
>>> volumeNode = getNode("Volume1")

>>> segmentationNode = getNode("Segmentation")

>>> segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(volumeNode)

>>> segmentID = "Segment_1"

>>> import numpy as np

>>> volumeArray = slicer.util.arrayFromVolume(volumeNode)

>>> segmentArray = arrayFromSegment(segmentationNode, segmentID)

>>> segmentVoxels = volumeArray[segmentArray != 0]

Traceback (most recent call last):

File "<console>", line 1, in <module>

IndexError: boolean index did not match indexed array along dimension 0; dimension is 112 but corresponding boolean dimension is 61

If anyone could tell me what I'm missing that would be fantastic! Unfortunately, I have no experience with python... 

Thanks!
Ben
```

---

<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:** [March 4, 2022, 7:27pm UTC](https://discourse.slicer.org/t/creating-histograms-from-segments/22297/2 "2022-03-04T19:27:13Z")

</div>

You need to use

```auto
segmentArray = slicer.util.arrayFromSegmentBinaryLabelmap(segmentationNode, segmentID, volumeNode)

```

Using this version ensures that the `segmentArray` is the same size as the `volumeArray` of `volumeNode`. The error you ran into is because the `segmentArray` is a different size than the `volumeArray`, so you can’t use it to properly index into `volumeArray` because it is not clear what the corresponding locations are.

---

<div class="post-metadata">

**Author:** ![benwilk](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@benwilk](https://discourse.slicer.org/u/benwilk)\
**Post date:** [March 7, 2022, 3:15pm UTC](https://discourse.slicer.org/t/creating-histograms-from-segments/22297/3 "2022-03-07T15:15:08Z")

</div>

Thank you for your reply! When I try that, I get the following error:  
Traceback (most recent call last):  
File “”, line 1, in   
AttributeError: ‘module’ object has no attribute ‘arrayFromSegmentBinaryLabelmap’

---

<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:** [March 7, 2022, 4:18pm UTC](https://discourse.slicer.org/t/creating-histograms-from-segments/22297/4 "2022-03-07T16:18:27Z")

</div>

What version of Slicer are you using? If you upgrade to a recent preview release, that function will be present. It was also used in the example you were following ([Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-histogram-of-a-segmented-region)). I would guess that you are using an older version of Slicer where the `arrayFromSegmentBinaryLabelmap` function had not been added yet.

---

<div class="post-metadata">

**Author:** ![SHADI](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@SHADI](https://discourse.slicer.org/u/SHADI)\
**Post date:** [May 20, 2023, 7:51pm UTC](https://discourse.slicer.org/t/creating-histograms-from-segments/22297/5 "2023-05-20T19:51:49Z")

</div>

Hi 3Dslicer community, I am trying to create a histogram from an existing segment by 3dslicer 5.2.2 and this code did not work. Your helps will be greatly appreciated.
