Skip to content

Instantly share code, notes, and snippets.

View lassoan's full-sized avatar

Andras Lasso lassoan

View GitHub Profile
@lassoan
lassoan / NvidiaAiaaTumorSegmentation.py
Last active June 6, 2022 13:41
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
# Load/generate input data
################################################
# Load master volume
import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadMRBrainTumor1()
# Define boundary points
import numpy as np
# This gist contained a script that automatically removes table from a CT volume.
# The code was improved and turned into a Slicer module, available in the Sandbox extension:
# https://github.com/PerkLab/SlicerSandbox#remove-ct-table
#
# Source code:
# https://github.com/PerkLab/SlicerSandbox/blob/master/RemoveCtTable/RemoveCtTable.py#L251
@lassoan
lassoan / ExtractSkin.py
Last active May 24, 2022 17:53
This example demonstrates how to extract skin surface from an MRI image using thresholding and smoothing effect of Segment Editor
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 / SegmentGrowCut.py
Last active August 19, 2021 18:46
This example script demonstrates how a grow-cut operation can be performed without graphical user interface using 3D Slicer
# This example script demonstrates how a grow-cut
# operation can be performed without graphical user interface.
# The first half of the script downloads input data and creates seed segments.
# The second half of the script converts segments to merged labelmap (that's the required
# input format for grow-cut filter), computes the complete segmentation, and writes
# results into new segments.
# Generate input data
################################################
@lassoan
lassoan / SegmentQuantification.py
Last active April 17, 2020 23:03
This example shows how to compute volume and surface area of a segment in 3D Slicer
# The module is now moved to SlicerSandbox extension:
#
# https://github.com/PerkLab/SlicerSandbox/tree/master/SegmentCrossSectionArea
#
@lassoan
lassoan / endocranium.py
Last active April 12, 2020 14:12
Automatic endocranium segmentation from dry bone CT scan
# 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 March 30, 2020 22:12
Create 3D Slicer subject hierarchy from BodyParts3D
# 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 March 23, 2020 22:00
This example shows how to estimate fat, muscle, and bone volume in a CT image by simple thresholding.
# 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
@lassoan
lassoan / SliceAreaPlot.py
Created August 6, 2019 03:43 — forked from hherhold/SliceAreaPlot.py
Example module that computes and plots the cross sectional area of each visible segment. Direction of cross-section can be picked.
import os
import unittest
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
from array import array
import logging
import vtk.util.numpy_support
import numpy as np
#
@lassoan
lassoan / MaskVolumeHistogramPlot.py
Last active September 3, 2019 16:08
Histogram plot of volume masked by segments
# Generate input data
################################################
import SampleData
import numpy as np
# Load master volume
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadMRBrainTumor1()