Skip to content

Commit d50f534

Browse files
authored
Merge pull request numpy#102 from rossbar/update-xray-multi-img-ary
Update multi-dimentional array example in xray tutorial
2 parents 902b05a + 8681961 commit d50f534

File tree

1 file changed

+17
-64
lines changed

1 file changed

+17
-64
lines changed

content/tutorial-x-ray-image-processing.md

Lines changed: 17 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ each notebook cell.
8989
+++
9090

9191
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
9393
3. Edge detection using the Laplacian-Gaussian, Gaussian gradient, Sobel, and
9494
Canny filters
9595
4. Apply masks to X-rays with `np.where()`
@@ -140,92 +140,45 @@ plt.axis('off')
140140
plt.show()
141141
```
142142

143-
## Combine images with `np.stack()` to demonstrate progression
143+
## Combine images into a multidimensional array to demonstrate progression
144144

145145
+++
146146

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
151148
images from the ChestX-ray8 dataset that have been downloaded and extracted
152149
from one of the dataset files. They are numbered from `...000.png` to
153150
`...008.png` and let's assume they belong to the same patient.
154151

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:
159154

160155
```{code-cell} ipython3
161156
import numpy as np
157+
num_imgs = 9
162158
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+
)
183162
```
184163

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:
214165

215166
```{code-cell} ipython3
216167
combined_xray_images_1.shape
217168
```
218169

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+
219173
**3.** You can now display the "health progress" by plotting each of frames next
220174
to each other using Matplotlib:
221175

222176
```{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))
224178
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')
229182
```
230183

231184
**4.** In addition, it can be helpful to show the progress as an animation.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy