How to automatically convert a labelmap volume to a segmentation node upon loading labelmap volume

Hi,

I want to script Slicer to automatically convert labelmap volume to a segmentation node when I load the labelmap volume, rather than having to right-click and convert it through the GUI. How can I achieve this? Can this be done in .slicerrc.py?

Thanks

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Create_a_segmentation_from_a_labelmap_volume_and_display_in_3D

1 Like

If you use a recent Slicer Preview version then you can load nrrd and nifti files directly as segmentation (just choose Segmentation in the Description column in Add Data dialog). If you make the segmentation filename end with .seg.nrrd then Segmentation is selected automatically.

Hi Aiden,

Thanks for the reply - Yes I have copied and pasted that script into .slicerrc.py. Although when I load a volume as a LabelMap, it does not automatically get converted to a segmentation node. I am a novice user - is there something I need to specify in the script?

Thanks

Do you use python? if yes, maybe I may try to send you a loadable python script so you may do it by just clicking a button.

Hi Aiden,

That is a kind offer! But I do not use python. I am hoping for a solution that can be directly applied to the slicerrc.py.

To give more context, I am loading pairs of volumes: one Scalar and one LabelMap volume - all one after the other. It is tedious to have to right click and convert the LabelMap volume to a segmentation node to perform edits. Is there a way that I can script slicerrc.py to automatically convert all LabelMap volume data to a segmentation node?

Thanks

It sounds like you need CaseIterator extension, which is developed for iterating through a set of volumes and labelmaps. We use this for AI segmentation for generating training data and quality check and recently got grant funding to improve it further, based on what users need. So, if you have any feedback or suggestion then let us know.

1 Like

Hi,

So, the script below only allows me to convert a labelmap volume that has already been added to the scene. Is there a way to modify the script so that it applies to all LabelMap volumes I load even if they have not been added to the scene?

labelmapVolumeNode = getNode(‘my_labelmap_name’)
seg = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLSegmentationNode’)
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode, seg)

1 Like

In recent Slicer Preview releases, you can load the volume file directly as segmentation node. No need for an intermediate labelmap.