-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport_del_phase_images.py
187 lines (150 loc) · 5.69 KB
/
export_del_phase_images.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 10 17:28:56 2021
@author: Katarina Milicevic, School of Electrical Engineering
Belgrade, Serbia
Exporting rendered data to .jpg and .stl files
"""
import vtk
import os
from datetime import datetime
def main(fileName, main_dir):
# Creating export directory
exp_dir = os.path.join(main_dir, "export")
if not os.path.exists(exp_dir):
os.makedirs(exp_dir)
colors = vtk.vtkNamedColors()
organsMap = CreateOrgansMap()
colorLut = CreateColorLut()
# Setup render window, renderer, and interactor.
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
appendPolydata = vtk.vtkAppendPolyData()
# Use this to ensure that the organs are selected in this order.
organs = [
'heart',
'bones',
'liver and spleen',
'kidneys',
'stone',
'veins'
]
for i in range(0, len(organs)):
actor, normals = CreateOrganActor(fileName, organsMap[organs[i]][0])
actor.GetProperty().SetOpacity(organsMap[organs[i]][1])
actor.GetProperty().SetDiffuseColor(colorLut.GetTableValue(organsMap[organs[i]][0])[:3])
actor.GetProperty().SetSpecular(.5)
actor.GetProperty().SetSpecularPower(10)
renderer.AddActor(actor)
# Collect data for stl
appendPolydata.AddInputConnection(normals.GetOutputPort())
# Save to stl
stlWriter = vtk.vtkSTLWriter()
stlWriter.SetInputConnection(appendPolydata.GetOutputPort())
stlWriter.SetFileName(generate_file_name(exp_dir,'.stl'))
stlWriter.Write()
renderer.GetActiveCamera().Elevation(-90)
renderer.ResetCamera()
renderer.ResetCameraClippingRange()
renderer.SetBackground(colors.GetColor3d("white"))
renderWindow.SetSize(800, 800)
renderWindow.OffScreenRenderingOn()
renderWindow.Render()
# Save first window view to jpg
vtk_win_im = vtk.vtkWindowToImageFilter()
vtk_win_im.SetInput(renderWindow)
vtk_win_im.Update()
vtk_image = vtk_win_im.GetOutput()
writer = vtk.vtkJPEGWriter()
writer.SetInputData(vtk_image)
writer.SetFileName(generate_file_name(exp_dir,'.jpg'))
writer.Write()
def get_program_parameters():
import argparse
description = 'Kidneys with some other abdominal organs'
epilogue = '''
'''
parser = argparse.ArgumentParser(description=description, epilog=epilogue,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('filename', help='organs.mhd.')
args = parser.parse_args()
return args.filename
def CreateColorLut():
colors = vtk.vtkNamedColors()
colorLut = vtk.vtkLookupTable()
colorLut.SetNumberOfColors(7)
colorLut.SetTableRange(0, 6)
colorLut.Build()
colorLut.SetTableValue(0, 0, 0, 0, 0)
colorLut.SetTableValue(1, colors.GetColor4d("red"))
colorLut.SetTableValue(2, colors.GetColor4d("wheat"))
colorLut.SetTableValue(3, colors.GetColor4d("darkred"))
colorLut.SetTableValue(4, colors.GetColor4d("cadmium_orange"))
colorLut.SetTableValue(5, colors.GetColor4d("lightslategray"))
colorLut.SetTableValue(6, colors.GetColor4d("blue"))
return colorLut
def CreateOrgansMap():
organMap = dict()
organMap["heart"] = [1, 0.4]
organMap["bones"] = [2, 1.0]
organMap["liver and spleen"] = [3, 0.4]
organMap["kidneys"] = [4, 0.4]
organMap["stone"] = [5, 1.0]
organMap["veins"] = [6, 1.0]
return organMap
def CreateOrganActor(fileName, organ):
reader = vtk.vtkMetaImageReader()
reader.SetFileName(fileName)
reader.Update()
selectorgan = vtk.vtkImageThreshold()
selectorgan.ThresholdBetween(organ, organ)
selectorgan.SetInValue(255)
selectorgan.SetOutValue(0)
selectorgan.SetInputConnection(reader.GetOutputPort())
gaussianRadius = 1
gaussianStandardDeviation = 2.0
gaussian = vtk.vtkImageGaussianSmooth()
gaussian.SetStandardDeviations(gaussianStandardDeviation, gaussianStandardDeviation, gaussianStandardDeviation)
gaussian.SetRadiusFactors(gaussianRadius, gaussianRadius, gaussianRadius)
gaussian.SetInputConnection(selectorgan.GetOutputPort())
isoValue = 127.5
mcubes = vtk.vtkMarchingCubes()
mcubes.SetInputConnection(gaussian.GetOutputPort())
mcubes.ComputeScalarsOff()
mcubes.ComputeGradientsOff()
mcubes.ComputeNormalsOff()
mcubes.SetValue(0, isoValue)
smoothingIterations = 5
passBand = 0.001
featureAngle = 60.0
smoother = vtk.vtkWindowedSincPolyDataFilter()
smoother.SetInputConnection(mcubes.GetOutputPort())
smoother.SetNumberOfIterations(smoothingIterations)
smoother.BoundarySmoothingOff()
smoother.FeatureEdgeSmoothingOff()
smoother.SetFeatureAngle(featureAngle)
smoother.SetPassBand(passBand)
smoother.NonManifoldSmoothingOn()
smoother.NormalizeCoordinatesOn()
smoother.Update()
normals = vtk.vtkPolyDataNormals()
normals.SetInputConnection(smoother.GetOutputPort())
normals.SetFeatureAngle(featureAngle)
stripper = vtk.vtkStripper()
stripper.SetInputConnection(normals.GetOutputPort())
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(stripper.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
return actor, normals
def generate_file_name(dir_path, extension):
n = datetime.now().strftime("%Y%m%d-%I%M%S")
return dir_path + '/' + n + extension
def stl_file_name():
n = datetime.now().strftime("%Y%m%d-%I%M%S")
return '/'+n+".stl"
if __name__ == '__main__':
main()