Convert a segment to np array

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
With :

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 :

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

So I changed by:

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

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

But:

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

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

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

Hi Pierre,
Thank you for your answer.

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

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

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

But I do that:

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

And Slicer told me:

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

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.

1 Like

Okay, thank you. It works ! But…

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

:sob:

It work without “volumeNode”… Is it ok ?

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 runhelp(object.methodname) to display the method’s documentation.

Okay, thank you for your help !

And here is the help:

arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId)

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/ 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/.

1 Like