# Automatically erase a part of a segmentation

**URL:** <https://discourse.slicer.org/t/automatically-erase-a-part-of-a-segmentation/40218>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [November 15, 2024, 4:11pm UTC](https://discourse.slicer.org/t/automatically-erase-a-part-of-a-segmentation/40218 "2024-11-15T16:11:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fleur\_Gaudfernau](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fleur_gaudfernau/32/78527_2.png) [@Fleur\_Gaudfernau](https://discourse.slicer.org/u/Fleur_Gaudfernau)\
**Post date:** [November 15, 2024, 4:11pm UTC](https://discourse.slicer.org/t/automatically-erase-a-part-of-a-segmentation/40218/1 "2024-11-15T16:11:15Z")

</div>

Hello,  
I am fairly new to using python scripts in Slicer.  
I have rather simple brain segmentation (1 label) and I want to physically separate the brain hemispheres, i.e. remove the **junction** between the hemispheres and obtain two **separate** segments/islands (one for each hemisphere) instead of one. As my segmentations have been registered to a common space, I do **know** which areas of the segmentation map have to be erased.

I basically want to go from this (first row) to that (second row):

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/a/0a177c5b6272593fd8a2122ffea15a31d82ff079.png)

The python code I am trying to transpose to Slicer is the following:

> image = nib.load(“path/to/segmentation.nii.gz”)  
> data = image.get\_fdata()  
> data[72:77, 105:140, 75:98] = 0  
> data[62:67, 105:140, 75:100] = 0

Basically I have tried:

1. using segmentEditor, using the “eraser” effect but I do not know how to automatically specify the area to erase

2. Create a new segment, and use the Paint tool to “write over” the existing segment.

> segmentationNode = slicer.util.loadSegmentation(path\_to\_file)  
> VolumeNode = slicer.util.loadVolume(path\_to\_volume)  
> segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()  
> segmentEditorWidget.setMRMLScene(slicer.mrmlScene)  
> segmentEditorNode =slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentEditorNode”)  
> segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)  
> segmentEditorWidget.setSegmentationNode(segmentationNode)  
> segmentEditorWidget.setMasterVolumeNode(VolumeNode)  
> theSegmentation = segmentationNode.GetSegmentation()  
> theSegmentation.AddEmptySegment(“Middle”)  
> segmentEditorNode.SetSelectedSegmentID(“Middle”)  
> segmentEditorWidget.setActiveEffectByName(“Paint”)  
> effect = segmentEditorWidget.activeEffect()  
> effect.setParameter(“BrushRelativeDiameter”, “1”) # Brush size relative to the volume size  
> effect.setParameter(“BrushSphere”, “1”) # Use a spherical brush  
> effect.paintApply((70,70,70))

I get an error with the last line. Same issue here, I do not know how to specify the coordinates for the Paint tool.  
**ValueError: Called paintApply(qMRMLWidget viewWidget) → void with wrong arguments: ((70, 70, 70),)**

1. Create a vtk polygon and use segmentationNode.AddSegmentFromClosedSurfaceRepresentation(Polygon, …) to create a new segment. I also get an error.

Any help would be greatly appreciated!
