Hi,
I am stuck with this for several days now.
Would like to evaluate regions in a CT via a python script.
Created a module, works ok.
Four regions should be evaluated in my script, which principally works due to the excellent source code example of Andras Lasso, but if I try to mask this work by using a “Right lung mask” or a “Left lung mask” (which I have priorly segmented and which are stored under “Segmentation”), the threshold algorithm is always using the complete data set and does not work within the mask.
Here is the relevant part I added for the right lung (the right lung mask is the first one stored under “Segmentation”).
segmentationNode2 = slicer.util.getNode('Segmentation')
segmentEditorNode.SetMaskSegmentID(segmentationNode2.GetSegmentation().GetNthSegmentID(0))
segmentEditorNode.SetOverwriteMode(slicer.vtkMRMLSegmentEditorNode.OverwriteNone)
segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentEditorNode.PaintAllowedInsideSingleSegment)
Here is the complete source:
def process(self, inputVolume):
if not inputVolume:
raise ValueError("Input volume is invalid")
import time
startTime = time.time()
logging.info('Processing started')
# Compute
masterVolumeNode = inputVolume
# Create segmentation
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
#segmentationNode2 = slicer.util.getNode('Segmentation')
#slicer.util.messageBox("Seg Node:" + segmentationNode2.GetSegmentation().GetNthSegmentID(0), dontShowAgainSettingsKey = "MainWindow/DontShowSomeMessage")
# Create temporary segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
# Create segments by thresholding
segmentsFromHounsfieldUnits = [
["Emphysema right", -1000, -900],
["Ventilated right", -900, -600],
["Infiltration right", -600, -50],
["Collapsed right", -50, 3000] ]
for segmentName, thresholdMin, thresholdMax in segmentsFromHounsfieldUnits:
# Create segment
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment(segmentName)
segmentEditorNode.SetSelectedSegmentID(addedSegmentID)
# slicer.util.messageBox("Some message", dontShowAgainSettingsKey = "MainWindow/DontShowSomeMessage")
segmentationNode2 = slicer.util.getNode('Segmentation')
segmentEditorNode.SetMaskSegmentID(segmentationNode2.GetSegmentation().GetNthSegmentID(0))
segmentEditorNode.SetOverwriteMode(slicer.vtkMRMLSegmentEditorNode.OverwriteNone)
segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentEditorNode.PaintAllowedInsideSingleSegment)
# Fill by thresholding
segmentEditorWidget.setActiveEffectByName("Threshold")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumThreshold",str(thresholdMin))
effect.setParameter("MaximumThreshold",str(thresholdMax))
effect.self().onApply()
# Delete temporary segment editor
segmentEditorWidget = None
slicer.mrmlScene.RemoveNode(segmentEditorNode)
# Compute segment volumes
resultsTableNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTableNode')
import SegmentStatistics
segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
segStatLogic.getParameterNode().SetParameter("ScalarVolume", masterVolumeNode.GetID())
segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled","True")
segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin.voxel_count.enabled","False")
segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin.volume_mm3.enabled","False")
segStatLogic.computeStatistics()
segStatLogic.exportToTable(resultsTableNode)
segStatLogic.showTable(resultsTableNode)
# Export segmentation to a labelmap
#labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
#slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, masterVolumeNode)
#slicer.util.saveNode(labelmapVolumeNode, "c:/tmp/BodyComposition-label.nrrd")
stopTime = time.time()
logging.info('Processing completed in {0:.2f} seconds'.format(stopTime-startTime))
Thank you for any help.
Best regards
rudolf