Last active
May 24, 2022 17:53
-
-
Save lassoan/1673b25d8e7913cbc245b4f09ed853f9 to your computer and use it in GitHub Desktop.
This example demonstrates how to extract skin surface from an MRI image using thresholding and smoothing effect of Segment Editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SampleData | |
sampleDataLogic = SampleData.SampleDataLogic() | |
masterVolumeNode = sampleDataLogic.downloadMRHead() | |
# Create segmentation | |
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode") | |
segmentationNode.CreateDefaultDisplayNodes() # only needed for display | |
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode) | |
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("skin") | |
# Create segment editor to get access to effects | |
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget() | |
segmentEditorWidget.setMRMLScene(slicer.mrmlScene) | |
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode") | |
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode) | |
segmentEditorWidget.setSegmentationNode(segmentationNode) | |
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode) | |
# Thresholding | |
segmentEditorWidget.setActiveEffectByName("Threshold") | |
effect = segmentEditorWidget.activeEffect() | |
effect.setParameter("MinimumThreshold","35") | |
effect.setParameter("MaximumThreshold","695") | |
effect.self().onApply() | |
# Smoothing | |
segmentEditorWidget.setActiveEffectByName("Smoothing") | |
effect = segmentEditorWidget.activeEffect() | |
effect.setParameter("SmoothingMethod", "MEDIAN") | |
effect.setParameter("KernelSizeMm", 11) | |
effect.self().onApply() | |
# Clean up | |
segmentEditorWidget = None | |
slicer.mrmlScene.RemoveNode(segmentEditorNode) | |
# Make segmentation results visible in 3D | |
segmentationNode.CreateClosedSurfaceRepresentation() | |
# Write to STL file | |
slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsClosedSurfaceRepresentationToFiles("c:/tmp", segmentationNode, None, "STL") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HI for the last time. I cannot write to STL file.
it returns true but, nothing is outputted. no STL file exists in the path I specified.
Can you help ?