# Threshold on Segment

**URL:** <https://discourse.slicer.org/t/threshold-on-segment/258>\
**Category:** Support\
**Tags:** segmentation, threshold\
**Created:** [May 3, 2017, 6:59pm UTC](https://discourse.slicer.org/t/threshold-on-segment/258 "2017-05-03T18:59:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@basti](https://discourse.slicer.org/u/basti)\
**Post date:** [May 3, 2017, 6:59pm UTC](https://discourse.slicer.org/t/threshold-on-segment/258/1 "2017-05-03T18:59:14Z")

</div>

I’m trying to threshold a volume to a created segment. Unfortunately the segment will be empty. What did I forget?

```
    # create SegmentationNode and Segment #

    self.segmentationNode = slicer.vtkMRMLSegmentationNode()
    slicer.mrmlScene.AddNode(self.segmentationNode)
    self.segmentPrintId = self.segmentationNode.GetSegmentation().AddEmptySegment("ThresholdSegment", "Segment_Threshold", [4,4,4])

    # threshold #

    import vtkSegmentationCorePython as vtkSegmentationCore
    segmentationsLogic = slicer.vtkSlicerSegmentationsModuleLogic()

    volumeNode = self.inputSelector.currentNode() #volumeNode of a selector
    inputImageData = volumeNode.GetImageData()

    outputImageData = vtkSegmentationCore.vtkOrientedImageData()

    thresh = vtk.vtkImageThreshold()
    thresh.SetInputData(volumeNode.GetImageData())
    thresh.ThresholdBetween(self.imageThresholdSliderWidget.minimumValue, self.imageThresholdSliderWidget.maximumValue)
    thresh.SetInValue(1000)
    thresh.SetOutValue(0)
    thresh.SetOutputScalarType(outputImageData.GetScalarType())
    thresh.Update()
    outputImageData.DeepCopy(thresh.GetOutput())

    segmentationsLogic.SetBinaryLabelmapToSegment(outputImageData, self.segmentationNode, self.segmentPrintId)

```

Thank you again!

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [May 4, 2017, 8:08pm UTC](https://discourse.slicer.org/t/threshold-on-segment/258/2 "2017-05-04T20:08:14Z")

</div>

Hi Basti,

Thanks for the detailed report!

First of all, it is best to use the actual module logic, that has the MRML scene and everything:  
`segmentationsLogic = slicer.modules.segmentations.logic()`

Second, I’d check if the output image actually contains a meaningful image, just for the sake of debugging. For example like this:  
`acc = vtk.vtkImageAccumulate() acc.SetInputData(outputImageData) acc.SetIgnoreZero(1) acc.Update() acc.GetVoxelCount() # Get number of non-zero voxels (should be >0) acc.SetIgnoreZero(0) acc.Update() acc.GetVoxelCount() # Get number of all voxels (should be different than the non-zero one) acc.GetMin() # Should be 0 acc.GetMax() # Should be 1000`

Third, what is the return value of the last call? True or False? if it’s false, is there any error in the log? Circle button with X in it in bottom right corner, or whole log in Help/Report a bug.

Fourth, I think I found a bug that might explain the problem if the above three are all good. I did this to get the IDs in the segmentation:  
`ids=vtk.vtkStringArray() segmentationNode.GetSegmentation().GetSegmentIDs(ids) ids.GetValue(0)`  
and it is not the same as segmentPrintId, which should be. It seems that the names and IDs are not handled correctly by vtkSegmentation::AddEmptySegment. I will debug into it and see what the exact problem is.

For a short term solution you can just get the IDs the same way I did for testing.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [May 4, 2017, 8:22pm UTC](https://discourse.slicer.org/t/threshold-on-segment/258/3 "2017-05-04T20:22:09Z")

</div>

The bug should be fixed in the commit below. Please download tomorrow’s nightly and try again.

> <https://github.com/Slicer/Slicer/commit/84f11249053c6da1c4f3abcb3edd9d20d226ef44>

---

<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:** [May 4, 2017, 9:02pm UTC](https://discourse.slicer.org/t/threshold-on-segment/258/4 "2017-05-04T21:02:35Z")

</div>

In tomorrow’s nightly version (that contains Csaba’s fixes) this will work:

```
import vtkSegmentationCorePython as vtkSegmentationCore

segmentationNode = slicer.vtkMRMLSegmentationNode()
slicer.mrmlScene.AddNode(segmentationNode)
segmentPrintId = segmentationNode.GetSegmentation().AddEmptySegment("ThresholdSegment", "Segment_Threshold", [1.0,1.0,0.0])

segmentationsLogic = slicer.modules.segmentations.logic()

volumeNode = getNode('MRHead')
inputImageData = segmentationsLogic.CreateOrientedImageDataFromVolumeNode(volumeNode)
inputImageData.UnRegister(None)

minThreshold = 20
maxThreshold = 80
thresh = vtk.vtkImageThreshold()
thresh.SetInputData(volumeNode.GetImageData())
thresh.ThresholdBetween(minThreshold, maxThreshold)
thresh.SetInValue(1000)
thresh.SetOutValue(0)
thresh.Update()

outputImageData = vtkSegmentationCore.vtkOrientedImageData()
outputImageData.ShallowCopy(thresh.GetOutput())
imageToWorldMatrix = vtk.vtkMatrix4x4()
inputImageData.GetImageToWorldMatrix(imageToWorldMatrix)
outputImageData.SetImageToWorldMatrix(imageToWorldMatrix)

segmentationsLogic.SetBinaryLabelmapToSegment(outputImageData, segmentationNode, segmentPrintId)
```

---

<div class="post-metadata">

**Author:** ![basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@basti](https://discourse.slicer.org/u/basti)\
**Post date:** [May 7, 2017, 10:01pm UTC](https://discourse.slicer.org/t/threshold-on-segment/258/5 "2017-05-07T22:01:57Z")

</div>

Thank you very much, now it works! I also added

```
segmentationNode.CreateDefaultDisplayNodes()

```

to display the Node.
