@@ -89,7 +89,7 @@ each notebook cell.
89
89
+++
90
90
91
91
1 . Examine an X-ray with ` imageio `
92
- 2 . Combine images with ` np.stack() ` to demonstrate progression
92
+ 2 . Combine images into a multi-dimensional array to demonstrate progression
93
93
3 . Edge detection using the Laplacian-Gaussian, Gaussian gradient, Sobel, and
94
94
Canny filters
95
95
4 . Apply masks to X-rays with ` np.where() `
@@ -140,92 +140,45 @@ plt.axis('off')
140
140
plt.show()
141
141
```
142
142
143
- ## Combine images with ` np.stack() ` to demonstrate progression
143
+ ## Combine images into a multidimensional array to demonstrate progression
144
144
145
145
+++
146
146
147
- With NumPy's ` np.stack() ` you can combine multiple X-rays to make an
148
- n-dimensional array and then show the "health progress" in a sequential manner.
149
-
150
- In the next example, instead of 1 image you'll use 8 X-ray 1024x1024-pixel
147
+ In the next example, instead of 1 image you'll use 9 X-ray 1024x1024-pixel
151
148
images from the ChestX-ray8 dataset that have been downloaded and extracted
152
149
from one of the dataset files. They are numbered from ` ...000.png ` to
153
150
` ...008.png ` and let's assume they belong to the same patient.
154
151
155
- +++
156
-
157
- ** 1.** Import NumPy, read in each of the X-rays, and stack them together with
158
- ` np.stack() ` :
152
+ ** 1.** Import NumPy, read in each of the X-rays, and create a three-dimensional
153
+ array where the first dimension corresponds to image number:
159
154
160
155
``` {code-cell} ipython3
161
156
import numpy as np
157
+ num_imgs = 9
162
158
163
- file1 = imageio.imread(os.path.join(DIR, '00000011_000.png'))
164
- file2 = imageio.imread(os.path.join(DIR, '00000011_001.png'))
165
- file3 = imageio.imread(os.path.join(DIR, '00000011_003.png'))
166
- file4 = imageio.imread(os.path.join(DIR, '00000011_004.png'))
167
- file5 = imageio.imread(os.path.join(DIR, '00000011_005.png'))
168
- file6 = imageio.imread(os.path.join(DIR, '00000011_006.png'))
169
- file7 = imageio.imread(os.path.join(DIR, '00000011_007.png'))
170
- file8 = imageio.imread(os.path.join(DIR, '00000011_008.png'))
171
-
172
- combined_xray_images_1 = np.stack([file1, file2, file3, file4, file5, file6, file7, file8])
173
- ```
174
-
175
- Alternatively, you can ` append ` the image arrays as follows:
176
-
177
- ``` {code-cell} ipython3
178
- combined_xray_images_2 = []
179
-
180
- for i in range(8):
181
- single_xray_image = imageio.imread(os.path.join(DIR, '00000011_00'+str(i)+'.png'))
182
- combined_xray_images_2.append(single_xray_image)
159
+ combined_xray_images_1 = np.array(
160
+ [imageio.imread(os.path.join(DIR, f"00000011_00{i}.png")) for i in range(num_imgs)]
161
+ )
183
162
```
184
163
185
- _ Note on performance:_
186
-
187
- - ` append ` ing the images may no be faster. If you care about performance, you
188
- should probably use ` np.stack() ` , as evidenced when you try to time the code
189
- with Python's ` timeit ` :
190
-
191
- ``` python
192
- % timeit combined_xray_images_1 = np.stack([file1, file2, file3, file4, file5, file6, file7, file8])
193
- ```
194
-
195
- Example output:
196
-
197
- ```
198
- 1.52 ms ± 49.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
199
- ```
200
-
201
- ```python
202
- % timeit C = [combined_xray_images_2.append(imageio.imread(os.path.join(DIR , ' 00000011_00' + str (i)+ ' .png' ))) for i in range (8 )]
203
- ```
204
-
205
- Example output:
206
-
207
- ```
208
- 159 ms ± 2.69 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
209
- ```
210
-
211
- ++ +
212
-
213
- ** 2 .** Check the shape of the new X- ray image array containing 8 stacked images:
164
+ ** 2.** Check the shape of the new X-ray image array containing 9 stacked images:
214
165
215
166
``` {code-cell} ipython3
216
167
combined_xray_images_1.shape
217
168
```
218
169
170
+ Note that the shape in the first dimension matches ` num_imgs ` , so the
171
+ ` combined_xray_images_1 ` array can be interpreted as a stack of 2D images.
172
+
219
173
** 3.** You can now display the "health progress" by plotting each of frames next
220
174
to each other using Matplotlib:
221
175
222
176
``` {code-cell} ipython3
223
- fig, axes = plt.subplots(nrows=1, ncols=8 , figsize=(30, 30))
177
+ fig, axes = plt.subplots(nrows=1, ncols=num_imgs , figsize=(30, 30))
224
178
225
- for i in range(8):
226
- x = combined_xray_images_1[i]
227
- axes[i].imshow(x, cmap='gray')
228
- axes[i].axis('off')
179
+ for img, ax in zip(combined_xray_images_1, axes):
180
+ ax.imshow(img, cmap='gray')
181
+ ax.axis('off')
229
182
```
230
183
231
184
** 4.** In addition, it can be helpful to show the progress as an animation.
0 commit comments