# Extract brain cavity using script

**URL:** <https://discourse.slicer.org/t/extract-brain-cavity-using-script/17270>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [April 23, 2021, 8:43am UTC](https://discourse.slicer.org/t/extract-brain-cavity-using-script/17270 "2021-04-23T08:43:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![park](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/park/32/5717_2.png) [@park](https://discourse.slicer.org/u/park)\
**Post date:** [April 23, 2021, 8:43am UTC](https://discourse.slicer.org/t/extract-brain-cavity-using-script/17270/1 "2021-04-23T08:43:58Z")

</div>

Hi everyone

I would like to extract the cavity of brain space using “Wrap solidify” and Python script.

This is my code to implement what I thought  
Before performing the “Wrap solidify”, the segmentation using "Threshold " works well. However, “Wrap solidify” gives the error “ValueError: Region segment cannot be the same segment as the current segment”

Could I get a solution to this error?

> # Compute
> 
> masterVolumeNode = getNode(“InputVolume”)
> 
> # Create segmentation
> 
> segmentationNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentationNode”)  
> #segmentationNode = slicer.mrmlScene.GetFirstNodeByClass(“vtkMRMLSegmentationNode”)  
> #segmentationNode = slicer.util.getNode(‘Segmentation\_1’)  
> segmentationNode.CreateDefaultDisplayNodes() # only needed for display  
> segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
> 
> # Create temporary segment editor to get access to effects
> 
> segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()  
> segmentEditorWidget.setMRMLScene(slicer.mrmlScene)  
> segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentEditorNode”)
> 
> # Do masking
> 
> addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment(“skull”)  
> segmentEditorNode.SetMaskSegmentID(addedSegmentID)  
> segmentEditorNode.SetOverwriteMode(slicer.vtkMRMLSegmentEditorNode.OverwriteAllSegments)  
> segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentEditorNode.PaintAllowedInsideSingleSegment)
> 
> segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)  
> segmentEditorWidget.setSegmentationNode(segmentationNode)  
> segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)  
> segmentationDisplayNode=segmentationNode.GetDisplayNode()  
> segmentation=segmentationNode.GetSegmentation()
> 
> segmentEditorWidget.setActiveEffectByName(“Threshold”)  
> effect = segmentEditorWidget.activeEffect()  
> effect.setParameter(“MinimumThreshold”, 250)  
> effect.setParameter(“MaximumThreshold”, 3000)  
> effect.self().onApply()
> 
> segmentEditorWidget.setActiveEffectByName(“Wrap Solidify”)  
> effect2 = segmentEditorWidget.activeEffect()  
> effect2.setParameter(“Largest cavity”, True)  
> effect2.setParameter(“Carve holes”, True)  
> effect2.setParameter(“Carve holes”, 25)
> 
> effect2.self().onApply()

---

<div class="post-metadata">

**Author:** ![Miri\_Trope](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/miri_trope/32/67690_2.png) [@Miri\_Trope](https://discourse.slicer.org/u/Miri_Trope)\
**Post date:** [February 15, 2025, 6:10pm UTC](https://discourse.slicer.org/t/extract-brain-cavity-using-script/17270/2 "2025-02-15T18:10:35Z")

</div>

I’ve implemented something similar to this [approach](https://gist.github.com/lassoan/1673b25d8e7913cbc245b4f09ed853f9) and encountered the same error. I noticed that there hasn’t been a response yet—maybe someone can help now?

> Traceback (most recent call last):  
> File “/Applications/Slicer.app/Contents/Extensions-33216/SurfaceWrapSolidify/lib/Slicer-5.8/qt-scripted-modules/SegmentEditorWrapSolidifyLib/SegmentEditorEffect.py”, line 252, in onApply  
> self.logic.applyWrapSolidify()  
> File “/Applications/Slicer.app/Contents/Extensions-33216/SurfaceWrapSolidify/lib/Slicer-5.8/qt-scripted-modules/SegmentEditorWrapSolidifyLib/SegmentEditorEffect.py”, line 338, in applyWrapSolidify  
> regionPd = self.\_getInitialRegionPd()  
> File “/Applications/Slicer.app/Contents/Extensions-33216/SurfaceWrapSolidify/lib/Slicer-5.8/qt-scripted-modules/SegmentEditorWrapSolidifyLib/SegmentEditorEffect.py”, line 526, in \_getInitialRegionPd  
> raise ValueError(“Region segment cannot be the same segment as the current segment”)  
> ValueError: Region segment cannot be the same segment as the current segment
