Export segmentation to labelmap using python

(Dmitriy Desser) #1

Hi guys,

I am trying to save some clicks and time by wrting a python script for saving my segmentations to files (labelmaps (nii.gz))
I simply do not undestand how to provide the reference image to the function:


so my code is:

seg = slicer.util.getNode(‘Segmentation_2’)
lmVN = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLLabelMapVolumeNode’)
slicer.modules.segmentations.logic().ExportAllSegmentsToLabelmapNode(seg, lmVN)
saveNode(lmVN, file_name)

if I give the reference image as additional argument, I get following error:

Traceback (most recent call last):

File “”, line 1, in

TypeError: ExportAllSegmentsToLabelmapNode() takes exactly 2 arguments (3 given)

Can anybody help?

Thank you very much!



(Andras Lasso) #2

See code snippet that does what you need in the script repository, and a complete working example here.

1 Like
(Dmitriy Desser) #3

thank you very much for your reply!

After running this Slicer crashes down:

#load volume
masterVolumeNode = slicer.util.loadVolume('c:/Users/desserdmi/Desktop/data.nii.gz', returnNode=True)

#make vtk object of mastervolume
masterVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkmasterVolumeNode")

#  --- Doing some   manual segmentation ----

#make segmentationNode
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")

#set references to masterVolumeNode

#make labelvolumenode
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')

#export Segmentation to labelmapNode
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, masterVolumeNode)

#save labelmap to disk

I am still doing something wrong… Do you maybe have any idea of what the reason could be?

(Csaba Pinter) #4

The original error was caused because that function is static, and the way you called it, it passes the object instance (self) as well.

To the code you recently pasted:

You don’t need this for sure. By the way there is no such class as vtkmasterVolumeNode. So this will return None and the variable won’t contain the loaded nifti file any more.

(Dmitriy Desser) #5

Thank you very much for your reply!

the reason, why I have tried this is the error message I get:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: SetReferenceImageGeometryParameterFromVolumeNode argument 1: method requires a VTK object

Anyway, is there no simple solution for this issue in 3dSlicer? I mean a simple save segmentation as function like in ITK-SNAP?

I have an amount of data with long names (BIDS) and trying to do some automatisation for segmentation like: load data -> segment it manually -> save segmentation to binary label maps

(Csaba Pinter) #6

In general, my advice is that try running the lines within the python interactor one by one, checking the content of variables. The error messages are usually pretty straightforward. For example if the message is that it should be a VTK object and it’s not, then you need to find out what it is instead.

Of course there is:
(this is in the Segmentations module)

Pleae check the source code for that if you want to automate it. Also the script repository (see @lassoan’s answer above) contains lots of examples you can directly use. In addition, this forum has topics for mostly every issue you may encounter, so please don’t hesitate to search it. The documentation may also help: https://www.slicer.org/wiki/Documentation/Nightly/Modules/Segmentations

(Andras Lasso) #7

We are of course aware that some simple segmentation software (that cannot work with arbitrary number of master volumes with varying geometry, overlapping segments, etc.) can save everything in a simple 3D labelmap. However, since Slicer is much more flexible, we need to use a more general file format by default to be able to save all segmentation data without losing information.

If you only use a single master volume and segments don’t overlap then you can export the segmentation into a simple 3D labelmap:

  • Using the application GUI by a few clicks either as Csaba showed above, or even simpler, just by right-clicking on the segmentation node in Data module and selecting “Export visible segments to binary labelmap”
  • Using two lines of Python code AddNewNodeByClass followed by ExportVisibleSegmentsToLabelmapNode (see here).

I’ve added a complete example that exports your segmentation to a simple 3D labelmap if you hit Ctrl+Shift+S - see here. You can add the code snippet to you .slicerrc file to make this feature permanently available.

(Dmitriy Desser) #8

Thank you so much guys!

editing .slicerrc works perfectly!

This is what I was looking for!