@@ -4,15 +4,17 @@ jupytext:
4
4
extension : .md
5
5
format_name : myst
6
6
format_version : 0.13
7
- jupytext_version : 1.10.3
7
+ jupytext_version : 1.11.2
8
8
kernelspec :
9
9
display_name : Python 3
10
10
language : python
11
11
name : python3
12
12
---
13
13
14
- ``` {code-cell} python
15
- :tags: [remove-input, remove-output]
14
+ ``` {code-cell}
15
+ ---
16
+ tags: [remove-input, remove-output]
17
+ ---
16
18
17
19
%matplotlib inline
18
20
%config InlineBackend.figure_format = 'retina'
@@ -26,7 +28,7 @@ Images are represented in ``scikit-image`` using standard ``numpy`` arrays. Thi
26
28
27
29
Let's see how to build a grayscale image as a 2D array:
28
30
29
- ``` {code-cell} python
31
+ ``` {code-cell}
30
32
import numpy as np
31
33
from matplotlib import pyplot as plt
32
34
@@ -38,7 +40,7 @@ plt.colorbar();
38
40
39
41
The same holds for "real-world" images:
40
42
41
- ``` {code-cell} python
43
+ ``` {code-cell}
42
44
from skimage import data
43
45
44
46
coins = data.coins()
@@ -52,7 +54,7 @@ plt.imshow(coins, cmap='gray');
52
54
53
55
A color image is a 3D array, where the last dimension has size 3 and represents the red, green, and blue channels:
54
56
55
- ``` {code-cell} python
57
+ ``` {code-cell}
56
58
cat = data.chelsea()
57
59
print("Shape:", cat.shape)
58
60
print("Values min/max:", cat.min(), cat.max())
@@ -62,7 +64,7 @@ plt.imshow(cat);
62
64
63
65
These are * just NumPy arrays* . E.g., we can make a red square by using standard array slicing and manipulation:
64
66
65
- ``` {code-cell} python
67
+ ``` {code-cell}
66
68
cat[10:110, 10:110, :] = [255, 0, 0] # [red, green, blue]
67
69
plt.imshow(cat);
68
70
```
@@ -84,14 +86,14 @@ Images can also include transparent regions by adding a 4th dimension, called an
84
86
85
87
## Displaying images using matplotlib
86
88
87
- ``` {code-cell} python
89
+ ``` {code-cell}
88
90
from skimage import data
89
91
90
92
img0 = data.chelsea()
91
93
img1 = data.rocket()
92
94
```
93
95
94
- ``` {code-cell} python
96
+ ``` {code-cell}
95
97
import matplotlib.pyplot as plt
96
98
97
99
f, (ax0, ax1) = plt.subplots(1, 2, figsize=(20, 10))
@@ -128,7 +130,7 @@ data-type of the array.
128
130
129
131
E.g., here, I generate two valid images:
130
132
131
- ``` {code-cell} python
133
+ ``` {code-cell}
132
134
linear0 = np.linspace(0, 1, 2500).reshape((50, 50))
133
135
linear1 = np.linspace(0, 255, 2500).reshape((50, 50)).astype(np.uint8)
134
136
@@ -148,7 +150,7 @@ as long as the range is correct (0-1 for floating point images, 0-255 for unsign
148
150
149
151
You can convert images between different representations by using `` img_as_float `` , `` img_as_ubyte `` , etc.:
150
152
151
- ``` {code-cell} python
153
+ ``` {code-cell}
152
154
from skimage import img_as_float, img_as_ubyte
153
155
154
156
image = data.chelsea()
@@ -164,7 +166,7 @@ print("231/255 =", 231/255.)
164
166
165
167
Your code would then typically look like this:
166
168
167
- ``` python
169
+ ``` {code-cell}
168
170
def my_function(any_image):
169
171
float_image = img_as_float(any_image)
170
172
# Proceed, knowing image is in [0, 1]
@@ -181,7 +183,7 @@ Mostly, we won't be using input images from the scikit-image example data sets.
181
183
182
184
scikit-image conveniently wraps many of these in the ` io ` submodule, and will use whichever of the libraries mentioned above are installed:
183
185
184
- ``` {code-cell} python
186
+ ``` {code-cell}
185
187
from skimage import io
186
188
187
189
image = io.imread('../images/balloon.jpg')
@@ -196,15 +198,15 @@ plt.imshow(image);
196
198
197
199
We also have the ability to load multiple images, or multi-layer TIFF images:
198
200
199
- ``` {code-cell} python
201
+ ``` {code-cell}
200
202
ic = io.ImageCollection('../images/*.png:../images/*.jpg')
201
203
202
204
print('Type:', type(ic))
203
205
204
206
ic.files
205
207
```
206
208
207
- ``` {code-cell} python
209
+ ``` {code-cell}
208
210
import os
209
211
210
212
f, axes = plt.subplots(nrows=3, ncols=len(ic) // 3 + 1, figsize=(20, 5))
@@ -227,11 +229,11 @@ plt.tight_layout()
227
229
228
230
` enumerate ` gives us each element in a container, along with its position.
229
231
230
- ``` {code-cell} python
232
+ ``` {code-cell}
231
233
animals = ['cat', 'dog', 'leopard']
232
234
```
233
235
234
- ``` {code-cell} python
236
+ ``` {code-cell}
235
237
for i, animal in enumerate(animals):
236
238
print('The animal in position {} is {}'.format(i, animal))
237
239
```
@@ -244,8 +246,10 @@ The arms and strut of the H should have a width of 3 pixels, and the H itself sh
244
246
245
247
Start with the following template:
246
248
247
- ``` {code-cell} python
248
- :tags: [hide-output]
249
+ ``` {code-cell}
250
+ ---
251
+ tags: [hide-output]
252
+ ---
249
253
250
254
def draw_H(image, coords, color=(0, 255, 0)):
251
255
out = image.copy()
@@ -257,8 +261,10 @@ def draw_H(image, coords, color=(0, 255, 0)):
257
261
258
262
Test your function like so:
259
263
260
- ``` {code-cell} python
261
- :tags: [remove-output]
264
+ ``` {code-cell}
265
+ ---
266
+ tags: [remove-output]
267
+ ---
262
268
263
269
cat = data.chelsea()
264
270
cat_H = draw_H(cat, (50, -50))
@@ -269,8 +275,10 @@ plt.imshow(cat_H);
269
275
270
276
Display the different color channels of the image along (each as a gray-scale image). Start with the following template:
271
277
272
- ``` {code-cell} python
273
- :tags: [raises-exception, remove-output]
278
+ ``` {code-cell}
279
+ ---
280
+ tags: [raises-exception, remove-output]
281
+ ---
274
282
275
283
# --- read in the image ---
276
284
@@ -308,20 +316,20 @@ ax_color.set_title('all channels');
308
316
309
317
Now, take a look at the following R, G, and B channels. How would their combination look? (Write some code to confirm your intuition.)
310
318
311
- ``` {code-cell} python
319
+ ``` {code-cell}
312
320
from skimage import draw
313
321
314
322
red = np.zeros((300, 300))
315
323
green = np.zeros((300, 300))
316
324
blue = np.zeros((300, 300))
317
325
318
- r, c = draw.circle (100, 100, 100)
326
+ r, c = draw.circle_perimeter (100, 100, 100, shape=red.shape )
319
327
red[r, c] = 1
320
328
321
- r, c = draw.circle (100, 200, 100)
329
+ r, c = draw.circle_perimeter (100, 200, 100, shape=green.shape )
322
330
green[r, c] = 1
323
331
324
- r, c = draw.circle (200, 150, 100)
332
+ r, c = draw.circle_perimeter (200, 150, 100, shape=blue.shape )
325
333
blue[r, c] = 1
326
334
327
335
f, axes = plt.subplots(1, 3)
@@ -344,8 +352,10 @@ Compare your results to that obtained with `skimage.color.rgb2gray`.
344
352
345
353
Change the coefficients to 1/3 (i.e., take the mean of the red, green, and blue channels, to see how that approach compares with ` rgb2gray ` ).
346
354
347
- ``` {code-cell} python
348
- :tags: [raises-exception, remove-output]
355
+ ``` {code-cell}
356
+ ---
357
+ tags: [raises-exception, remove-output]
358
+ ---
349
359
350
360
from skimage import color, img_as_float
351
361
0 commit comments