-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBiomedical Image Analysis.py
244 lines (188 loc) · 4.85 KB
/
Biomedical Image Analysis.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
237
238
239
240
241
242
243
244
# Sample Walk thru.
import imageio
import matplotlib.pyplot as plt
im = imageio.imread('body-001.dcm')
type(im)
im.meta
im.meta['Modality']
im.meta.keys()
# Draw the image in grayscale
plt.imshow(im, cmap='gray')
plt.axis('off')
# Render the image
plt.show()
# Ex1
# Import ImageIO
import imageio
# Load "chest-220.dcm"
im =imageio.imread('chest-220.dcm')
# Print image attributes
print('Image type:', type(im))
print('Shape of image array:', im.shape)
# Draw the image in grayscale
plt.imshow(im, cmap='gray')
plt.axis('off')
# Render the image
plt.show()
# Ex2
# Import ImageIO
import imageio
im = imageio.imread('chest-220.dcm')
# Print the available metadata fields
print(im.meta.keys())
print(im.meta['Modality'])
# Result: CT
print(im.meta['PatientSex'])
# Result: F
print(im.meta['StudyDate'])
# Result: 20040529
# Ex3
# Import ImageIO and PyPlot
import imageio
import matplotlib.pyplot as plt
# Read in "chest-220.dcm"
im = imageio.imread('chest-220.dcm')
# Draw the image in grayscale
plt.imshow(im, cmap='gray', vmin=-200, vmax=200)
plt.axis('off')
# Render the image
plt.show()
# Guide Sample
import imageio
import numpy as np
im1 = imageio.imread('chest-000.dcm')
im2 = imageio.imread('chest-001.dcm')
im3 = imageio.imread('chest-002.dcm')
im1.shape
# Result: (512, 512)
vol = np.stack([im1, im2, im3])
vol.shape
# Result: (3, 512, 512)
import os
os.listdir('chest-data')
# Result: ['chest-000.dcm', 'chest-001.dcm', 'chest-002.dcm', ..., 'chest-049.dcm']
import imageio
vol = imageio.volread('chest-data')
vol.shape
# Result: (50, 512, 512)
# Image Shape - Number of elements along each axis
import imageio
vol = imageio.volread(
'chest-data')
# Image shape (in vowels)
n0, n1, n2 = vol.shape
n0, n1, n2
# Result: (50, 512, 512)
# Sampling rate - Physical space covered by each element
# Sampling rate (in mm)
d0, d1, d2 = vol.meta['sampling']
d0, d1, d2
# Result: (2, 0.5, 0.5)
# Field of view - Physical space covered along each axis
# Field of view( in mm)
n0 * d0, n1 * d1, n2 * d2
# Result: (100, 256, 256)
# Ex4
# Import ImageIO and NumPy
import imageio
import numpy as np
# Read in each 2D image
im1 = imageio.imread('chest-220.dcm')
im2 = imageio.imread('chest-221.dcm')
im3 = imageio.imread('chest-222.dcm')
# Stack images into a volume
vol = np.stack([im1, im2, im3])
print('Volume dimensions:', vol.shape)
# Ex5
# Import ImageIO
import imageio
# Load the "tcia-chest-ct" directory
vol = imageio.volread('tcia-chest-ct')
# Print image attributes
print('Available metadata:', vol.meta.keys())
print('Shape of image array:', vol.shape)
# Diving in the metadata
print (vol.meta['PatientWeight'])
# Result: 82.0
print (vol.meta['StudyDescription'])
# Result: PET CT with registered MR
print (vol.meta['PatientID'])
# Result: STS_007
print (vol.meta['StudyTime'])
# Result: 115208
print (vol.meta['Manufacturer'])
# Result: GE MEDICAL SYSTEMS
# EX6
print(vol.shape)
# Result: (25, 512, 512)
print(vol.meta['sampling'])
# Result: (3.270000000000001, 0.976562, 0.976562)
#Calculating Field of View in mm using Array shape & Sampling resolution
25 * 3.270000000000001, 512 * 0.976562, 512 * 0.976562
# Result: (81.75000000000003, 499.999744, 499.999744)
# Rounded off
(82, 500, 500)
#Sample Guide
# Plotting multiple images at once
# plt.subplots - creates a figure canvas with multiple AxesSubplots objects.
import imageio
vol = imageio.volread('chest-data')
fig, axes = plt.subplots(nrows=1, ncols=3)
axes[0].imshow(vol[0],cmap='gray')
axes[1].imshow(vol[10],cmap='gray')
axes[2].imshow(vol[20],cmap='gray')
for ax in axes:
ax.axis('off')
plt.show()
# Non-standard views
import imageio
vol = imageio.volread('chest-data')
views_1v2 = vol[pln, :, :]
views_1v2 = vol[pln]
view_0v2 = vol[:, row, :]
view_0v1 = vol[:, :, col]
# Modifying the aspect ratio
im = vol[:,:,100]
d0, d1, d2 = vol.meta['sampling']
d0, d1, d2
# Result: (2, 0.5, 0.5)
asp = d0 / d1
asp
# Result: 3
plt.imshow(im, cmap='gray', aspect=asp)
plt.show()
# Ex7
# Import PyPlot
import matplotlib.pyplot as plt
# Initialize figure and axes grid
fig, axes = plt.subplots(nrows=2, ncols=1)
# Draw an image on each subplot
axes[0].imshow(im1, cmap='gray')
axes[1].imshow(im2, cmap='gray')
# Remove ticks/labels and render
axes[0].axis('off')
axes[1].axis('off')
plt.show()
#Ex8
# Plot the images on a subplots array
fig, axes = plt.subplots(nrows=1, ncols=4)
# Loop through subplots and draw image
for ii in range(4):
im = vol[ii * 40, :, :]
axes[ii].imshow(im, cmap='gray')
axes[ii].axis('off')
# Render the figure
plt.show()
# Ex8
# Select frame from "vol"
im1 = vol[:, 256, :]
im2 = vol[:, :, 256]
# Compute aspect ratios
d0, d1, d2 = vol.meta['sampling']
asp1 = d0 / d2
asp2 = d0 / d1
# Plot the images on a subplots array
fig, axes = plt.subplots(nrows=2, ncols=1)
axes[0].imshow(im1, cmap='gray', aspect=3.3484817144226335)
axes[1].imshow(im2, cmap='gray', aspect=3.3484817144226335)
plt.show()