-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume_render.py
143 lines (88 loc) · 3.5 KB
/
volume_render.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
"""
Version: 1.5
Summary: render the cross section skeleton
Author: suxing liu
Author-email: [email protected]
USAGE
python3 volume_render.py -p /home/suxingliu/cross_section_scan/
arguments:
("-p", "--path", required=True, help="path to cross section files")
"""
import vtk
import glob
import os
import argparse
if __name__ == '__main__':
# construct the argument and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required = True, help = "path to cross section files")
args = vars(ap.parse_args())
# setting path to cross section image files
file_path = args["path"]
ext = "png"
#accquire image file list (stack of images)
filetype = '*.' + ext
image_file_path = file_path + filetype
#accquire image file list
files = sorted(glob.glob(image_file_path))
# Setting the file path
filePath = vtk.vtkStringArray()
# Sorting file to arrange in ascending order to get slices correctly
files.sort(key = lambda x: int(''.join(filter(str.isdigit, x))))
filePath.SetNumberOfValues(len(files))
for i in range(0,len(files),1):
filePath.SetValue(i,files[i])
#print(files[i])
#Source -Reader
#reader=vtk.vtkJPEGReader()
reader = vtk.vtkPNGReader()
reader.SetFileNames(filePath)
reader.SetDataSpacing(1,1,1)
reader.Update()
print(reader)
#Filter --> Setting the color mapper, Opacity for VolumeProperty
colorFunc = vtk.vtkColorTransferFunction()
colorFunc.AddRGBPoint(1, 1, 0.0, 0.0) # Red
# To set different colored pores
colorFunc.AddRGBPoint(2, 0.0, 1, 0.0) # Green
#colorFunc.AddRGBPoint(3, 0.0, 0, 1.0) # Black
#colorFunc.AddRGBPoint(4, 0.0, 0.0, 1) # Blue
opacity = vtk.vtkPiecewiseFunction()
# opacity.AddPoint(1, 1, 0.0, 0.0)
# opacity.AddPoint(2, 0.0, 0.0, 0.0)
# The previous two classes stored properties and we want to apply
# these properties to the volume we want to render,
# we have to store them in a class that stores volume properties.
volumeProperty = vtk.vtkVolumeProperty()
# set the color for volumes
volumeProperty.SetColor(colorFunc)
# To add black as background of Volume
volumeProperty.SetScalarOpacity(opacity)
volumeProperty.SetInterpolationTypeToLinear()
volumeProperty.SetIndependentComponents(2)
#Ray cast function know how to render the data
volumeMapper = vtk.vtkOpenGLGPUVolumeRayCastMapper()
#volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper()
#volumeMapper = vtk.vtkUnstructuredGridVolumeRayCastMapper()
volumeMapper.SetInputConnection(reader.GetOutputPort())
volumeMapper.SetBlendModeToMaximumIntensity()
# Different modes are available in vtk for Blend mode functions
#volumeMapper.SetBlendModeToAverageIntensity()
#volumeMapper.SetBlendModeToMinimumIntensity()
#volumeMapper.SetBlendModeToComposite()
#volumeMapper.SetBlendModeToAdditive()
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
ren = vtk.vtkRenderer()
ren.AddVolume(volume)
#No need to set by default it is black
ren.SetBackground(0, 0, 0)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(900, 900)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renWin)
interactor.Initialize()
renWin.Render()
interactor.Start()