How to split segmentation the islands?

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

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

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

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.

1 Like

sorry for the intrusion :slight_smile: 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?

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 for details.