# Converting SegmentationNode to Labelmap takes a long time through Python

**URL:** <https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696>\
**Category:** Development\
**Created:** [June 3, 2022, 9:03pm UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696 "2022-06-03T21:03:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![edwardwang1](https://avatars.discourse-cdn.com/v4/letter/e/858c86/32.png) [@edwardwang1](https://discourse.slicer.org/u/edwardwang1)\
**Post date:** [June 3, 2022, 9:03pm UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696/1 "2022-06-03T21:03:15Z")

</div>

Hello,

I am using Slicer to convert RT structs to binary label maps. I am able to do it via Python, but it takes much longer than using the Segmentations module and doing it by hand (under the representations section). For some files it takes a few minutes, and for some it hangs for hours. I have tested this both via the Jupyter Extension, as well as running a script via the command line.

The line that causes the issue is:  
`classvtkMRMLSegmentationNode. CreateBinaryLabelmapRepresentation()`

OS: Windows 10  
Slicer versions: 4.11 (Jupyter + python script) and 5.1 (python script only, Jupyter not tested)

I suspect that this issue arises because the Segmentations module is using a different function call, though I have not been able to find more details to support this.

Thanks for your help!  
Edward

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [June 5, 2022, 7:17pm UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696/2 "2022-06-05T19:17:59Z")

</div>

Please read more.about this.class 🙂  
[https://apidocs.slicer.org/master/classvtkSegmentation.html](https://apidocs.slicer.org/master/classvtkSegmentation.html)

I thimkmit may help

---

<div class="post-metadata">

**Author:** ![edwardwang1](https://avatars.discourse-cdn.com/v4/letter/e/858c86/32.png) [@edwardwang1](https://discourse.slicer.org/u/edwardwang1)\
**Post date:** [June 6, 2022, 3:48pm UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696/3 "2022-06-06T15:48:10Z")

</div>

Hi Mauro,

Thanks for your response. Is there a particular function that you suggest I look at? I’ve double checked that the conversion parameters are the same when running it through Python as well as using the Segmentations module.

I have also tried first converting my planar contours to closed surfaces, then to binary label maps (because it has a much lower estimated cost than planar contour → ribbon model → binary label map), but it is still taking a long time.

Thanks,  
Edward

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [June 6, 2022, 5:25pm UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696/4 "2022-06-06T17:25:11Z")

</div>

I think you should read about this

> **[SlicerRT/DicomRtImportExport/ConversionRules at master · SlicerRt/SlicerRT](https://github.com/SlicerRt/SlicerRT/tree/master/DicomRtImportExport/ConversionRules)**
>
> master/DicomRtImportExport/ConversionRules

Hope it helps

---

<div class="post-metadata">

**Author:** ![steffen-o](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/steffen-o/32/2660_2.png) [@steffen-o](https://discourse.slicer.org/u/steffen-o)\
**Post date:** [June 7, 2022, 7:10am UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696/5 "2022-06-07T07:10:51Z")

</div>

I use this code snippet and it works fine:

> segmentationNode = getNode(“Segmentation”)  
> labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLLabelMapVolumeNode”)  
> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, referenceVolumeNode)

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html?highlight=snippet#export-labelmap-node-from-segmentation-node](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html?highlight=snippet#export-labelmap-node-from-segmentation-node)

---

<div class="post-metadata">

**Author:** ![edwardwang1](https://avatars.discourse-cdn.com/v4/letter/e/858c86/32.png) [@edwardwang1](https://discourse.slicer.org/u/edwardwang1)\
**Post date:** [June 11, 2022, 9:18pm UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696/6 "2022-06-11T21:18:01Z")

</div>

Thanks for the response! Using the code you provided, I was able to narrow down my problem to a few problematic contours/segmentations. Once I get that figured out, I am confident that I will get everything working.

---

<div class="post-metadata">

**Author:** ![edwardwang1](https://avatars.discourse-cdn.com/v4/letter/e/858c86/32.png) [@edwardwang1](https://discourse.slicer.org/u/edwardwang1)\
**Post date:** [June 13, 2022, 7:04pm UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696/7 "2022-06-13T19:04:12Z")

</div>

After playing around with the Segmentation modules some more, I think I’ve discovered the source of my issue. Some of the contours that I exported from our radiation contour planning software are improperly converted to closed surfaces when I import their DICOM into Slicer. Therefore, the conversion of planar contour → closed surface → binary label map doesn’t work.

Using the Segmentation module, I can choose the higher cost option of converting planar contour → ribbon model → binary label map, and this appears to work. Previously when I converted the contours manually in Slicer, I must have chosen this path. So it appears that I can convert everything using the Slicer app, but I would like to do this via a script, as I have a lot patients to convert.

I tried to use the **BatchStructureSetConversion.py** script in the SlicerRT extension and that didn’t work either, I think because it also tries to use closed surface as an intermediate step. Looking at the code, it calls `CreateBinaryLabelmapRepresentation()`, which is what I did in my original script.

Looking at the Python API for vtkMRMLSegmentationNode and vtkSegmentation, there does not appear to be a way to use a ribbon model as an intermediary. My next step is try and create a C++ extension that can make use of the relevant Conversion Rules in SlicerRT - but before I get to deep in this, I wonder if anyone has any alternate suggestions.

---

<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:** [June 13, 2022, 7:50pm UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696/8 "2022-06-13T19:50:43Z")

</div>

All features are available via the Python interface, so you don’t need to switch to C++ if you don’t want to. It is possible that in some cases we missed wrapping some structures into Python-compatible types - if you find one then let us know and we’ll fix it.

You can see how conversion is done with custom conversion path in the icdr GUI: [Slicer/qMRMLSegmentationConversionParametersWidget.cxx at master · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/Widgets/qMRMLSegmentationConversionParametersWidget.cxx)

There are some [related Python code snippets](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#convert-all-segments-using-default-path-and-conversion-parameters) that you may find useful.

---

<div class="post-metadata">

**Author:** ![edwardwang1](https://avatars.discourse-cdn.com/v4/letter/e/858c86/32.png) [@edwardwang1](https://discourse.slicer.org/u/edwardwang1)\
**Post date:** [June 13, 2022, 9:26pm UTC](https://discourse.slicer.org/t/converting-segmentationnode-to-labelmap-takes-a-long-time-through-python/23696/9 "2022-06-13T21:26:56Z")

</div>

Hi Andras,

Thanks for your response. I took a look at the Python code examples, and it seems like there aren’t any examples using a custom conversion path, just ones that use a custom conversion parameter.

I also took a look at the C++ code that you linked. There is a snippet that I would like to use:

```auto
  vtkSegmentation* segmentation = d->SegmentationNode->GetSegmentation();
  vtkSegmentationConverter::ConversionPathAndCostListType pathsCosts;
  segmentation->GetPossibleConversions(d->TargetRepresentationName.toUtf8().constData(), pathsCosts);

```

but it seems like the method:

```auto
GetPossibleConversions()

```

is not available to the Python object of vtkSegmentation (I checked this using the dir() function in Python).

Furthermore, looking at vtkSegmentationConverter, it appears there is no equivalent to: `GetBinaryLabelmapRepresentationName ()` or  
`GetSegmentationBinaryLabelmapRepresentationName ()`  
for a Ribbon Model, which is the intermediate representation that I want to use. I would be curious to see if

```auto
segmentation.GetPossibleConversions()

```

would include it in one if its paths.

Thanks
