/
vtkThreshold.py
195 lines (145 loc) · 4.61 KB
/
vtkThreshold.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
188
189
190
191
192
193
194
195
'''
Author: QIN Shuo
Date: 2016/01/28
This is a example to threshold a CT image
Add plane widget
Key interact to adjust the threshold
MarchingCube to visiualize 3D model
'''
import os
import vtk
# load data block
def LoadNifti( name ):
reader = vtk.vtkNIFTIImageReader()
reader.SetFileName(name);
reader.Update()
return reader.GetOutput()
def CreateSlider(renWinInteractor,plane,y):
#build a slide bar
slideBar = vtk.vtkSliderRepresentation2D()
slideBar.SetMinimumValue(3.0)
slideBar.SetMaximumValue(20.0)
slideBar.SetTitleText("sphere")
slideBar.GetSliderProperty().SetColor(1,0,0)
slideBar.GetTitleProperty().SetColor(1,0,0)
slideBar.GetLabelProperty().SetColor(1,0,0)
slideBar.GetSelectedProperty().SetColor(1,0,0)
slideBar.GetTubeProperty().SetColor(0,1,0)
slideBar.GetCapProperty().SetColor(1,1,0)
slideBar.GetPoint1Coordinate().SetCoordinateSystemToDisplay()
slideBar.GetPoint1Coordinate().SetValue(40 ,40+y)
slideBar.GetPoint2Coordinate().SetCoordinateSystemToDisplay()
slideBar.GetPoint2Coordinate().SetValue(200,40+y)
sliderWidget = vtk.vtkSliderWidget()
sliderWidget.SetInteractor(renWinInteractor)
sliderWidget.SetRepresentation(slideBar)
sliderWidget.EnabledOn()
def myCallback(obj,event):
print obj.__class__.__name__," called"
value = int (obj.GetRepresentation().GetValue())
plane.SetSliceIndex(value)
sliderWidget.AddObserver("InteractionEvent",myCallback)
win = vtk.vtkRenderWindow()
#create visulization component
def DisplayPoly(source,callback):
global win
polyMapper = vtk.vtkPolyDataMapper()
polyMapper.SetInputData(source)
actor = vtk.vtkActor()
actor.SetMapper(polyMapper)
renderer = vtk.vtkRenderer()
intact = vtk.vtkRenderWindowInteractor()
style = vtk.vtkInteractorStyleTrackballCamera()
intact.AddObserver(vtk.vtkCommand.KeyPressEvent,callback)
win.AddRenderer(renderer)
renderer.AddActor(actor)
intact.SetInteractorStyle(style)
intact.SetRenderWindow(win)
win.Render()
intact.Start()
def DisplayComponent(source):
renderer = vtk.vtkRenderer()
win = vtk.vtkRenderWindow()
win.AddRenderer(renderer)
style = vtk.vtkInteractorStyleImage()
intact = vtk.vtkRenderWindowInteractor()
intact.SetInteractorStyle(style)
intact.SetRenderWindow(win)
planeX = vtk.vtkImagePlaneWidget()
planeY = vtk.vtkImagePlaneWidget()
planeZ = vtk.vtkImagePlaneWidget()
planeX.SetInteractor(intact)
planeY.SetInteractor(intact)
planeZ.SetInteractor(intact)
planeX.SetInputData(source)
planeY.SetInputData(source)
planeZ.SetInputData(source)
planeX.SetPlaneOrientationToXAxes()
planeY.SetPlaneOrientationToYAxes()
planeZ.SetPlaneOrientationToZAxes()
planeX.On()
planeY.On()
planeZ.On()
global pos
pos = 1
def myCallback(obj,event):
global pos
#print obj.__class__.__name__," called"
key = obj.GetKeySym()
if key=="Up":
pos = pos+1
planeX.SetSliceIndex(pos)
print pos
win.Render()
else:
pass
intact.AddObserver(vtk.vtkCommand.KeyPressEvent,myCallback)
intact.Start()
# start main function
name = "E:/test/spine.nii"
if os.path.isfile(name):
img = LoadNifti(name)
else:
print name, "is not a file"
DisplayComponent(img)
#threshold imagedata
#threshold = vtk.vtkImageThreshold()
#threshold.SetInputData(img)
#lower = 500
#upper = 1000
#threshold.ThresholdBetween(lower,upper)
#threshold.ReplaceOutOn()
#threshold.SetOutValue(0)
#threshold.Update()
march = vtk.vtkMarchingCubes()
march.SetInputData(img)
march.GenerateValues(1,1200,1500)
march.Update()
value_u = 1500
value_l = 1200
def callback(obj,event):
global value_l
global value_u
global win
key = obj.GetKeySym()
if key == "Up":
value_l = value_l+50
march.GenerateValues(1,value_l,value_u)
march.Update()
print (value_l)
elif key == "Down":
value_u = value_u-50
march.GenerateValues(1,value_l,value_u)
march.Update()
print (value_u)
else:
pass
win.Render()
print "threshold"
img_dis = march.GetOutput()
# convert vtkImageData to vtkPolyData
geometryFilter = vtk.vtkImageDataGeometryFilter()
#geometryFilter.SetInputData(img_dis)
#geometryFilter.Update()
print "converted"
DisplayPoly(img_dis,callback)