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):
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:
-
using segmentEditor, using the “eraser” effect but I do not know how to automatically specify the area to erase
-
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),)
- 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!