From labelmap to model and from model to labelmap python scripts

Dear all,

I wrote these to functions to convert between labelmap and model nodes. Is there a similar simple non-slicer solution e.g. using only itk and vtk?

 def lbl2model(lblNode,lblName):
     segNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
     segNode.SetName(lblName)
     slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(lblNode, segNode)
     modelHNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelHierarchyNode")
     slicer.modules.segmentations.logic().ExportAllSegmentsToModelHierarchy(segNode, modelHNode)
     vColls = vtk.vtkCollection()
     modelHNode.GetChildrenModelNodes(vColls)
     modelNode = vColls.GetItemAsObject(0)
     return modelNode

def model2lbl(modelNode,modelName,refNode):
     segNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
     segNode.SetName(modelName)
     slicer.modules.segmentations.logic().ImportModelToSegmentationNode(modelNode,segNode)
     labelmapVolumeNode = 
     slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
     labelmapVolumeNode.SetName(modelName)
     ids = vtk.vtkStringArray()
     segNode.GetDisplayNode().GetVisibleSegmentIDs(ids)
     slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(segNode, ids,labelmapVolumeNode, refNode)
     return labelmapVolumeNode

All these methods use VTK filters - about 5-10 processing steps, including various smoothing, cleaning, simplification, etc. to make the conversion more robust and accurate. You can find the exact steps in “conversion rule” classes here. ITK does not have that many surface mesh processing filters, but it can do basic conversion between labelmaps and surfaces.

Note that you can run Slicer scripts from the command-line and you can use any Python package in Slicer’s virtual Python environment as you would use any other Python virtual environments.

1 Like

All these methods use VTK filters - about 5-10 processing steps, including various smoothing, cleaning, simplification, etc. to make the conversion more robust and accurate. You can find the exact steps in “conversion rule” classes here. ITK does not have that many surface mesh processing filters, but it can do basic conversion between labelmaps and surfaces.

Thanks for your informative and quick reply, I will have a look at the link.

Note that you can run Slicer scripts from the command-line and you can use any Python package in Slicer’s virtual Python environment as you would use any other Python virtual environments

This is what I am using now.