-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDICOMViewer.py
230 lines (187 loc) · 8.28 KB
/
DICOMViewer.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
from os.path import dirname, join
from pprint import pprint
import numpy as np
from traits.api import HasTraits, Instance, Array, \
on_trait_change
from traitsui.api import View, Item, HGroup, Group
from tvtk.api import tvtk
from tvtk.pyface.scene import Scene
from mayavi import mlab
from mayavi.core.api import PipelineBase, Source
from mayavi.core.ui.api import SceneEditor, MayaviScene, \
MlabSceneModel
import pydicom
from pydicom.data import get_testdata_files
from pydicom.filereader import read_dicomdir
################################################################################
# Author: Gael Varoquaux <[email protected]>
# Copyright (c) 2009, Enthought, Inc.
# License: BSD Style.
# The object implementing the dialog
class VolumeSlicer(HasTraits):
# The data to plot
data = Array()
# The 4 views displayed
scene3d = Instance(MlabSceneModel, ())
scene_x = Instance(MlabSceneModel, ())
scene_y = Instance(MlabSceneModel, ())
scene_z = Instance(MlabSceneModel, ())
# The data source
data_src3d = Instance(Source)
# The image plane widgets of the 3D scene
ipw_3d_x = Instance(PipelineBase)
ipw_3d_y = Instance(PipelineBase)
ipw_3d_z = Instance(PipelineBase)
_axis_names = dict(x=0, y=1, z=2)
#---------------------------------------------------------------------------
def __init__(self, **traits):
super(VolumeSlicer, self).__init__(**traits)
# Force the creation of the image_plane_widgets:
self.ipw_3d_x
self.ipw_3d_y
self.ipw_3d_z
#---------------------------------------------------------------------------
# Default values
#---------------------------------------------------------------------------
def _data_src3d_default(self):
return mlab.pipeline.scalar_field(self.data,
figure=self.scene3d.mayavi_scene)
def make_ipw_3d(self, axis_name):
ipw = mlab.pipeline.image_plane_widget(self.data_src3d,
figure=self.scene3d.mayavi_scene,
plane_orientation='%s_axes' % axis_name)
return ipw
def _ipw_3d_x_default(self):
return self.make_ipw_3d('x')
def _ipw_3d_y_default(self):
return self.make_ipw_3d('y')
def _ipw_3d_z_default(self):
return self.make_ipw_3d('z')
#---------------------------------------------------------------------------
# Scene activation callbaks
#---------------------------------------------------------------------------
@on_trait_change('scene3d.activated')
def display_scene3d(self):
outline = mlab.pipeline.outline(self.data_src3d,
figure=self.scene3d.mayavi_scene,colormap=self.colormap,
)
self.scene3d.mlab.view(40, 50)
# Interaction properties can only be changed after the scene
# has been created, and thus the interactor exists
for ipw in (self.ipw_3d_x, self.ipw_3d_y, self.ipw_3d_z):
# Turn the interaction off
ipw.ipw.interaction = 0
self.scene3d.scene.background = (0, 0, 0)
# Keep the view always pointing up
self.scene3d.scene.interactor.interactor_style = \
tvtk.InteractorStyleTerrain()
def make_side_view(self, axis_name):
scene = getattr(self, 'scene_%s' % axis_name)
# To avoid copying the data, we take a reference to the
# raw VTK dataset, and pass it on to mlab. Mlab will create
# a Mayavi source from the VTK without copying it.
# We have to specify the figure so that the data gets
# added on the figure we are interested in.
outline = mlab.pipeline.outline(
self.data_src3d.mlab_source.dataset,
figure=scene.mayavi_scene,
)
ipw = mlab.pipeline.image_plane_widget(
outline,
plane_orientation='%s_axes' % axis_name,colormap=self.colormap)
setattr(self, 'ipw_%s' % axis_name, ipw)
# Synchronize positions between the corresponding image plane
# widgets on different views.
ipw.ipw.sync_trait('slice_position',
getattr(self, 'ipw_3d_%s'% axis_name).ipw)
# Make left-clicking create a crosshair
ipw.ipw.left_button_action = 0
# Add a callback on the image plane widget interaction to
# move the others
def move_view(obj, evt):
position = obj.GetCurrentCursorPosition()
for other_axis, axis_number in self._axis_names.items():
if other_axis == axis_name:
continue
ipw3d = getattr(self, 'ipw_3d_%s' % other_axis)
ipw3d.ipw.slice_position = position[axis_number]
ipw.ipw.add_observer('InteractionEvent', move_view)
ipw.ipw.add_observer('StartInteractionEvent', move_view)
# Center the image plane widget
ipw.ipw.slice_position = 0.5*self.data.shape[
self._axis_names[axis_name]]
# Position the view for the scene
views = dict(x=( 0, 90),
y=(90, 90),
z=( 0, 0),
)
scene.mlab.view(*views[axis_name])
# 2D interaction: only pan and zoom
scene.scene.interactor.interactor_style = \
tvtk.InteractorStyleImage()
scene.scene.background = (0, 0, 0)
@on_trait_change('scene_x.activated')
def display_scene_x(self):
return self.make_side_view('x')
@on_trait_change('scene_y.activated')
def display_scene_y(self):
return self.make_side_view('y')
@on_trait_change('scene_z.activated')
def display_scene_z(self):
return self.make_side_view('z')
#---------------------------------------------------------------------------
# The layout of the dialog created
#---------------------------------------------------------------------------
view = View(HGroup(
Group(
Item('scene_y',
editor=SceneEditor(scene_class=Scene),
height=250, width=300),
Item('scene_z',
editor=SceneEditor(scene_class=Scene),
height=250, width=300),
show_labels=False,
),
Group(
Item('scene_x',
editor=SceneEditor(scene_class=Scene),
height=250, width=300),
Item('scene3d',
editor=SceneEditor(scene_class=MayaviScene),
height=250, width=300),
show_labels=False,
),
),
resizable=True,
title='Volume Slicer',
)
################################################################################
# Create some data
x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
data = np.sin(3*x)/x + 0.05*z**2 + np.cos(3*y)
# fetch the path to the test data
filepath = 'C:\\Users\\eramirez\\Desktop\\CT\\LENS_P3\\INSP_CPAP\\DICOMDIR'
print('Path to the DICOM directory: {}'.format(filepath))
# load the data
dicom_dir = read_dicomdir(filepath)
base_dir = dirname(filepath)
patient_record = dicom_dir.patient_records[0] #select one patient
if (hasattr(patient_record, 'PatientID') and
hasattr(patient_record, 'PatientName')):
print("Patient: {}".format(patient_record.PatientID))
study = patient_record.children[0]
#print(study)
print(" " * 4 + "Study {}: {}: {}".format(study.StudyID,
study.StudyDate,
study.StudyDescription))
series = study.children[0]
image_records = series.children
image_filenames = [join(base_dir, *image_rec.ReferencedFileID)
for image_rec in image_records]
#get the pixel array
datasets = [pydicom.dcmread(image_filename).pixel_array
for image_filename in image_filenames]
#convert to numpy array
npa = np.array(datasets)
m = VolumeSlicer(data=npa,colormap='gray')
m.configure_traits()