Skip to content

Instantly share code, notes, and snippets.

@ungi
ungi / VolumeResliceWithTracking.py
Last active July 24, 2023 10:27
Volume reslice with tracking
# Take a slice out of a volume
import SampleData
# Input parameters
observerTag = None
outputSpacing = [2.0, 2.0, 2.0] # Millimeters/pixel
outputExtent = [0, 99, 0, 99, 0, 0] # First and last pixel indices along each axis
from optparse import OptionParser
# import os
import sys
import zipfile
import xml.etree.ElementTree as ET
parser = OptionParser()
parser.add_option( "-i", "--input", dest="InputFilename", help="Input file", metavar="FILE" )
parser.add_option( "-o", "--output", dest="OutputFilename", help="Output file", metavar="FILE" )
@ungi
ungi / CreateFrustumModel.py
Created May 24, 2019 18:02
Creates a VTK model in the shape of a frustum of a cone (truncated cone).
bottomRadius = 20
topRadius = 12
height = 30
resolution=32
points = vtk.vtkPoints()
lines = vtk.vtkCellArray()
pts = list()
import numpy as np
import os
transform_names = ['LinearTransform_3']
export_fullpath = r"j:\Temp"
browserNode = slicer.util.getFirstNodeByName('SequenceBrowser', className='vtkMRMLSequenceBrowserNode')
numItems = browserNode.GetNumberOfItems()
n = numItems - 1
# Show the distance between models as colors
# Create two models
createModelsLogic = slicer.modules.createmodels.logic()
cubeNode = createModelsLogic.CreateCube(150, 150, 150)
sphereNode = createModelsLogic.CreateSphere(100)
# Hide original models, so only the output will be visible
@ungi
ungi / LandmarkRegistrationExperiment.py
Last active August 9, 2022 17:38
Experiment with fiducial (landmark) registration
# Create transform node for registration result (optional)
alphaToBeta = slicer.vtkMRMLLinearTransformNode()
alphaToBeta.SetName('AlphaToBeta')
slicer.mrmlScene.AddNode(alphaToBeta)
# Experiment parameters (start from here if you have alphaToBeta already)
N = 10 # Number of fiducials
Scale = 100.0 # Size of space where fiducial are placed
@ungi
ungi / ObserveTransformNode.py
Created January 24, 2017 03:27
Copy and paste this code snippet in the Python interactor. Watch what happens when you change the ‘ModelToRas’ transform using e.g. the Transforms module.
# Create coordinate system models to visualize the transforms
createModelsLogic = slicer.modules.createmodels.logic()
preModelNode = createModelsLogic.CreateCoordinate(20,2)
preModelNode.SetName('PreModel')
preModelNode.GetDisplayNode().SetColor(1,1,0)
postModelNode = createModelsLogic.CreateCoordinate(20,2)
postModelNode.SetName('PostModel')
@ungi
ungi / PrePostMultiply.py
Created January 24, 2017 02:31
And example explaining the difference between PreMultiply and PostMultiply methods of vtkTransform objects
# Example to show difference between PreMultiply and PostMultiply
# Create coordinate models using the CreateModels module
createModelsLogic = slicer.modules.createmodels.logic()
preModelNode = createModelsLogic.CreateCoordinate(20,2)
preModelNode.SetName('PreModel')
postModelNode = createModelsLogic.CreateCoordinate(20,2)
postModelNode.SetName('PostModel')
originModelNode = createModelsLogic.CreateCoordinate(20,2)
# Copy and paste into Slicer Python interactor
# Drag visible fiducials to deform transform
# Scale defines how large cube will be created
# numPerEdge defines how many fiducials to put on each edge of the cube
scale = 30.0
numPerEdge = 3
# Create the FROM fiducial list, and hide it so it doesn't change with mouse interactions