Skip to content

Instantly share code, notes, and snippets.

View lassoan's full-sized avatar

Andras Lasso lassoan

View GitHub Profile
@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 / 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 / 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 / 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")
# 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 / 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
@lassoan
lassoan / LineProfile.py
Last active August 25, 2022 02:45
3D Slicer module for computing intensity profile in a volume along a ruler line
#
#
#
#
#
# This example has been completely reworked and converted to a module, now available at:
#
#
# https://github.com/lassoan/SlicerLineProfile/blob/master/LineProfile/LineProfile.py
#
# Convert volume nodes of 3D Slicer to/from xarray and read/write into zarr format
def xarrayFromVolume(volumeNode) -> "xr.DataArray":
"""Convert an volume node to an xarray.DataArray.
Origin and spacing metadata is preserved in the xarray's coords. The
Direction is set in the `direction` attribute.
Dims are labeled as `x`, `y`, `z`, `t`, and `c`.
This interface is and behavior is experimental and is subject to possible
future changes."""
import xarray as xr
@lassoan
lassoan / coloredvolumerendering.py
Last active December 11, 2022 22:39
Colored volume rendering
# This script can be used with this example scene:
# https://github.com/lassoan/PublicTestingData/releases/download/data/ColoredVolumeRenderingScene.mrb
volumeNode = getNode('Panoramix-cropped')
segmentationNode = getNode('Panoramix-cropped segmentation')
boostSegmentOpacity = False # Increase voxel value inside segments to allow making them them more opaque
volumesLogic = slicer.modules.volumes.logic()
segmentIds = vtk.vtkStringArray()
@lassoan
lassoan / LoadRemoteFile.py
Last active February 13, 2023 17:07
Launch 3D Slicer from web browser to view 3D image file
The example was moved to Sandbox extension so that users can more easily install it:
https://github.com/PerkLab/SlicerSandbox/blob/master/LoadRemoteFile/LoadRemoteFile.py
More information: https://github.com/PerkLab/SlicerSandbox/blob/master/README.md#loadremotefile