# How to convert volume to label map programmatically

**URL:** <https://discourse.slicer.org/t/how-to-convert-volume-to-label-map-programmatically/22288>\
**Category:** Support\
**Created:** [March 3, 2022, 2:35pm UTC](https://discourse.slicer.org/t/how-to-convert-volume-to-label-map-programmatically/22288 "2022-03-03T14:35:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![koeglfryderyk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/koeglfryderyk/32/14638_2.png) [@koeglfryderyk](https://discourse.slicer.org/u/koeglfryderyk)\
**Post date:** [March 3, 2022, 2:35pm UTC](https://discourse.slicer.org/t/how-to-convert-volume-to-label-map-programmatically/22288/1 "2022-03-03T14:35:09Z")

</div>

I know how to do it in the GUI (go to ‘Volumes’ and at the bottom of ‘Volume Information’ go to ‘Convert to label map’).

I found the corresponding function to be:

```auto
void qSlicerVolumesModuleWidget::convertVolume()

```

But I cannot figure out how to access the function.

I can get  
`qSlicerVolumesModuleWidget`  
with  
`slicer.util.getModuleWidget('Volumes')`  
but it does not have the `convertVolume()` method (contrary to the [documentation](https://apidocs.slicer.org/master/classqSlicerVolumesModuleWidget.html))

Am I missing something or is this a bug?

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [March 3, 2022, 6:27pm UTC](https://discourse.slicer.org/t/how-to-convert-volume-to-label-map-programmatically/22288/2 "2022-03-03T18:27:04Z")

</div>

The `convertVolume()` method is present in the current Slicer code [Slicer/qSlicerVolumesModuleWidget.cxx at cb3a1ff14fac3419f3e3f08632a6cac6bedb52ca · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/blob/cb3a1ff14fac3419f3e3f08632a6cac6bedb52ca/Modules/Loadable/Volumes/qSlicerVolumesModuleWidget.cxx#L151), but I can confirm that it does not seem to be accessible from Python any way I can figure out either. Maybe something has gone wrong with the wrapping?

As a hack workaround, you could programmatically press the button in the Volumes module as follows:

```auto
w = slicer.util.getModuleWidget('Volumes')
convertButton = slicer.util.findChild(w, 'ConvertVolumeButton')
convertButton.clicked()

```

You would need to select the active volume in the module (and the target node if you aren’t replacing the input node) if you were to go this route.

Alternatively, you could look at the code inside the `convertVolume()` method and reimplement it in your own module.

---

<div class="post-metadata">

**Author:** ![koeglfryderyk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/koeglfryderyk/32/14638_2.png) [@koeglfryderyk](https://discourse.slicer.org/u/koeglfryderyk)\
**Post date:** [March 3, 2022, 8:16pm UTC](https://discourse.slicer.org/t/how-to-convert-volume-to-label-map-programmatically/22288/3 "2022-03-03T20:16:31Z")

</div>

Another workaround: create a label-map volume node from the pixel data of the volume node

1. get the volume node data that you want to convert to a label map:

2. Create a new label map volume node (or use an existing one):

3. Place the volume data into the label map node:

4. The new label-map volume node will probably have a different IJKToRAS matrix (and origin), so update them with the ones from the original volume node

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [March 3, 2022, 8:30pm UTC](https://discourse.slicer.org/t/how-to-convert-volume-to-label-map-programmatically/22288/4 "2022-03-03T20:30:10Z")

</div>

Also, I just looked a little deeper, and the functionality you originally wanted (and pretty much recreated) is available through the following:

```auto
volume_node = slicer.util.getFirstNodeByName(volume_name)
label_node = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
volumes_logic = slicer.modules.volumes.logic()
volumes_logic.CreateLabelVolumeFromVolume(slicer.mrmlScene, label_node, volume_node)

```

This function requires a labelmap node, so it, by itself, does not handle the case where you want to created labelmap node to replace the original scalar volume node, but it does duplicate the functionality in the code you just shared, where you get a new labelmap node created from the volume node.

The `CreateLabelVolumeFromVolume` method code is here [Slicer/vtkSlicerVolumesLogic.cxx at 409c7f035aa9015863cffc827ed37661322a550d · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/blob/409c7f035aa9015863cffc827ed37661322a550d/Modules/Loadable/Volumes/Logic/vtkSlicerVolumesLogic.cxx#L843)
