# Threshold scalar volume to labelmap

**URL:** https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457
**Category:** Development
**Tags:** volume, cli, filters
**Created:** [October 18, 2018, 8:22pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457 "2018-10-18T20:22:07Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)
#### Post date: [October 18, 2018, 8:22pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/1 "2018-10-18T20:22:07Z")

</div>

I am trying to use the python interface to load a (float) scalar volume in the form of a signed distance field want to apply thresholdscalarvolume to generate a label map with everything below 0.0. It doesn’t seem to be working in terms of creating the label map and running the cli interface.

here is a brief attempt with the code:

```auto
level_set = value['level_set']

[success, volumeNode] = slicer.util.loadVolume(level_set, returnNode=True)
volumeNode.SetName("case4" + "level_set")

tempLabel = slicer.vtkMRMLLabelMapVolumeNode() 
slicer.mrmlScene.AddNode(tempLabel) 
tempLabel.SetName("case4" + "thingy") 
temp_stuff = slicer.vtkSlicerVolumesLogic().CreateLabelVolumeFromVolume(slicer.mrmlScene, tempLabel, volumeNode)

# Compute the thresholded output volume using the Threshold Scalar Volume CLI module
cliParams = {'InputVolume': volumeNode.GetID(), 'OutputVolume': tempLabel.GetID(), 'ThresholdValue' : 0.0, 'ThresholdType' : 'Below', 'OutsideValue' : 1.0}
cliNode = slicer.cli.runSync(slicer.modules.thresholdscalarvolume, None, cliParams, wait_for_completion=True)

```

---

<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: [October 18, 2018, 8:52pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/2 "2018-10-18T20:52:49Z")

</div>

You can do it much simpler way:

```auto
voxelArray = slicer.util.arrayFromVolume(volumeNode)
voxelArray[voxelArray < thresholdValue] = 0
slicer.util.arrayFromVolumeModified(volumeNode)

```

Next steps depend on what you would like to do. Would you like to convert this to a segmentation node?

---

<div class="post-metadata">

### Author: ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)
#### Post date: [October 18, 2018, 9:23pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/3 "2018-10-18T21:23:02Z")

</div>

I figured out two things. runSync forces the command to wait until completed whereas run immediately returns and has to be checked.

the second thing was checking cliNode.GetErrorText() is empty the problem is that I used lower case “below” rather than “Below”.

now the line executes but doesn’t show anything on the screen.

---

<div class="post-metadata">

### Author: ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)
#### Post date: [October 18, 2018, 9:24pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/4 "2018-10-18T21:24:08Z")

</div>

That is much simpler. thank you.

yes that is it exactly, I saw the another post where there are commands to import the the label map to a segmentation mode, but hadn’t gotten that far yet.

---

<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: [October 18, 2018, 10:15pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/5 "2018-10-18T22:15:28Z")

</div>

You can create a segmentation from thresholded input `volumeNode` like this:

```auto
volumeNode = ...
thresholdValue = 80

# Create temporary labelmap
labelVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
slicer.vtkSlicerVolumesLogic().CreateLabelVolumeFromVolume(slicer.mrmlScene, labelVolumeNode, volumeNode)

# Fill temporary labelmap by thresholding input volumeNode
voxelArray = slicer.util.arrayFromVolume(volumeNode)
labelVoxelArray = slicer.util.arrayFromVolume(labelVolumeNode)
labelVoxelArray[voxelArray >= thresholdValue] = 100
labelVoxelArray[voxelArray < thresholdValue] = 0
slicer.util.arrayFromVolumeModified(labelVolumeNode)

# Import labelmap to segmentation
segmentationNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLSegmentationNode')
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelVolumeNode, segmentationNode)
segmentationNode.CreateClosedSurfaceRepresentation()

# Delete temporary labelmap
slicer.mrmlScene.RemoveNode(labelVolumeNode)

```

---

<div class="post-metadata">

### Author: ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)
#### Post date: [October 18, 2018, 11:08pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/6 "2018-10-18T23:08:14Z")

</div>

is it possible to set the master volume in the editor? can I do from the segmentationNode?

and also is it possible to select the slice views orientation button via as well?

---

<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: [October 19, 2018, 3:11am UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/7 "2018-10-19T03:11:13Z")

</div>

Yes, good point, you can easily create segments directly from a grayscale volume by choosing the volume as master volume and using Threshold effect. See full examples in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_run_segment_editor_effects_from_a_script).

