Skip to content

Instantly share code, notes, and snippets.

Avatar

Andras Lasso lassoan

View GitHub Profile
View TableRemoveFromCT.py
# This script automatically removes table from a CT volume
# Generate input data
################################################
import SampleData
# Get input image (in this example, download a sample data set)
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadCTACardio()
@lassoan
lassoan / ExtractSkin.py
Last active Oct 24, 2020
This example demonstrates how to extract skin surface from an MRI image using thresholding and smoothing effect of Segment Editor
View ExtractSkin.py
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")
@lassoan
lassoan / NvidiaAiaaTumorSegmentation.py
Last active Sep 18, 2020
This example demonstrates how to do segment brain tumor using Nvidia's AI-assisted annotation tool in batch mode (without GUI, using qMRMLSegmentEditorWidget) using 3D Slicer
View NvidiaAiaaTumorSegmentation.py
# Load/generate input data
################################################
# Load master volume
import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadMRBrainTumor1()
# Define boundary points
import numpy as np
@lassoan
lassoan / CurvedPlanarReformatting.py
Created Oct 22, 2019
Computing a panoramic X-ray from a cone-beam dental CT. Demonstration of how an image can be resampled along a curve - the code is not optimized for performance or quality and distance along curve is not scaled (we simply used all point indices instead of retrieving point indices based on desired distance along curve).
View CurvedPlanarReformatting.py
# Get a dental CT scan
import SampleData
volumeNode = SampleData.SampleDataLogic().downloadDentalSurgery()[1]
# Define curve
curveNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLMarkupsCurveNode')
curveNode.CreateDefaultDisplayNodes()
curveNode.GetCurveGenerator().SetNumberOfPointsPerInterpolatingSegment(25) # add more curve points between control points than the default 10
curveNode.AddControlPoint(vtk.vtkVector3d(-45.85526315789473, -104.59210526315789, 74.67105263157896))
curveNode.AddControlPoint(vtk.vtkVector3d(-50.9078947368421, -90.06578947368418, 66.4605263157895))
@lassoan
lassoan / SegmentGrowCutSimple.py
Last active Jul 14, 2020
This example demonstrates how to run Grow from seeds effect in batch mode (without GUI, using qMRMLSegmentEditorWidget) using 3D Slicer
View SegmentGrowCutSimple.py
# Generate input data
################################################
import SampleData
# Load master volume
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadMRBrainTumor1()
# Create segmentation
@lassoan
lassoan / volumeAugment.py
Last active Jun 6, 2020
Training data augmentation with random volume translation, rotation, and deformation
View volumeAugment.py
# This script randomly warps a 3D volume and adds random translations, rotations,
# and save each resulting 3D volume (and a screenshot for quick overview)
#
# The script can be executed by copy-pasting into 3D Slicer's Python console
# or in a Jupyter notebook running 3D Slicer kernel (provided by SlicerJupyter extension).
#
# Prerequisites:
# - Recent Slicer-4.11 version
# - SlicerIGT extension installed (for random deformations)
@lassoan
lassoan / SegmentQuantification.py
Last active Apr 17, 2020
This example shows how to compute volume and surface area of a segment in 3D Slicer
View SegmentQuantification.py
# The module is now moved to SlicerSandbox extension:
#
# https://github.com/PerkLab/SlicerSandbox/tree/master/SegmentCrossSectionArea
#
@lassoan
lassoan / endocranium.py
Last active Apr 12, 2020
Automatic endocranium segmentation from dry bone CT scan
View endocranium.py
# Load dry bone CT of skull into the scene and run this script to automatically segment endocranium
masterVolumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
smoothingKernelSizeMm = 3.0 # this is used for closing small holes in the se
# Compute bone threshold value automatically
import vtkITK
thresholdCalculator = vtkITK.vtkITKImageThresholdCalculator()
thresholdCalculator.SetInputData(masterVolumeNode.GetImageData())
thresholdCalculator.SetMethodToOtsu()
@lassoan
lassoan / create-partof-hierarchy.py
Created Mar 30, 2020
Create 3D Slicer subject hierarchy from BodyParts3D
View create-partof-hierarchy.py
# Source data can be obtained from http://lifesciencedb.jp/bp3d/ (tested with partof_BP3D_4.0_obj_99)
# These input file must be loaded into the scene:
# - partof_inclusion_relation_list.txt and partof_element_parts.txt (loaded as Table nodes)
# - OBJ files
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
sceneItemID = shNode.GetSceneItemID()
def getItemParentsFmaIds(shNode, itemShItemId):
existingParentShItemId = shNode.GetItemParent(itemShItemId)
@lassoan
lassoan / SegmentByThresholding.py
Last active Mar 23, 2020
This example shows how to estimate fat, muscle, and bone volume in a CT image by simple thresholding.
View SegmentByThresholding.py
# Download a sample data set (chest CT)
import SampleData
masterVolumeNode = SampleData.SampleDataLogic().downloadCTChest()
# Create segmentation
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
# Create temporary segment editor to get access to effects
You can’t perform that action at this time.