# Islands segmentation via python script

**URL:** <https://discourse.slicer.org/t/islands-segmentation-via-python-script/21021>\
**Category:** Support\
**Created:** [December 11, 2021, 6:28pm UTC](https://discourse.slicer.org/t/islands-segmentation-via-python-script/21021 "2021-12-11T18:28:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valentina\_Nepi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/valentina_nepi/32/13474_2.png) [@Valentina\_Nepi](https://discourse.slicer.org/u/Valentina_Nepi)\
**Post date:** [December 11, 2021, 6:28pm UTC](https://discourse.slicer.org/t/islands-segmentation-via-python-script/21021/1 "2021-12-11T18:28:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [December 13, 2021, 3:27pm UTC](https://discourse.slicer.org/t/islands-segmentation-via-python-script/21021/2 "2021-12-13T15:27:07Z")

</div>

Hi Valentina,

All this should be doable with a python script.  
I would involve the Segment Editor like this (untested):

```auto
# 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)

```

---

<div class="post-metadata">

**Author:** ![Valentina\_Nepi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/valentina_nepi/32/13474_2.png) [@Valentina\_Nepi](https://discourse.slicer.org/u/Valentina_Nepi)\
**Post date:** [December 13, 2021, 4:06pm UTC](https://discourse.slicer.org/t/islands-segmentation-via-python-script/21021/3 "2021-12-13T16:06:05Z")

</div>

> [@rbumm](#):
>
> All this should be doable with a python script.  
> I would involve the Segment Editor like this (untested):
> 
> ```auto
> # 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

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [December 13, 2021, 4:24pm UTC](https://discourse.slicer.org/t/islands-segmentation-via-python-script/21021/4 "2021-12-13T16:24:14Z")

</div>

> [@Valentina\_Nepi](#):
>
> do you have some good idea

Actually, yes. You need to call the SegmentStatisticsLogic() in your script which is quite easy (untested):

```auto
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")

```

---

<div class="post-metadata">

**Author:** ![Valentina\_Nepi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/valentina_nepi/32/13474_2.png) [@Valentina\_Nepi](https://discourse.slicer.org/u/Valentina_Nepi)\
**Post date:** [December 13, 2021, 4:43pm UTC](https://discourse.slicer.org/t/islands-segmentation-via-python-script/21021/5 "2021-12-13T16:43:27Z")

</div>

I’ll try soon! about bin width? could you tell me how to select it before the extraction?

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [December 13, 2021, 5:19pm UTC](https://discourse.slicer.org/t/islands-segmentation-via-python-script/21021/6 "2021-12-13T17:19:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Valentina\_Nepi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/valentina_nepi/32/13474_2.png) [@Valentina\_Nepi](https://discourse.slicer.org/u/Valentina_Nepi)\
**Post date:** [December 13, 2021, 6:39pm UTC](https://discourse.slicer.org/t/islands-segmentation-via-python-script/21021/7 "2021-12-13T18:39:56Z")

</div>

Ok Rudolf that you so much! your help has been preciuos!
