Skip to content

Instantly share code, notes, and snippets.

@lassoan
Last active May 6, 2023 15:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lassoan/2d5a5b73645f65a5eb6f8d5f97abf31b to your computer and use it in GitHub Desktop.
Save lassoan/2d5a5b73645f65a5eb6f8d5f97abf31b to your computer and use it in GitHub Desktop.
This example demonstrates how to run Grow from seeds effect in batch mode (without GUI, using qMRMLSegmentEditorWidget) using 3D Slicer
# Generate input data
################################################
import SampleData
# Load master volume
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadMRBrainTumor1()
# Create segmentation
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
# Create seed segment inside tumor
tumorSeed = vtk.vtkSphereSource()
tumorSeed.SetCenter(-6, 30, 28)
tumorSeed.SetRadius(10)
tumorSeed.Update()
segmentationNode.AddSegmentFromClosedSurfaceRepresentation(tumorSeed.GetOutput(), "Tumor", [1.0,0.0,0.0])
# Create seed segment outside tumor
backgroundSeedPositions = [[0,65,32], [1, -14, 30], [0, 28, -7], [0,30,64], [31, 33, 27], [-42, 30, 27]]
append = vtk.vtkAppendPolyData()
for backgroundSeedPosition in backgroundSeedPositions:
backgroundSeed = vtk.vtkSphereSource()
backgroundSeed.SetCenter(backgroundSeedPosition)
backgroundSeed.SetRadius(10)
backgroundSeed.Update()
append.AddInputData(backgroundSeed.GetOutput())
append.Update()
backgroundSegmentId = segmentationNode.AddSegmentFromClosedSurfaceRepresentation(append.GetOutput(), "Background", [0.0,1.0,0.0])
# Run filter
################################################
# 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.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
# Run segmentation
segmentEditorWidget.setActiveEffectByName("Grow from seeds")
effect = segmentEditorWidget.activeEffect()
# You can change parameters by calling: effect.setParameter("MyParameterName", someValue)
# Most effect don't have onPreview, you can just call onApply
effect.self().onPreview()
effect.self().onApply()
# Clean up and show results
################################################
# Clean up
slicer.mrmlScene.RemoveNode(segmentEditorNode)
# Make segmentation results nicely visible in 3D
segmentationDisplayNode = segmentationNode.GetDisplayNode()
segmentationDisplayNode.SetSegmentVisibility(backgroundSegmentId, False)
@kbehlmirusmed
Copy link

To run a file like this, would you download it to a text editor and run the code from the text editor itself or through the 3D slicer module? If the latter, do you ever encounter issues where the display disappears with the reload and test?

@lassoan
Copy link
Author

lassoan commented Jul 12, 2020

This is a code snippet, not a module, so you cannot load it as a module (it has not GUI, so if you replace a module with this script then the previous module GUI will disappear and nothing will be displayed instead).

The quickest is to copy-paste the code from here to Slicer's Python console. If you want to edit/rerun it with different parameters then I would recommend to use Jupyter notebooks with Slicer chosen as kernel.

@Lhomer
Copy link

Lhomer commented Jun 29, 2021

Thanks for making this, would there be a way to manually paint the seeds and then run the script using the painted seeds?

@lassoan
Copy link
Author

lassoan commented Jun 29, 2021

Yes, of course. You can then simply use Segment Editor module in 3D Slicer and the "Grow from seeds" effect. See a simple example here: https://youtu.be/cybL5A0w3hw

@DrMohamedAssadawy
Copy link

what are the best segmentation methods of maxillary bone?

@lassoan
Copy link
Author

lassoan commented Mar 28, 2022

@DrMohamedAssadawy please post this question on the Slicer Forum.

@AApingp
Copy link

AApingp commented Apr 26, 2023

When I run this code using my own CT data, there is an error as follows. How can I solve it?
File "qt-scripted-modules\SegmentEditorEffects\AbstractScriptedSegmentEditorAutoCompleteEffect.py", line 301, in onApply
previewContainsClosedSurfaceRepresentation = previewNode.GetSegmentation().ContainsRepresentation(
AttributeError: 'NoneType' object has no attribute 'GetSegmentation'

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