Import labelmap to segmentation node - in batch processing

There were a couple of problems:

  • It is not enough to create the segmentation node but you need to also add to the scene. You can use slicer.mrmlScene.AddNewNodeByClass to create a node and add it to the scene in one step.
  • slicer.util.loadVolume loads file as scalar volume, while ImportLabelmapToSegmentationNode expects a labelmap volume; you need to use slicer.util.loadLabelVolume instead
  • Relative path was used for the input image. I’m not sure what is the working directory of Slicer. It is safer to use absolute path.

After fixing these issues, a full working example:

[success, labelmapVolumeNode] = slicer.util.loadLabelVolume(r"c:\Users\msliv\OneDrive\Projects\SlicerTesting3\20180410-StartupSegmentation\Segmentation-label.nrrd", returnNode=True)
segNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode, segNode)
2 Likes