Hi everyone.
I am trying to create and island segmentation directly via python script bypassing the 3dslicer gui.
In the GUI i do these operations:
load two diffents files, a label map and a master volume
import the label map on the the master volume
create a new segmenation and edit chosing remover islands to delete islands smaller that a voxel value size
Everyone know if is it possible?
Thanks in advantage
rbumm
(Rudolf Bumm)
December 13, 2021, 3:27pm
2
Hi Valentina,
All this should be doable with a python script.
I would involve the Segment Editor like this (untested):
# Initialization
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(yourOutputSegmentation)
segmentEditorWidget.setMasterVolumeNode(yourVolume)
# Systematically remove small islands
segmentEditorNode.SetSelectedSegmentID("YourSegmentID")
segmentEditorWidget.setActiveEffectByName("Islands")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumSize","1000")
effect.setParameter("Operation","KEEP_LARGEST_ISLAND")
segmentEditorNode.SetOverwriteMode(slicer.vtkMRMLSegmentEditorNode.OverwriteNone)
segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentEditorNode.PaintAllowedEverywhere)
effect.self().onApply()
# Deactivates all effects
segmentEditorWidget.setActiveEffect(None)
segmentEditorWidget = None
slicer.mrmlScene.RemoveNode(segmentEditorNode)
1 Like
rbumm:
All this should be doable with a python script.
I would involve the Segment Editor like this (untested):
# Initialization
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(yourOutputSegmentation)
segmentEditorWidget.setMasterVolumeNode(yourVolume)
# Systematically remove small islands
segmentEditorNode.SetSelectedSegmentID("YourSegmentID")
segmentEditorWidget.setActiveEffectByName("Islands")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumSize","1000")
effect.setParameter("Operation","KEEP_LARGEST_ISLAND")
segmentEditorNode.SetOverwriteMode(slicer.vtkMRMLSegmentEditorNode.OverwriteNone)
segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentEditorNode.PaintAllowedEverywhere)
effect.self().onApply()
# Deactivates all effects
segmentEditorWidget.setActiveEffect(None)
segmentEditorWidget = None
slicer.mrmlScene.RemoveNode(segmentEditorNode)
thank you Rudolf! is similar to the one I’ve done! I will test your!
Since you are so kind, I ask you some suggestion! for the next step I would extract features for this new segmentation generated putting it as input region and then do a quantification of the segment statistics. do you have some good idea?
thank you in advantage
rbumm
(Rudolf Bumm)
December 13, 2021, 4:24pm
4
Actually, yes. You need to call the SegmentStatisticsLogic() in your script which is quite easy (untested):
resultsTable = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTableNode', 'Your analysis results')
# Compute stats
import SegmentStatistics
segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
segStatLogic.getParameterNode().SetParameter("Segmentation", yourOutputSegmentation.GetID())
segStatLogic.getParameterNode().SetParameter("ScalarVolume", yourInputVolume.GetID())
segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled", "True" if self.generateStatistics else "False")
segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin.voxel_count.enabled", "False")
segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin.volume_mm3.enabled", "False")
segStatLogic.computeStatistics()
segStatLogic.exportToTable(self.resultsTable)
self.outputStats = segStatLogic.getStatistics()
segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled", "True")
I’ll try soon! about bin width? could you tell me how to select it before the extraction?
rbumm
(Rudolf Bumm)
December 13, 2021, 5:19pm
6
It is not good to populate this thread with add-on questions because other users will not find the new follow-up topic. I suggest you try your way and maybe ask a new question in support.
1 Like
Ok Rudolf that you so much! your help has been preciuos!
1 Like