Skip to content

Instantly share code, notes, and snippets.

View lassoan's full-sized avatar

Andras Lasso lassoan

View GitHub Profile
@lassoan
lassoan / CurvedPlanarReformatting.py
Created October 22, 2019 13:05
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).
# 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 / 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
#
volumesDir = r"c:\Users\andra\OneDrive\Projects\SlicerTesting4\20190523-AutoWWWL\volumes"
screenshotsDir = r"c:\Users\andra\OneDrive\Projects\SlicerTesting4\20190523-AutoWWWL\screenshots"
methods = [
["baseline", autoContrastSlicerDefault],
["hist-0.1-99.9", lambda fn: autoContrastVtkImageHistogramStatisticsDefault(fn, 0.1, 99.9, 0.00, 0.0)], # = itksnap
["hist-1.0-99.9", lambda fn: autoContrastVtkImageHistogramStatisticsDefault(fn, 1.0, 99.9, 0.10, 0.0)],
["hist-1.0-99.0-x0.10", lambda fn: autoContrastVtkImageHistogramStatisticsDefault(fn, 1.0, 99.0, 0.10, 0.10)],
["hist-1.0-99.0-x0.20", lambda fn: autoContrastVtkImageHistogramStatisticsDefault(fn, 1.0, 99.0, 0.10, 0.20)],
]
@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 / SimpleView.py
Last active December 20, 2018 18:32
Simple 3D Slicer based image viewer
def showSimpleUserInterface(simpleView):
# Do not store these display settings permanently
settings = qt.QSettings()
settings.setValue('MainWindow/RestoreGeometry', not simpleView)
for toolbar in slicer.util.mainWindow().findChildren('QToolBar'):
toolbar.setVisible(not simpleView)
modulePanelDockWidget = slicer.util.mainWindow().findChildren('QDockWidget','PanelDockWidget')[0]
@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 / ReconstructSurface.py
Created March 16, 2018 02:40
Reconstruct open surface from labelmap
# Important: the input volume must have isotropic spacing.
# If surface is thick, use Extract skeleton module with Skeleton type = 2D, Do not prune branches = enabled.
# Increase radius parameter to fill more holes.
# Increase dimension to preserve more details.
inputLabelmap = getNode('Input labelmap')
ici = vtk.vtkImageChangeInformation()
ici.SetInputData(inputLabelmap.GetImageData())
ici.SetOutputSpacing(inputLabelmap.GetSpacing())
@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()
@lassoan
lassoan / CardiacAgatstonScoring
Last active September 18, 2023 03:36
Cardiac Agatston scoring function for 3D Slicer
# There are several variants of the metric. If you need to compute the metric slice by slice then
# you can use Mask volume effect to create a volume where all voxels are blanked out (set to -1000)
# except the calcifications in the selected vessel and compute the total score using this script.
# (based on Agatston (1990) - https://www.sciencedirect.com/science/article/pii/073510979090282T)
#
# Sample data set is available at:
# https://github.com/lassoan/PublicTestingData/releases/download/data/CardiacAgatstonScore.mrb
def computeAgatstonScore(volumeNode, minimumIntensityThreshold=130, minimumIslandSizeInMm2=1.0, verbose=False):
import numpy as np
@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")