-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModifyVolume.py
236 lines (188 loc) · 8.62 KB
/
ModifyVolume.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# A ModifyVolume for test Slicer module
import vtk
import qt
import ctk
import slicer
from slicer.ScriptedLoadableModule import *
import logging
import math
class ModifyVolume(ScriptedLoadableModule):
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
self.parent.title = 'ModifyVolume'
self.parent.categories = ['Examples']
self.parent.dependencies = []
self.parent.contributors = ["Quentan Qi (University of Hull)"]
self.parent.helpText = """
A playground for testing new script.
"""
self.parent.acknowledgementText = """
Thanks to the tutorials.
"""
class ModifyVolumeWidget(ScriptedLoadableModuleWidget):
def setup(self):
ScriptedLoadableModuleWidget.setup(self)
# Sample collasible Button
pgCoBtn = ctk.ctkCollapsibleButton()
pgCoBtn.text = "ModifyVolume Collasible Button"
self.layout.addWidget(pgCoBtn)
# Layout
pgFormLayout = qt.QFormLayout(pgCoBtn)
# Test Button
testBtn = qt.QPushButton("Test Button")
testBtn.toolTip = "Print 'Test Button Pressed.' in standard output"
pgFormLayout.addWidget(testBtn)
testBtn.connect('clicked()', self.onTestBtnClicked)
# Gradient Button
gradientBtn = qt.QPushButton("Gradient")
pgFormLayout.addWidget(gradientBtn)
gradientBtn.connect('clicked()', self.onGradientBtnClicked)
# Create New Volume
newVolBtn = qt.QPushButton("New Volume")
pgFormLayout.addWidget(newVolBtn)
newVolBtn.connect("clicked()", self.onNewVolBtnClicked)
# Gradient in new Volume
gradientInNewVolBtn = qt.QPushButton("Gradient in New Volume")
pgFormLayout.addWidget(gradientInNewVolBtn)
gradientInNewVolBtn.connect('clicked()', self.onGradientInNewVolBtnClicked)
# A Check box
testCheckBox = qt.QCheckBox("Test Check Box")
testCheckBox.toolTip = "Test how CheckBox works"
pgFormLayout.addWidget(testCheckBox)
testCheckBox.connect('toggled(bool)', self.onTestCheckBoxToggled)
# Spacer if needed
self.layout.addStretch(1)
# Set local variables as instance attributes
self.testBtn = testBtn
self.gradentBtn = gradientBtn
self.newVolBtn = newVolBtn
def onTestBtnClicked(self):
print("Test Button Pressed!\n")
# qt.QMessageBox.information(slicer.util.mainWindow(),
# "TEST Window", "For test only!")
volumeNode = slicer.util.getNode('MRHead')
ijkToRas = vtk.vtkMatrix4x4()
volumeNode.GetIJKToRASMatrix(ijkToRas)
imageData = volumeNode.GetImageData()
extent = imageData.GetExtent()
# image_np = slicer.util.array('MRHead')
for k in xrange(extent[4], extent[5]/2+1):
for j in xrange(extent[2], extent[3]/2+1):
for i in xrange(extent[0], extent[1]/2+1):
position_Ijk = [i, j, k, 1]
position_Ras = ijkToRas.MultiplyPoint(position_Ijk)
r = position_Ras[0]
a = position_Ras[1]
s = position_Ras[2]
# print "r=%f, a=%f, s=%f, Value[r, a, s]=%f" % (r, a, s, image_np[r, a, s])
# print "i=%f, j=%f, k=%f, Value[i, j, k]=%f" % (i, j, k, image_np[k, j, i])
functionValue = (r-10)*(r-10) + (a+15)*(a+15) + s*s
# functionValue = (i-10)**2 + (j+15)**2 + s**2
# functionValue = math.sqrt((r-10)**2 + (a+15)**2 + s**2)
# functionValue = r-10 + a+15 + s
# functionValue += image_np[k, j, i]
imageData.SetScalarComponentFromDouble(
i, j, k, 0, functionValue)
# imageData.SetScalarComponentFromFloat(distortionVerctorPosition_Ijk[0],
# distortionVerctorPosition_Ijk[1],
# distortionVerctorPosition_Ijk[2],
# 0, fillValue)
imageData.Modified()
def onGradientBtnClicked(self):
volumeNode = slicer.util.getNode('MRHead')
ijkToRas = vtk.vtkMatrix4x4()
volumeNode.GetIJKToRASMatrix(ijkToRas)
imageData = volumeNode.GetImageData()
extent = imageData.GetExtent()
# npData = slicer.util.array('MRHead')
impVol = vtk.vtkImplicitVolume()
impVol.SetVolume(imageData)
for k in xrange(extent[4], extent[5]/2+1):
for j in xrange(extent[2], extent[3]/2+1):
for i in xrange(extent[0], extent[1]/2+1):
g = impVol.FunctionGradient(i, j, k)
gradient = math.sqrt(g[0]**2 + g[1]**2 + g[2]**2)
imageData.SetScalarComponentFromFloat(i, j, k, 0, gradient)
imageData.Modified()
def onNewVolBtnClicked(self):
imageSize = [128]*3
imageSpacing = [1.0]*3
voxelType = vtk.VTK_UNSIGNED_CHAR
# Create an empty image volume
imageData = vtk.vtkImageData()
imageData.SetDimensions(imageSize)
imageData.AllocateScalars(voxelType, 64)
thresholder = vtk.vtkImageThreshold()
thresholder.SetInputData(imageData)
thresholder.SetInValue(0)
thresholder.SetOutValue(0)
# Create volume node
volumeNode = slicer.vtkMRMLScalarVolumeNode()
volumeNode.SetSpacing(imageSpacing)
volumeNode.SetImageDataConnection(thresholder.GetOutputPort())
# Add volume to scene
scene = slicer.mrmlScene
scene.AddNode(volumeNode)
displayNode = slicer.vtkMRMLScalarVolumeDisplayNode()
scene.AddNode(displayNode)
colorNode = slicer.util.getNode('Grey')
displayNode.SetAndObserveColorNodeID(colorNode.GetID())
volumeNode.SetAndObserveDisplayNodeID(displayNode.GetID())
volumeNode.CreateDefaultStorageNode()
# Show the new volume in the Slice view
applicationLogic = slicer.app.applicationLogic()
selectionNode = applicationLogic.GetSelectionNode()
selectionNode.SetSecondaryVolumeID(volumeNode.GetID())
applicationLogic.PropagateForegroundVolumeSelection(0)
# Center the 3D View on the scene
# It works only after showing the 3D scene
layoutManager = slicer.app.layoutManager()
threeDWidget = layoutManager.threeDWidget(0)
threeDView = threeDWidget.threeDView()
threeDView.resetFocalPoint()
def onGradientInNewVolBtnClicked(self):
# Result is not right!
volumeNode = slicer.util.getNode('MRHead')
ijkToRas = vtk.vtkMatrix4x4()
volumeNode.GetIJKToRASMatrix(ijkToRas)
imageData = volumeNode.GetImageData()
extent = imageData.GetExtent()
imageSize = imageData.GetDimensions()
imageSpacing = imageData.GetSpacing()
voxelType = vtk.VTK_FLOAT
# Create empty image volume
imageData_2 = vtk.vtkImageData()
imageData_2.SetDimensions(imageSize[0]/2, imageSize[1]/2, imageSize[2]/2)
imageData_2.SetSpacing(imageSpacing)
imageData_2.AllocateScalars(voxelType, 0)
thresholder = vtk.vtkImageThreshold()
thresholder.SetInputData(imageData_2)
thresholder.SetInValue(0)
thresholder.SetOutValue(0)
volumeNode_2 = slicer.vtkMRMLScalarVolumeNode()
volumeNode_2.SetSpacing(imageSpacing)
volumeNode_2.SetImageDataConnection(thresholder.GetOutputPort())
# Add volume to scene
scene = slicer.mrmlScene
scene.AddNode(volumeNode_2)
displayNode = slicer.vtkMRMLScalarVolumeDisplayNode()
scene.AddNode(displayNode)
colorNode = slicer.util.getNode('Grey')
displayNode.SetAndObserveColorNodeID(colorNode.GetID())
volumeNode_2.SetAndObserveDisplayNodeID(displayNode.GetID())
volumeNode_2.CreateDefaultStorageNode()
# npData = slicer.util.array('MRHead')
impVol = vtk.vtkImplicitVolume()
impVol.SetVolume(imageData)
for k in xrange(extent[4], extent[5]/2+1):
for j in xrange(extent[2], extent[3]/2+1):
for i in xrange(extent[0], extent[1]/2+1):
g = impVol.FunctionGradient(i, j, k)
gradient = math.sqrt(g[0]**2 + g[1]**2 + g[2]**2)
imageData_2.SetScalarComponentFromFloat(i, j, k, 0, gradient)
imageData_2.Modified()
def onTestCheckBoxToggled(self, state):
if state:
print "The CheckBox is toggled!"
else:
print "The CheckBox is untoggled!"