---

<div class="post-metadata">

### Author: ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)
#### Post date: [October 22, 2018, 6:24pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/8 "2018-10-22T18:24:20Z")

</div>

Thanks,  
what I am doing is a little more complicated. I am pre segmenting with vmtk and want to edit that segmentation with slicer.

I used the command segmentEditorWidget.setMasterVolumeNode(volumeNode) to set the master volume but it didn’t change the gui view like it did when changed the name of the segmentation.

also whats the command to do the alignment? I wish I had a better way to figure out how buttons are linked to the commands to be used in as script.

---

<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: [October 22, 2018, 6:44pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/9 "2018-10-22T18:44:23Z")

</div>

> [@kayarre](#):
>
> I used the command segmentEditorWidget.setMasterVolumeNode(volumeNode) to set the master volume but it didn’t change the gui view like it did when changed the name of the segmentation.

You can change view by using [`slicer.util.setSliceViewerLayers` method](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#module-slicer.util).

> [@kayarre](#):
>
> also whats the command to do the alignment?

I’m not sure what alignment you are referring to.

> [@kayarre](#):
>
> I wish I had a better way to figure out how buttons are linked to the commands to be used in as script.

When I need to find how GUI actions mapped to code, the most direct way is to download Slicer source code and search in full text of all files. Most cases GUI strings are defined in Qt .ui files. In the .ui file you can find widget’s name that shows that string. Once you have the widget name, search full text of Slicer source code for widget name and you’ll find code that runs when the widget is manipulated.

You might find [Slicer API documentation](http://apidocs.slicer.org/master/index.html) and you can always ask on this forum.

---

<div class="post-metadata">

### Author: ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)
#### Post date: [October 22, 2018, 7:30pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/10 "2018-10-22T19:30:44Z")

</div>

Whats the python command to do the alignment of the image volume and segmentation?

 ![figure](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/2/a2a53960f19e430a9f885137a5fdde5cc98fd7ea.jpeg)

found it: segmentEditorWidget.rotateSliceViewsToSegmentation()

---

<div class="post-metadata">

### Author: ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)
#### Post date: [October 22, 2018, 9:09pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/11 "2018-10-22T21:09:11Z")

</div>

when I try to execute the following I get an error:

```auto
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)

```

segmentEditorWidget.setMRMLScene(slicer.mrmlScene) gives an error:

```auto
Python console user input: segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
void qMRMLSegmentEditorWidget::onSegmentSelectionChanged(const QItemSelection&, const QItemSelection&) : Invalid segment editor parameter set node
void qMRMLSegmentEditorWidget::onSegmentationNodeChanged(vtkMRMLNode*) : Invalid segment editor parameter set node
void qMRMLSegmentEditorWidget::onSegmentSelectionChanged(const QItemSelection&, const QItemSelection&) : Invalid segment editor parameter set node
void qMRMLSegmentEditorWidget::onSegmentationNodeChanged(vtkMRMLNode*) : Invalid segment editor parameter set node
void qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(vtkMRMLNode*) : Invalid segment editor parameter set node
void qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(vtkMRMLNode*) : Invalid segment editor parameter set node
void qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(vtkMRMLNode*) : Invalid segment editor parameter set node
virtual void qMRMLSegmentEditorWidget::updateWidgetFromMRML() : Invalid segment editor parameter set node

```

---

<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: [October 22, 2018, 11:01pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/12 "2018-10-22T23:01:33Z")

</div>

These errors are logged because the widget attempts to update itself but segment editor parameter node is not set yet (using `setMRMLSegmentEditorNode`). You can ignore these errors and we’ll update the widget to not log these errors.

---

<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 4, 2021, 1:34pm UTC](https://discourse.slicer.org/t/threshold-scalar-volume-to-labelmap/4457/13 "2021-06-04T13:34:56Z")

</div>

A post was split to a new topic: [How to get intensity range of a volume?](https://discourse.slicer.org/t/how-to-get-intensity-range-of-a-volume/17949)
