# Segment / BinaryLabelMap to numpy array

**URL:** <https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [July 27, 2017, 9:02pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778 "2017-07-27T21:02:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_McCurdy](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@Colin\_McCurdy](https://discourse.slicer.org/u/Colin_McCurdy)\
**Post date:** [July 27, 2017, 9:02pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/1 "2017-07-27T21:02:23Z")

</div>

Operating system:Windows 10  
Slicer version: 4.7.0-2017-07-19

Hello All,

I’m trying to get a numpy array out of a segment or binarylabelmap and I can’t find a way to do that currently. I’m sure it’s right in front of me…

I’m just trying to use it to mask my grayscale data array so if there’s a better way to go about this I’m all ears!

What I hoped to do:  
grayDataArray = arrayFromVolume(grayscaleVolume)  
segmentArray = arrayFromVolume(segment) # this obviously doesn’t work 🙂  
maskedData = grayDataArray \* segmentArray

Thanks for any help and advice!

Colin

---

<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 28, 2017, 12:50am UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/2 "2017-07-28T00:50:25Z")

</div>

Segmentation node -\> labelmap node: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Export\_labelmap\_node\_from\_segmentation\_node](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Export_labelmap_node_from_segmentation_node)

Labelmap node -\> numpy array: [https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python\_scripting#Accessing\_Volume\_data\_as\_numpy\_array](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Accessing_Volume_data_as_numpy_array)

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [July 28, 2017, 8:23pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/3 "2017-07-28T20:23:26Z")

</div>

This works for me, if your Segmentation is named “Seg” and Volume is named “Vol”. If they are named differently, you would just change the parameters to ‘getNode’ in the first two lines

```
volumeNode = slicer.util.getNode('Vol') # Get the volume node
seg = slicer.util.getNode('Seg') # Get the segmentation node

# Create a binary label volume from segmentation
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
#To export specific segments instead, edit the following line with the one in the bottom
slicer.modules.segmentations.logic().ExportAllSegmentsToLabelmapNode(seg, labelmapVolumeNode) 

# Export data as numpy arrays
dicomData = arrayFromVolume(volumeNode)
mask = arrayFromVolume(labelmapVolumeNode)

```

**Edit** : to export specific segments instead of all of the segmentation, use the following code:

`slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(seg,segmentsToExport, labelMapVolumeNode)`

The `segmentsToExport` value has to be a `vtkStringArray` containing all the segment ID’s that you would like to be exported (note, I noticed that this overrides the label map), which is conceptually a fancy string array. To initialize it you have to run the following:

```
#To get the segment ID of the first segment on the list (named Segment_1 below):
segmentId = seg.GetSegmentation().GetNthSegmentID(0)

```

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/9/995b3aa6f5b9583114471fb465e601e9819a9a0c.png)

```
import vtk
segmentsToExport = vtk.vtkStringArray()
segmentsToExport.InsertNextValue(segmentId)

```

---

<div class="post-metadata">

**Author:** ![Colin\_McCurdy](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@Colin\_McCurdy](https://discourse.slicer.org/u/Colin_McCurdy)\
**Post date:** [July 31, 2017, 6:42pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/4 "2017-07-31T18:42:12Z")

</div>

I’m assuming the following code converts all of the segments onto a single labelmap?

> [@moselhy](#):
>
> slicer.modules.segmentations.logic().ExportAllSegmentsToLabelmapNode(seg, labelmapVolumeNode)

So If I wanted this for one or multiple segments (but not all) I can just use:

> slicer.modules.segmentations.logic().ExportSegmentsToLabelMapNode(seg,segmentNameArray,labelMapVolumeNode)

Now, I’m not entirely sure what I’m getting from this because now I have an error that the volume array (volumeNode) and the segmentation array (labelmapVolumeNode) have different sizes

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [July 31, 2017, 6:46pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/5 "2017-07-31T18:46:40Z")

</div>

Try the edit that I made, does that still give you an error?

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [July 31, 2017, 8:58pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/6 "2017-07-31T20:58:16Z")

</div>

> [@Colin\_McCurdy](#):
>
> the volume array (volumeNode) and the segmentation array (labelmapVolumeNode) have different sizes

You can resample the label to match the volume geometry, for example as done here: [SlicerRadiomics/SlicerRadiomics/SlicerRadiomics.py at master · AIM-Harvard/SlicerRadiomics · GitHub](https://github.com/Radiomics/SlicerRadiomics/blob/master/SlicerRadiomics/SlicerRadiomics.py#L365-L373)

The option of matching geometry is exposed in the `Segmentations` module GUI (specified by the “Reference volume” node), perhaps there is a corresponding API for that too, but I don’t have time to look for it now.

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

---

<div class="post-metadata">

**Author:** ![Colin\_McCurdy](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@Colin\_McCurdy](https://discourse.slicer.org/u/Colin_McCurdy)\
**Post date:** [August 9, 2017, 8:11pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/7 "2017-08-09T20:11:08Z")

</div>

Sorry I’m late to respond to this.  
I think your link is incorrect, it looks like the solution you’re speaking of is a bit further down. I think I found it though:

> <https://github.com/Radiomics/SlicerRadiomics/blob/master/SlicerRadiomics/SlicerRadiomics.py#L383-L387>

I appreciate the help!

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [August 9, 2017, 8:22pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/8 "2017-08-09T20:22:00Z")

</div>

> [@Colin\_McCurdy](#):
>
> it looks like the solution you’re speaking of is a bit further down

That’s probably because the code changed since I inserted the link, and I referred to the master instead of the specific commit.

Glad you found it anyway!

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [August 9, 2017, 8:44pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/9 "2017-08-09T20:44:00Z")

</div>

> [@fedorov](#):
>
> because the code changed since I inserted the link

A neat GitHub trick is to press “y” to get a canonical link.  
See [Getting permanent links to files - GitHub Docs](https://help.github.com/articles/getting-permanent-links-to-files/)

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [August 9, 2017, 8:57pm UTC](https://discourse.slicer.org/t/segment-binarylabelmap-to-numpy-array/778/10 "2017-08-09T20:57:46Z")

</div>

Thanks @jcfr!

Here comes the permalink: [https://github.com/Radiomics/SlicerRadiomics/blob/97bdf52df6ba40b911b4e5d9783a017be2d2bfe4/SlicerRadiomics/SlicerRadiomics.py#L383-L386](https://github.com/Radiomics/SlicerRadiomics/blob/97bdf52df6ba40b911b4e5d9783a017be2d2bfe4/SlicerRadiomics/SlicerRadiomics.py#L383-L386)
