-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimshow3D.py
50 lines (41 loc) · 1.88 KB
/
imshow3D.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
import numpy as np
import ipywidgets as ipyw
import matplotlib.pyplot as plt
class imshow3D:
"""
imshow3D 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('plasma'), 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, zaxis=2, figsize=(8,8), cmap='plasma'):
self.volume = volume
self.figsize = figsize
self.cmap = cmap
self.view = ['y-z', 'z-x', 'x-y'][zaxis]
self.v = [np.min(volume), np.max(volume)]
# Call to select slice plane
ipyw.interact(self.view_selection, view=ipyw.RadioButtons(
options=['x-y','y-z', 'z-x'], value=self.view,
description='Slice plane selection:', disabled=False,
style={'description_width': 'initial'}))
def view_selection(self, view):
# Transpose the volume to orient according to the slice plane selection
orient = {"y-z":[1,2,0], "z-x":[2,0,1], "x-y": [0,1,2]}
self.vol = np.transpose(self.volume, orient[view])
maxZ = self.vol.shape[2] - 1
# Call to view a slice within the selected slice plane
ipyw.interact(self.plot_slice,
z=ipyw.IntSlider(min=0, max=maxZ, step=1, continuous_update=False,
description='Image Slice:'))
def plot_slice(self, z):
# Plot slice for the given plane and slice
self.fig = plt.figure(figsize=self.figsize)
plt.imshow(self.vol[:,:,z], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1])