-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSliceViewer.py
160 lines (133 loc) · 6.08 KB
/
SliceViewer.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
from os.path import dirname, join
from pprint import pprint
import numpy as np
import ipywidgets as ipyw
import matplotlib.pyplot as plt
import pydicom
from pydicom.data import get_testdata_files
from pydicom.filereader import read_dicomdir
class ImageSliceViewer3D:
"""
ImageSliceViewer3D is for viewing volumetric image slices in jupyter or
ipython notebooks.
User can interactively change the slice plane selection for the image and
the slice plane being viewed.
Argumentss:
Volume = 3D input image
figsize = default(8,8), to set the size of the figure
cmap = default('gray'), string for the matplotlib colormap. You can find
more matplotlib colormaps on the following link:
https://matplotlib.org/users/colormaps.html
"""
def __init__(self, volume, figsize=(10,10), cmap='gray'):
self.volume = volume
self.figsize = figsize
self.cmap = cmap
self.v = [np.min(volume), np.max(volume)]
# Call to select slice plane
ipyw.interact(self.views)
def views(self):
self.vol1 = np.transpose(self.volume, [1,2,0])
self.vol2 = np.rot90(np.transpose(self.volume, [2,0,1]), 3) #rotate 270 degrees
self.vol3 = np.transpose(self.volume, [0,1,2])
maxZ1 = self.vol1.shape[2] - 1
maxZ2 = self.vol2.shape[2] - 1
maxZ3 = self.vol3.shape[2] - 1
ipyw.interact(self.plot_slice,
z1=ipyw.IntSlider(min=0, max=maxZ1, step=1, continuous_update=False,
description='Axial:'),
z2=ipyw.IntSlider(min=0, max=maxZ2, step=1, continuous_update=False,
description='Coronal:'),
z3=ipyw.IntSlider(min=0, max=maxZ3, step=1, continuous_update=False,
description='Sagittal:'))
def plot_slice(self, z1, z2, z3):
# Plot slice for the given plane and slice
f,ax = plt.subplots(1,3, figsize=self.figsize, sharex=False, sharey=False,dpi=100) #, gridspec_kw={'height_ratios':[1,5,5]})
#print(self.figsize)
#self.fig = plt.figure(figsize=self.figsize)
#f(figsize = self.figsize)
ax[0].imshow(self.vol1[:,:,z1], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1], aspect='equal')
ax[1].imshow(self.vol2[:,:,z2], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1], aspect='auto')
ax[2].imshow(self.vol3[:,:,z3], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1], aspect='auto')
plt.show()
class ImageSliceViewer3D_1view:
"""
ImageSliceViewer3D is for viewing volumetric image slices in jupyter or
ipython notebooks.
User can interactively change the slice plane selection for the image and
the slice plane being viewed.
Argumentss:
Volume = 3D input image
figsize = default(8,8), to set the size of the figure
cmap = default('gray'), string for the matplotlib colormap. You can find
more matplotlib colormaps on the following link:
https://matplotlib.org/users/colormaps.html
"""
def __init__(self, volume, figsize=(5,5), cmap='gray'):
self.volume = volume
self.figsize = figsize
self.cmap = cmap
self.v = [np.min(volume), np.max(volume)]
# Call to select slice plane
ipyw.interact(self.views)
def views(self):
self.vol1 = np.transpose(self.volume, [1,2,0])
maxZ1 = self.vol1.shape[2] - 1
ipyw.interact(self.plot_slice,
z1=ipyw.IntSlider(min=0, max=maxZ1, step=1, continuous_update=False,
description='Axial:'))
def plot_slice(self, z1):
# Plot slice for the given plane and slice
f,ax = plt.subplots(1,1,figsize=self.figsize, dpi=100)
ax.imshow(self.vol1[:,:,z1], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1])
plt.show()
class ImageSliceViewer3D_2views:
"""
ImageSliceViewer3D is for viewing volumetric image slices in jupyter or
ipython notebooks.
User can interactively change the slice plane selection for the image and
the slice plane being viewed.
Argumentss:
Volume = 3D input image
figsize = default(8,8), to set the size of the figure
cmap = default('gray'), string for the matplotlib colormap. You can find
more matplotlib colormaps on the following link:
https://matplotlib.org/users/colormaps.html
"""
def __init__(self, volume1,volume2, figsize=(10,10), cmap='gray'):
self.volume1 = volume1[0]
self.volume2 = volume2[0]
self.sliceloc1 = volume1[1]
self.sliceloc2 = volume2[1]
self.figsize = figsize
self.cmap = cmap
self.v = [min(np.min(self.volume1),np.min(self.volume2)), max(np.max(self.volume1),np.max(self.volume2))]
# Call to select slice plane
ipyw.interact(self.views)
def views(self):
self.vol1 = np.transpose(self.volume1, [1,2,0])
self.vol2 = np.transpose(self.volume2, [1,2,0])
maxZ1 = self.vol1.shape[2] - 1
maxZ2 = self.vol2.shape[2] - 1
ipyw.interact(self.plot_slice,
z1=ipyw.IntSlider(min=0, max=maxZ1, step=1, continuous_update=False,
description='Volume 1:'),
z2=ipyw.IntSlider(min=0, max=maxZ2, step=1, continuous_update=False,
description='Volume 2:'))
def plot_slice(self, z1, z2):
# Plot slice for the given plane and slice
f,ax = plt.subplots(1,2, figsize=self.figsize, sharex=False, sharey=False,dpi=100) #, gridspec_kw={'height_ratios':[1,5,5]})
#print(self.figsize)
#self.fig = plt.figure(figsize=self.figsize)
#f(figsize = self.figsize)
ax[0].imshow(self.vol1[:,:,z1], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1], aspect='equal')
ax[0].title.set_text('Z loc: ' + str(self.sliceloc1[z1]))
ax[1].imshow(self.vol2[:,:,z2], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1], aspect='equal')
ax[1].title.set_text('Z loc: ' + str(self.sliceloc2[z2]))
plt.show()