-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmyshow.py
97 lines (70 loc) · 2.8 KB
/
myshow.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
"""
myshow.py
=========
Functions created in guides for visualization in other guides.
If you want to run the code from other guides, please download this file (by
clicking ``Download Python source code`` at the bottom of the page) and add it
to your python path.
"""
import matplotlib.pyplot as plt
import SimpleITK as sitk
def myshow(img, title=None, margin=0.05, dpi=80):
nda = sitk.GetArrayFromImage(img)
spacing = img.GetSpacing()
if nda.ndim == 3:
# fastest dim, either component or x
c = nda.shape[-1]
# the the number of components is 3 or 4 consider it an RGB image
if c not in (3, 4):
nda = nda[nda.shape[0] // 2, :, :]
elif nda.ndim == 4:
c = nda.shape[-1]
if c not in (3, 4):
raise RuntimeError("Unable to show 3D-vector Image")
# take a z-slice
nda = nda[nda.shape[0] // 2, :, :, :]
xsize = nda.shape[1]
ysize = nda.shape[0]
# Make a figure big enough to accommodate an axis of xpixels by ypixels
# as well as the ticklabels, etc...
figsize = (1 + margin) * xsize / dpi, (1 + margin) * ysize / dpi
plt.figure(figsize=figsize, dpi=dpi, tight_layout=True)
ax = plt.gca()
extent = (0, xsize * spacing[0], ysize * spacing[1], 0)
t = ax.imshow(nda, extent=extent, interpolation=None)
if nda.ndim == 2:
t.set_cmap("gray")
if(title):
plt.title(title)
plt.show()
def myshow3d(img, xslices=[], yslices=[], zslices=[], title=None, margin=0.05,
dpi=80):
img_xslices = [img[s, :, :] for s in xslices]
img_yslices = [img[:, s, :] for s in yslices]
img_zslices = [img[:, :, s] for s in zslices]
maxlen = max(len(img_xslices), len(img_yslices), len(img_zslices))
img_null = sitk.Image([0, 0], img.GetPixelID(),
img.GetNumberOfComponentsPerPixel())
img_slices = []
d = 0
if len(img_xslices):
img_slices += img_xslices + [img_null] * (maxlen - len(img_xslices))
d += 1
if len(img_yslices):
img_slices += img_yslices + [img_null] * (maxlen - len(img_yslices))
d += 1
if len(img_zslices):
img_slices += img_zslices + [img_null] * (maxlen - len(img_zslices))
d += 1
if maxlen != 0:
if img.GetNumberOfComponentsPerPixel() == 1:
img = sitk.Tile(img_slices, [maxlen, d])
# TO DO check in code to get Tile Filter working with vector images
else:
img_comps = []
for i in range(0, img.GetNumberOfComponentsPerPixel()):
img_slices_c = [sitk.VectorIndexSelectionCast(s, i)
for s in img_slices]
img_comps.append(sitk.Tile(img_slices_c, [maxlen, d]))
img = sitk.Compose(img_comps)
myshow(img, title, margin, dpi)