Skip to content

Instantly share code, notes, and snippets.

@lassoan
Created May 26, 2019 13:48
Show Gist options
  • Save lassoan/8dde0bda226b20943a8b9dc430ed520b to your computer and use it in GitHub Desktop.
Save lassoan/8dde0bda226b20943a8b9dc430ed520b to your computer and use it in GitHub Desktop.
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)],
]
def autoContrastVtkImageHistogramStatisticsDefault(volNode, lowPercentile, highPercentile, lowExpansionFactor, highExpansionFactor):
stats = vtk.vtkImageHistogramStatistics()
stats.SetAutoRangePercentiles(lowPercentile, highPercentile)
stats.SetAutoRangeExpansionFactors(lowExpansionFactor, highExpansionFactor)
stats.SetInputConnection(volNode.GetImageDataConnection())
stats.Update()
displayNode = volNode.GetVolumeDisplayNode()
displayNode.AutoWindowLevelOff()
intensityRange = stats.GetAutoRange()
displayNode.SetWindowLevelMinMax(intensityRange[0], intensityRange[1])
def autoContrastSlicerDefault(volNode):
displayNode = volNode.GetDisplayNode()
displayNode.AutoWindowLevelOn()
import os
screenCapture = slicer.modules.screencapture.widgetRepresentation().self().logic
screenCapture.showViewControllers(False)
for filename in os.listdir(volumesDir):
print(filename)
[success, volNode] = slicer.util.loadVolume(volumesDir+"/"+filename, returnNode=True)
for (methodName, methodFunction) in methods:
methodFunction(volNode)
screenCapture.captureImageFromView(None, screenshotsDir+"/"+volNode.GetName()+"_"+methodName+".png")
slicer.mrmlScene.Clear()
screenCapture.showViewControllers(True)
# Experiment with a specific volume:
# volNode=slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
# autoContrastVtkImageHistogramStatisticsDefault(volNode,1.0,99.9,0.1,0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment