-
Notifications
You must be signed in to change notification settings - Fork 2
/
command line_loopfolders_volumerendering.py
120 lines (100 loc) · 4.91 KB
/
command line_loopfolders_volumerendering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import argparse
import os
import ScreenCapture
#Create command line arguments using argparse module and then parses them using parse_args() method.
parser = argparse.ArgumentParser(description='load volumes')
parser.add_argument('-f', '--folder', type=str, help='main volume path', required=True)
args = parser.parse_args()
#set the argument for the folder path
basedir = args.folder
dir_list = os.listdir(basedir)
#print files we're working with
print("Files and directories in '", basedir, "' :")
print(dir_list)
#loop through each of the folders in the base directories to prociess
for i in dir_list:
#change colours of 3d viewer to be a black-white gradient
viewNode = slicer.app.layoutManager().threeDWidget(0).mrmlViewNode()
viewNode.SetBackgroundColor(0,0,0)
viewNode.SetBackgroundColor2(0,0,0)
#remove bounding box and orientation axes labels (a,s,r)
viewNode.SetBoxVisible(0)
viewNode.SetAxisLabelsVisible(0)
subfolder = f'{basedir}\\{i}'#get info for file naming
print(subfolder)
print(i)
print(subfolder+'\\'+i)
contents = os.listdir(subfolder)
volpath = subfolder+'\\'+contents[0]#get
print(volpath)
#load volume
slicer.util.loadVolume(rf'{volpath}')
#view the 3d layout only
slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutOneUp3DView)
#center 3d view and slice views, to fill background
def reset_views():
layoutManager = slicer.app.layoutManager()
threeDWidget = layoutManager.threeDWidget(0)
threeDView = threeDWidget.threeDView()
threeDView.resetFocalPoint()#just resets the focal point, camera might be tilted off still
threeDView.resetCamera()#reset to snap to face on, anterior position
#reset slice views
for col in ["Red","Yellow","Green"]:
slicer.app.layoutManager().sliceWidget(col).fitSliceToBackground()
#apply function
reset_views()
# Set up volume rendering and display
volumeNode = slicer.mrmlScene.GetNthNodeByClass(0, 'vtkMRMLScalarVolumeNode')
volRenLogic = slicer.modules.volumerendering.logic()
# Get/create volume rendering display node
volRenLogic = slicer.modules.volumerendering.logic()
displayNode = volRenLogic.CreateDefaultVolumeRenderingNodes(volumeNode)
displayNode.SetVisibility(True)
# Set up volume rendering preset
displayNode.GetVolumePropertyNode().Copy(volRenLogic.GetPresetByName('CT-X-ray'))
volpropertynode = slicer.mrmlScene.GetNthNodeByClass(0, 'vtkMRMLVolumePropertyNode')
reset_views()
# Set up scalar opacity mapping. This is a 6 point transfer function copied from settings after manual set to 210 minimum and
#ther upper and lower
scalarOpacity = vtk.vtkPiecewiseFunction()
scalarOpacity.AllowDuplicateScalarsOn()
scalarOpacity.AddPoint(volpropertynode.GetVolumeProperty().GetScalarOpacity().GetRange()[0], 0)#low bound of scalar range
scalarOpacity.AddPoint(210, 0)#set threshold
scalarOpacity.AddPoint(210, 0.02)#set threshold
scalarOpacity.AddPoint(volpropertynode.GetVolumeProperty().GetScalarOpacity().GetRange()[1], 0.02)#upper bound of scalar range
scalarOpacity.AddPoint(volpropertynode.GetVolumeProperty().GetScalarOpacity().GetRange()[1], 0)#upper bound of scalar range
scalarOpacity.AddPoint(volpropertynode.GetVolumeProperty().GetScalarOpacity().GetRange()[1], 0)#upper bound of scalar range
#print(scalarOpacity)
displayNode.GetVolumePropertyNode().GetVolumeProperty().SetScalarOpacity(scalarOpacity)
#center view
reset_views()
#Rotate the 3D View¶
layoutManager = slicer.app.layoutManager()
threeDWidget = layoutManager.threeDWidget(0)
threeDView = threeDWidget.threeDView()
threeDView.yaw()
axisIndex = [0,#viewfromleft
1,#viewfromrightside
2,#back
3,#front
4,#from above
5]
#various possible views of the 3d image
cap = ScreenCapture.ScreenCaptureLogic()
for screenshotIndex in range(len(axisIndex)):
threeDView.rotateToViewAxis(axisIndex[screenshotIndex])#loop through views
#save screenshots to disk
cap = ScreenCapture.ScreenCaptureLogic()
threeDView.rotateToViewAxis(3) #rotate to Anterior view
outputFilename = subfolder+'\\'+i+'_anterior.png'
outputFilename = rf'{outputFilename}'
cap.captureImageFromView(threeDView, outputFilename)
print("Screenshots saved to disk." + outputFilename)
threeDView.rotateToViewAxis(1) #rotate to lateral view
outputFilename = subfolder+'\\'+i+'_lateral.png'
outputFilename = rf'{outputFilename}'
cap.captureImageFromView(threeDView, outputFilename)
print("Screenshots saved to disk." + outputFilename)
#clear the scene before opening the next volume (next iteration of the loop)
slicer.mrmlScene.Clear(0)
slicer.mrmlScene.Clear(0)