# From labelmap to model and from model to labelmap python scripts

**URL:** https://discourse.slicer.org/t/from-labelmap-to-model-and-from-model-to-labelmap-python-scripts/15833
**Category:** Support
**Created:** [February 4, 2021, 9:35am UTC](https://discourse.slicer.org/t/from-labelmap-to-model-and-from-model-to-labelmap-python-scripts/15833 "2021-02-04T09:35:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)
#### Post date: [February 4, 2021, 9:35am UTC](https://discourse.slicer.org/t/from-labelmap-to-model-and-from-model-to-labelmap-python-scripts/15833/1 "2021-02-04T09:35:01Z")

</div>

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
```

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [February 4, 2021, 2:22pm UTC](https://discourse.slicer.org/t/from-labelmap-to-model-and-from-model-to-labelmap-python-scripts/15833/2 "2021-02-04T14:22:34Z")

</div>

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](https://github.com/Slicer/Slicer/tree/master/Libs/vtkSegmentationCore). 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.

---

<div class="post-metadata">

### Author: ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)
#### Post date: [February 4, 2021, 3:27pm UTC](https://discourse.slicer.org/t/from-labelmap-to-model-and-from-model-to-labelmap-python-scripts/15833/3 "2021-02-04T15:27:36Z")

</div>

> 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](https://github.com/Slicer/Slicer/tree/master/Libs/vtkSegmentationCore). 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.
