# How to split segmentation the islands?

**URL:** <https://discourse.slicer.org/t/how-to-split-segmentation-the-islands/17952>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [June 4, 2021, 8:16am UTC](https://discourse.slicer.org/t/how-to-split-segmentation-the-islands/17952 "2021-06-04T08:16:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [June 4, 2021, 8:16am UTC](https://discourse.slicer.org/t/how-to-split-segmentation-the-islands/17952/1 "2021-06-04T08:16:29Z")

</div>

> [@Scissors segment editor effects from a script](https://discourse.slicer.org/t/scissors-segment-editor-effects-from-a-script/8348/17):
>
> `segmentEditorNode.SetMasterVolumeIntensityMask(True)`

Hi Andras,  
I need to get the volume convert to labelmap import to segmentationnode and then split it into islands.

I am using the script below but its not working it crashes. could you please help.

Thank you

labelVolumeNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLLabelMapVolumeNode”)  
slicer.vtkSlicerVolumesLogic().CreateLabelVolumeFromVolume(slicer.mrmlScene, labelVolumeNode, inputVolume)  
#convert volume to labelmap

# Create segmentation

```
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(inputVolume)

# 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(inputVolume)

#import volume to labelmap     
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelVolumeNode, segmentationNode) 
segmentEditorWidget.setCurrentSegmentID(segmentationNode.GetSegmentation().GetNthSegmentID(0))
segmentEditorNode.SetMasterVolumeIntensityMask(True)
# check the volume scalar range and based on that use the island filter to split the segment
# get scalar range of a volume
img = inputVolume.GetImageData()
rng = img.GetScalarRange()
r2 = rng[1]
print(r2)
if r2==1:
    #do the island thing for the volume as only one segment will be crreated and we need to split the segment into islands
    segmentEditorWidget.setActiveEffectByName("Islands")
    effect = segmentEditorWidget.activeEffect()
    operationName = 'SPLIT_ISLANDS_TO_SEGMENTS'
    minsize = 500
    effect.setParameter("Operation", operationName)
    effect.setParameter("MinimumSize",minsize)
    effect.self().onApply()

```

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [June 8, 2021, 5:19am UTC](https://discourse.slicer.org/t/how-to-split-segmentation-the-islands/17952/2 "2021-06-08T05:19:49Z")

</div>

> [@Saima](#):
>
> Volume)

I found the problem. The split island filter is not working properly. Its not creating islands it ignores 113 island 0 created. why is this happening although it should split the segment.

Thank you

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [June 8, 2021, 5:52am UTC](https://discourse.slicer.org/t/how-to-split-segmentation-the-islands/17952/3 "2021-06-08T05:52:04Z")

</div>

I have reduced the minimumsize it works but from the script it crashes. is there any problem with the script below.

Thank you

labelVolumeNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLLabelMapVolumeNode”)  
slicer.vtkSlicerVolumesLogic().CreateLabelVolumeFromVolume(slicer.mrmlScene, labelVolumeNode, inputVolume)  
#convert volume to labelmap

# Create segmentation

```
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(inputVolume)

# 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(inputVolume)

#import volume to labelmap     
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelVolumeNode, segmentationNode) 
segmentationNode.CreateClosedSurfaceRepresentation()
segmentEditorWidget.setCurrentSegmentID(segmentationNode.GetSegmentation().GetNthSegmentID(0))
#segmentEditorNode.SetMasterVolumeIntensityMask(True)
# check the volume scalar range and based on that use the island filter to split the segment
# get scalar range of a volume
img = inputVolume.GetImageData()
rng = img.GetScalarRange()
r2 = rng[1]
print(r2)

if int(r2)==1:
    #do the island thing for the volume as only one segment will be crreated and we need to split the segment into islands
    segmentEditorWidget.setActiveEffectByName("Islands")
    effect = segmentEditorWidget.activeEffect()
    operationName = 'SPLIT_ISLANDS_TO_SEGMENTS'
    minsize = 45
    effect.setParameter("Operation", operationName)
    effect.setParameter("MinimumSize",minsize)
    effect.self().onApply()

```

---

<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 8, 2021, 8:21pm UTC](https://discourse.slicer.org/t/how-to-split-segmentation-the-islands/17952/4 "2021-06-08T20:21:09Z")

</div>

The application probably does not actually crash, but it slows down extremely due to the large number of created segments. If you don’t filter out the noise and set the minimum size to be 0 then you can get a separate segment for each single-voxel speckle that you have in the image. You can end up with hundreds of thousands of segments, which will make everything extremely slow.

---

<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, 10:58pm UTC](https://discourse.slicer.org/t/how-to-split-segmentation-the-islands/17952/5 "2021-12-11T22:58:29Z")

</div>

sorry for the intrusion 🙂 I would like to reproduce this islands generation directly in pyradiomics without passing for 3d slicer. how can I do this? adn mainly, is it possible?

---

<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:** [December 14, 2021, 2:27pm UTC](https://discourse.slicer.org/t/how-to-split-segmentation-the-islands/17952/6 "2021-12-14T14:27:14Z")

</div>

There should be connected component filters in SimpleITK or ITK-Python that you can use in any Python environment to split a label image to disconnected islands. You can ask on the [ITK forum](https://discourse.itk.org/) for details.
