Skip to content

Instantly share code, notes, and snippets.

@lassoan
Last active February 4, 2024 18:54
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lassoan/0f45db8bae792ea19ccad36ceefbf52d to your computer and use it in GitHub Desktop.
Fills all internal holes inside segments. For example, fills in empty regions inside vertebral bodies segmented with Grow from seeds with intensity-based masking.
segmentationNode = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLSegmentationNode')
# set value to the size of the larges cracks in the segment surfaces
maximumHoleSizeMm = 2.0
############
masterVolumeNode = segmentationNode.GetNodeReference(segmentationNode.GetReferenceImageGeometryReferenceRole())
# Create segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
# To show segment editor widget (useful for debugging):
# segmentEditorWidget.show()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
slicer.mrmlScene.AddNode(segmentEditorNode)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
# Visible segments will be processed
inputSegmentIDs = vtk.vtkStringArray()
segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(inputSegmentIDs)
# Create a segment that will contain all vertebrae
allVertebraeSegmentId = segmentationNode.GetSegmentation().AddEmptySegment()
segmentEditorNode.SetOverwriteMode(slicer.vtkMRMLSegmentEditorNode.OverwriteNone)
segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentEditorNode.PaintAllowedEverywhere)
# Fill surface cracks in each segment and add to allVertebraeSegment
for index in range(inputSegmentIDs.GetNumberOfValues()):
segmentID = inputSegmentIDs.GetValue(index)
segmentEditorWidget.setCurrentSegmentID(segmentID)
# Grow the segment to fill in surface cracks
segmentEditorWidget.setActiveEffectByName("Margin")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MarginSizeMm",str(maximumHoleSizeMm))
effect.self().onApply()
# Invert the segment
segmentEditorWidget.setActiveEffectByName("Logical operators")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation", "INVERT")
effect.self().onApply()
# Remove islands in inverted segment (these are the holes inside the segment)
segmentEditorWidget.setActiveEffectByName("Islands")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation", "KEEP_LARGEST_ISLAND")
effect.self().onApply()
# Grow the inverted segment by the same margin as before to restore the original size
segmentEditorWidget.setActiveEffectByName("Margin")
effect = segmentEditorWidget.activeEffect()
effect.self().onApply()
# Invert the inverted segment (it will contain all the segment without the holes)
segmentEditorWidget.setActiveEffectByName("Logical operators")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation", "INVERT")
effect.self().onApply()
# Add it to the allVertebraeSegment
segmentEditorWidget.setCurrentSegmentID(allVertebraeSegmentId)
segmentEditorWidget.setActiveEffectByName("Logical operators")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation", "UNION")
effect.setParameter("ModifierSegmentID", segmentID)
effect.self().onApply()
# Grow from seeds, restricting growth to allVertebrae
segmentationNode.GetDisplayNode().SetSegmentVisibility(allVertebraeSegmentId, False)
segmentEditorNode.SetMaskSegmentID(allVertebraeSegmentId)
segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentEditorNode.PaintAllowedInsideSingleSegment)
segmentEditorWidget.setActiveEffectByName("Grow from seeds")
effect = segmentEditorWidget.activeEffect()
effect.self().onPreview()
effect.self().onApply()
# Remove the mask segment
segmentationNode.RemoveSegment(allVertebraeSegmentId)
slicer.mrmlScene.RemoveNode(segmentEditorNode)
del segmentEditorWidget
@rbumm
Copy link

rbumm commented Jun 30, 2021

Works great with a test spine !

@pieper
Copy link

pieper commented Jun 30, 2021

Awesome!

@ralkalay
Copy link

Andras thanks, works great, looking forward to working with the group!

@rphellan
Copy link

rphellan commented Mar 9, 2023

Awesome work, thanks!

I had to do the following changes to make it work with a recent version of Slicer:

Line 26: segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentEditorNode.PaintAllowedEverywhere), replace with:
segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentationNode.EditAllowedEverywhere)

Line 67: segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentEditorNode.PaintAllowedInsideSingleSegment), replace with: segmentEditorNode.SetMaskMode(slicer.vtkMRMLSegmentationNode.EditAllowedInsideSingleSegment)

@PiotrBorkowskiPB
Copy link

Hi.
I try to use code in version 5.4.0.
and without success.
Can somebody help.

@rbumm
Copy link

rbumm commented Oct 18, 2023

We had problems with the last TotalSegmentator version. Weights were not downloaded correctly.
Recently a bugfix was puhed to update TS to version 1.5.7.
This should be available tomorrow in the extension manager via download. Please force install Totalsegmentator after this bugfix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment