# Convert a segment to np array

**URL:** <https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [July 19, 2021, 11:55am UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797 "2021-07-19T11:55:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaume](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@Guillaume](https://discourse.slicer.org/u/Guillaume)\
**Post date:** [July 19, 2021, 11:55am UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/1 "2021-07-19T11:55:16Z")

</div>

Hello, I am doing an extension with Slicer 14.11 on windows in Python. So, I try to import the segmentation directly with. It gets stuck at the time of import (for the selection everything works) and convertion.

I used this helper: [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#read-and-write-a-segment-as-a-numpy-array)  
With :

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

segmentArray = slicer.util.arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId, volumeNode)

```

But slicer told me :

```auto
slicer.util.MRMLNodeNotFoundException: could not find nodes in the scene by name or id 'MRHead'

```

So I changed by:

```auto
volumeNode = slicer.mrmlScene.GetNodeByID('MRHead')
segmentationNode = slicer.mrmlScene.GetNodeByID('Segementation')
segmentId = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName('Segment_1')

mask = slicer.util.arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId, volumeNode)

```

But:

```auto
AttributeError: 'NoneType' object has no attribute 'GetSegmentation'

```

Thanks for your help ! Many topics have already helped me a lot.

---

<div class="post-metadata">

**Author:** ![riep](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/riep/32/9933_2.png) [@riep](https://discourse.slicer.org/u/riep)\
**Post date:** [July 19, 2021, 1:12pm UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/2 "2021-07-19T13:12:46Z")

</div>

Hi Guillaume,  
You have to first import the volume “MRHead” using the “sample data” module and do a segmentation with the segmentation module, before calling slicer.util.getNode functions.  
Pierre

---

<div class="post-metadata">

**Author:** ![Guillaume](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@Guillaume](https://discourse.slicer.org/u/Guillaume)\
**Post date:** [July 19, 2021, 2:26pm UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/3 "2021-07-19T14:26:39Z")

</div>

Hi Pierre,  
Thank you for your answer.

“GetNode” is not for picking up a module? If so, why is there a need for SampleData?

---

<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 19, 2021, 3:20pm UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/4 "2021-07-19T15:20:24Z")

</div>

You need to replace `MRHead` with the actual name of your volume.

---

<div class="post-metadata">

**Author:** ![Guillaume](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@Guillaume](https://discourse.slicer.org/u/Guillaume)\
**Post date:** [July 19, 2021, 3:45pm UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/5 "2021-07-19T15:45:40Z")

</div>

Oh okay, I didn’t understand it like that ! Thank’s a lot !

But I do that:

```auto
volumeNode = slicer.mrmlScene.GetNodeByID('IMG_001')
segmentationNode = slicer.mrmlScene.GetNodeByID('MASK_001')
segmentId = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName('Segment_1')

```

And Slicer told me:

```auto
AttributeError: 'NoneType' object has no attribute 'GetSegmentation'

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/a/faa806f33b52ec4d153dbbd4b658854807ff8da2.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 19, 2021, 4:02pm UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/6 "2021-07-19T16:02:02Z")

</div>

`GetNodeByID` gets a node by node ID, not by node name. Node name is displayed for users and it is not required to be unique. You can get the first node in the scene by the specified name by using `GetFirstNodeByName` method, but for interactive use in the Python console, I would recommend to use `getNode` method because it can take either node name or ID as input and throws an exception if the node is not found (instead of just returning None, so that you only find out that you did not get the node when you try to access its methods). This is all described in detail [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-mrml-node-from-the-scene).

---

<div class="post-metadata">

**Author:** ![Guillaume](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@Guillaume](https://discourse.slicer.org/u/Guillaume)\
**Post date:** [July 19, 2021, 4:26pm UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/7 "2021-07-19T16:26:25Z")

</div>

Okay, thank you. It works ! But…

```auto
TypeError: arrayFromSegmentBinaryLabelmap() takes 2 positional arguments but 3 were given

```

😭

It work without “volumeNode”… Is it ok ?

---

<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 19, 2021, 4:42pm UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/8 "2021-07-19T16:42:02Z")

</div>

List of input arguments to this helper function has changed a few times over the last 1-2 years, so I don’t know what arguments are needed in the Slicer version that you use. You can run`help(object.methodname)` to display the method’s documentation.

---

<div class="post-metadata">

**Author:** ![Guillaume](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@Guillaume](https://discourse.slicer.org/u/Guillaume)\
**Post date:** [July 20, 2021, 6:58am UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/9 "2021-07-20T06:58:04Z")

</div>

Okay, thank you for your help !

And here is the help:

```auto
arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId)

```

---

<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 20, 2021, 11:53am UTC](https://discourse.slicer.org/t/convert-a-segment-to-np-array/18797/11 "2021-07-20T11:53:55Z")

</div>

I would recommend to use the latest Slicer Preview Release, because there `arrayFromSegmentBinaryLabelmap` returns an array that has the same geometry as the first selected master volume, while earlier the array was cropped to the minimum required size, which was a bit more complicated than the use.

“Latest” documentation at the link [https://slicer.readthedocs.io/en/latest/](https://slicer.readthedocs.io/en/latest/) is for the latest Slicer Preview Release. Therefore, all the examples in the script repository page work in the latest Slicer Preview Release. You can find documentation for the current stable release (4.11) at [https://slicer.readthedocs.io/en/v4.11/](https://slicer.readthedocs.io/en/v4.11/).
