Skip to content

Commit b80f6b1

Browse files
committed
markdown source builds
Auto-generated via {sandpaper} Source : 0c29d28 Branch : main Author : Toby Hodges <tbyhdgs@gmail.com> Time : 2023-09-05 08:21:04 +0000 Message : Merge pull request #299 from mkcor/new-import-ski Use new import convention for scikit-image.
1 parent 3440cba commit b80f6b1

10 files changed

+153
-161
lines changed

02-image-basics.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ First, the necessary imports:
8484
```python
8585
"""Python libraries for learning and performing image processing."""
8686

87-
import numpy as np
88-
import matplotlib.pyplot as plt
89-
import ipympl
9087
import imageio.v3 as iio
91-
import skimage
88+
import ipympl
89+
import matplotlib.pyplot as plt
90+
import numpy as np
91+
import skimage as ski
9292
```
9393

9494
The `v3` module of imageio (`imageio.v3`) is imported as `iio`. This module
@@ -112,7 +112,7 @@ You will encounter several different forms of `import` statement.
112112
import skimage # form 1, load whole skimage library
113113
import skimage.draw # form 2, load skimage.draw module only
114114
from skimage.draw import disk # form 3, load only the disk function
115-
import numpy as np # form 4, load all of numpy into an object called np
115+
import skimage as ski # form 4, load all of skimage into an object called ski
116116
```
117117

118118
:::::::::::::: solution
@@ -126,18 +126,14 @@ e.g., to access the `disk` function used in [the drawing episode](04-drawing.md)
126126
you would write `skimage.draw.disk()`.
127127

128128
Form 2 loads only the `draw` module of `skimage` into the program.
129-
When we run the code,
130-
the program will take less time and use less memory
131-
because we will not load the whole scikit-image library.
132129
The syntax needed to use the module remains unchanged:
133130
to access the `disk` function,
134131
we would use the same function call as given for form 1.
135132

136-
To further reduce the time and memory requirements for your program,
137-
form 3 can be used to import only a specific function/class from a library/module.
133+
Form 3 can be used to import only a specific function/class from a library/module.
138134
Unlike the other forms, when this approach is used,
139135
the imported function or class can be called by its name only,
140-
without prefixing it with the name of the module/library from which it was loaded,
136+
without prefixing it with the name of the library/module from which it was loaded,
141137
i.e., `disk()` instead of `skimage.draw.disk()` using the example above.
142138
One hazard of this form is that importing like this will overwrite any
143139
object with the same name that was defined/imported earlier in the program,
@@ -148,7 +144,7 @@ Finally, the `as` keyword can be used when importing,
148144
to define a name to be used as shorthand for the library/module being imported.
149145
This name is referred to as an alias. Typically, using an alias (such as
150146
`np` for the NumPy library) saves us a little typing.
151-
You may see `as` combined with any of the other first three forms of `import` statement.
147+
You may see `as` combined with any of the other first three forms of `import` statements.
152148

153149
Which form is used often depends on
154150
the size and number of additional tools being loaded into the program.

03-skimage-images.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ for accessing and changing digital images.
2626
## First, import the packages needed for this episode
2727

2828
```python
29-
import numpy as np
30-
import matplotlib.pyplot as plt
31-
import ipympl
3229
import imageio.v3 as iio
33-
import skimage
34-
import skimage.color
35-
import skimage.transform
36-
import skimage.util
30+
import ipympl
31+
import matplotlib.pyplot as plt
32+
import numpy as np
33+
import skimage as ski
3734
```
3835

3936
## Reading, displaying, and saving images
@@ -146,19 +143,18 @@ functions we will cover in this workshop.
146143

147144
## Resizing an image (10 min)
148145

149-
Add `import skimage.transform` and `import skimage.util` to your list of imports.
150146
Using the `chair.jpg` image located in the data folder,
151147
write a Python script to read your image into a variable named `chair`.
152148
Then, resize the image to 10 percent of its current size using these lines of code:
153149

154150
```python
155151
new_shape = (chair.shape[0] // 10, chair.shape[1] // 10, chair.shape[2])
156-
resized_chair = skimage.transform.resize(image=chair, output_shape=new_shape)
157-
resized_chair = skimage.util.img_as_ubyte(resized_chair)
152+
resized_chair = ski.transform.resize(image=chair, output_shape=new_shape)
153+
resized_chair = ski.util.img_as_ubyte(resized_chair)
158154
```
159155

160156
As it is used here,
161-
the parameters to the `skimage.transform.resize()` function are
157+
the parameters to the `ski.transform.resize()` function are
162158
the image to transform, `chair`,
163159
the dimensions we want the new image to have, `new_shape`.
164160

@@ -168,7 +164,7 @@ Note that the pixel values in the new image are an approximation of
168164
the original values and should not be confused with actual, observed
169165
data. This is because scikit-image interpolates the pixel values when
170166
reducing or increasing the size of an
171-
image. `skimage.transform.resize` has a number of optional
167+
image. `ski.transform.resize` has a number of optional
172168
parameters that allow the user to control this interpolation. You
173169
can find more details in the [scikit-image
174170
documentation](https://scikit-image.org/docs/stable/api/skimage.transform.html#skimage.transform.resize).
@@ -178,7 +174,7 @@ documentation](https://scikit-image.org/docs/stable/api/skimage.transform.html#s
178174
Image files on disk are normally stored as whole numbers for space efficiency,
179175
but transformations and other math operations often result in
180176
conversion to floating point numbers.
181-
Using the `skimage.util.img_as_ubyte()` method converts it back to whole numbers
177+
Using the `ski.util.img_as_ubyte()` method converts it back to whole numbers
182178
before we save it back to disk.
183179
If we don't convert it before saving,
184180
`iio.imwrite()` may not recognise it as image data.
@@ -210,8 +206,8 @@ chair = iio.imread(uri="data/chair.jpg")
210206

211207
# resize the image
212208
new_shape = (chair.shape[0] // 10, chair.shape[1] // 10, chair.shape[2])
213-
resized_chair = skimage.transform.resize(image=chair, output_shape=new_shape)
214-
resized_chair = skimage.util.img_as_ubyte(resized_chair)
209+
resized_chair = ski.transform.resize(image=chair, output_shape=new_shape)
210+
resized_chair = ski.util.img_as_ubyte(resized_chair)
215211

216212
# write out image
217213
iio.imwrite(uri="data/resized_chair.jpg", image=resized_chair)
@@ -301,12 +297,12 @@ the result is an image in which the extraneous background detail has been remove
301297

302298
It is often easier to work with grayscale images, which have a single channel,
303299
instead of colour images, which have three channels.
304-
scikit-image offers the function `skimage.color.rgb2gray()` to achieve this.
300+
scikit-image offers the function `ski.color.rgb2gray()` to achieve this.
305301
This function adds up the three colour channels in a way that matches
306302
human colour perception,
307303
see [the scikit-image documentation for details](https://scikit-image.org/docs/dev/api/skimage.color.html#skimage.color.rgb2gray).
308304
It returns a grayscale image with floating point values in the range from 0 to 1.
309-
We can use the function `skimage.util.img_as_ubyte()` in order to convert it back to the
305+
We can use the function `ski.util.img_as_ubyte()` in order to convert it back to the
310306
original data type and the data range back 0 to 255.
311307
Note that it is often better to use image values represented by floating point values,
312308
because using floating point numbers is numerically more stable.
@@ -320,7 +316,7 @@ which is why we use "colour" in the explanatory text of this lesson.
320316
However, scikit-image contains many modules and functions that include
321317
the US English spelling, `color`.
322318
The exact spelling matters here,
323-
e.g. you will encounter an error if you try to run `skimage.colour.rgb2gray()`.
319+
e.g. you will encounter an error if you try to run `ski.colour.rgb2gray()`.
324320
To account for this, we will use the US English spelling, `color`,
325321
in example Python code throughout the lesson.
326322
You will encounter a similar approach with "centre" and `center`.
@@ -338,7 +334,7 @@ fig, ax = plt.subplots()
338334
plt.imshow(chair)
339335

340336
# convert to grayscale and display
341-
gray_chair = skimage.color.rgb2gray(chair)
337+
gray_chair = ski.color.rgb2gray(chair)
342338
fig, ax = plt.subplots()
343339
plt.imshow(gray_chair, cmap="gray")
344340
```
@@ -576,8 +572,8 @@ iio.imwrite(uri="data/clipped_maize.jpg", image=clipped_maize)
576572

577573
- Images are read from disk with the `iio.imread()` function.
578574
- We create a window that automatically scales the displayed image with Matplotlib and calling `show()` on the global figure object.
579-
- Colour images can be transformed to grayscale using `skimage.color.rgb2gray()` or, in many cases, be read as grayscale directly by passing the argument `mode="L"` to `iio.imread()`.
580-
- We can resize images with the `skimage.transform.resize()` function.
575+
- Colour images can be transformed to grayscale using `ski.color.rgb2gray()` or, in many cases, be read as grayscale directly by passing the argument `mode="L"` to `iio.imread()`.
576+
- We can resize images with the `ski.transform.resize()` function.
581577
- NumPy array commands, such as `image[image < 128] = 0`, can be used to manipulate the pixels of an image.
582578
- Array slicing can be used to extract sub-images or modify areas of images, e.g., `clip = image[60:150, 135:480, :]`.
583579
- Metadata is not retained when images are loaded as scikit-image images.

04-drawing.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,16 @@ based on changes in colour or shape.
2727
## First, import the packages needed for this episode
2828

2929
```python
30-
import numpy as np
31-
import matplotlib.pyplot as plt
32-
import ipympl
3330
import imageio.v3 as iio
34-
import skimage
35-
import skimage.draw
31+
import ipympl
32+
import matplotlib.pyplot as plt
33+
import numpy as np
34+
import skimage as ski
35+
3636
%matplotlib widget
3737
```
3838

39-
Here, we import the `draw` submodule of `skimage` as well as packages familiar
40-
from earlier in the lesson.
39+
Here, we import the same packages as earlier in the lesson.
4140

4241
## Drawing on images
4342

@@ -113,7 +112,7 @@ Next, we draw a filled, rectangle on the mask:
113112

114113
```python
115114
# Draw filled rectangle on the mask image
116-
rr, cc = skimage.draw.rectangle(start=(357, 44), end=(740, 720))
115+
rr, cc = ski.draw.rectangle(start=(357, 44), end=(740, 720))
117116
mask[rr, cc] = False
118117

119118
# Display mask image
@@ -139,7 +138,7 @@ it is wise to check how the function is used, via
139138
or other usage examples on programming-related sites such as
140139
[Stack Overflow](https://stackoverflow.com/).
141140
Basic information about scikit-image functions can be found interactively in Python,
142-
via commands like `help(skimage)` or `help(skimage.draw.rectangle)`.
141+
via commands like `help(ski)` or `help(ski.draw.rectangle)`.
143142
Take notes in your lab notebook.
144143
And, it is always wise to run some test code to verify
145144
that the functions your program uses are behaving in the manner you intend.
@@ -170,21 +169,21 @@ such that it is easier for other people to understand your code.
170169
## Other drawing operations (15 min)
171170

172171
There are other functions for drawing on images,
173-
in addition to the `skimage.draw.rectangle()` function.
172+
in addition to the `ski.draw.rectangle()` function.
174173
We can draw circles, lines, text, and other shapes as well.
175174
These drawing functions may be useful later on, to help annotate images
176175
that our programs produce.
177176
Practice some of these functions here.
178177

179-
Circles can be drawn with the `skimage.draw.disk()` function,
178+
Circles can be drawn with the `ski.draw.disk()` function,
180179
which takes two parameters:
181180
the (ry, cx) point of the centre of the circle,
182181
and the radius of the circle.
183182
There is an optional `shape` parameter that can be supplied to this function.
184183
It will limit the output coordinates for cases where the circle
185184
dimensions exceed the ones of the image.
186185

187-
Lines can be drawn with the `skimage.draw.line()` function,
186+
Lines can be drawn with the `ski.draw.line()` function,
188187
which takes four parameters:
189188
the (ry, cx) coordinate of one end of the line,
190189
and the (ry, cx) coordinate of the other end of the line.
@@ -215,15 +214,15 @@ Drawing a circle:
215214

216215
```python
217216
# Draw a blue circle with centre (200, 300) in (ry, cx) coordinates, and radius 100
218-
rr, cc = skimage.draw.disk(center=(200, 300), radius=100, shape=canvas.shape[0:2])
217+
rr, cc = ski.draw.disk(center=(200, 300), radius=100, shape=canvas.shape[0:2])
219218
canvas[rr, cc] = (0, 0, 255)
220219
```
221220

222221
Drawing a line:
223222

224223
```python
225224
# Draw a green line from (400, 200) to (500, 700) in (ry, cx) coordinates
226-
rr, cc = skimage.draw.line(r0=400, c0=200, r1=500, c1=700)
225+
rr, cc = ski.draw.line(r0=400, c0=200, r1=500, c1=700)
227226
canvas[rr, cc] = (0, 255, 0)
228227
```
229228

@@ -249,7 +248,7 @@ canvas = np.zeros(shape=(600, 800, 3), dtype="uint8")
249248

250249
# draw a blue circle at a random location 15 times
251250
for i in range(15):
252-
rr, cc = skimage.draw.disk(center=(
251+
rr, cc = ski.draw.disk(center=(
253252
random.randrange(600),
254253
random.randrange(800)),
255254
radius=50,
@@ -278,7 +277,7 @@ for i in range(15):
278277
x = random.random()
279278
if x < 0.33:
280279
# draw a blue circle at a random location
281-
rr, cc = skimage.draw.disk(center=(
280+
rr, cc = ski.draw.disk(center=(
282281
random.randrange(600),
283282
random.randrange(800)),
284283
radius=50,
@@ -287,7 +286,7 @@ for i in range(15):
287286
color = (0, 0, 255)
288287
elif x < 0.66:
289288
# draw a green line at a random location
290-
rr, cc = skimage.draw.line(
289+
rr, cc = ski.draw.line(
291290
r0=random.randrange(600),
292291
c0=random.randrange(800),
293292
r1=random.randrange(600),
@@ -296,7 +295,7 @@ for i in range(15):
296295
color = (0, 255, 0)
297296
else:
298297
# draw a red rectangle at a random location
299-
rr, cc = skimage.draw.rectangle(
298+
rr, cc = ski.draw.rectangle(
300299
start=(random.randrange(600), random.randrange(800)),
301300
extent=(50, 50),
302301
shape=canvas.shape[0:2],
@@ -359,7 +358,7 @@ maize_seedlings = iio.imread(uri="data/maize-seedlings.tif")
359358
mask = np.ones(shape=maize_seedlings.shape[0:2], dtype="bool")
360359

361360
# Draw a filled rectangle on the mask image
362-
rr, cc = skimage.draw.rectangle(start=(357, 44), end=(740, 720))
361+
rr, cc = ski.draw.rectangle(start=(357, 44), end=(740, 720))
363362
mask[rr, cc] = False
364363
```
365364

@@ -416,7 +415,7 @@ remote = np.array(remote)
416415
mask = np.ones(shape=remote.shape[0:2], dtype="bool")
417416

418417
# Draw a filled rectangle on the mask image
419-
rr, cc = skimage.draw.rectangle(start=(93, 1107), end=(1821, 1668))
418+
rr, cc = ski.draw.rectangle(start=(93, 1107), end=(1821, 1668))
420419
mask[rr, cc] = False
421420

422421
# Apply the mask
@@ -484,7 +483,7 @@ with open("data/centers.txt", "r") as center_file:
484483
ry = int(coordinates[1])
485484

486485
# ... and drawing a circle on the mask
487-
rr, cc = skimage.draw.disk(center=(ry, cx), radius=16, shape=wellplate.shape[0:2])
486+
rr, cc = ski.draw.disk(center=(ry, cx), radius=16, shape=wellplate.shape[0:2])
488487
mask[rr, cc] = False
489488

490489
# apply the mask
@@ -554,7 +553,7 @@ for row in range(12):
554553
for col in range(8):
555554

556555
# ... and drawing a circle on the mask
557-
rr, cc = skimage.draw.disk(center=(ry, cx), radius=16, shape=wellplate.shape[0:2])
556+
rr, cc = ski.draw.disk(center=(ry, cx), radius=16, shape=wellplate.shape[0:2])
558557
mask[rr, cc] = False
559558
cx += deltaCX
560559
# after one complete row, move to next row
@@ -575,7 +574,7 @@ plt.imshow(wellplate)
575574
:::::::::::::::::::::::::::::::::::::::: keypoints
576575

577576
- We can use the NumPy `zeros()` function to create a blank, black image.
578-
- We can draw on scikit-image images with functions such as `skimage.draw.rectangle()`, `skimage.draw.disk()`, `skimage.draw.line()`, and more.
577+
- We can draw on scikit-image images with functions such as `ski.draw.rectangle()`, `ski.draw.disk()`, `ski.draw.line()`, and more.
579578
- The drawing functions return indices to pixels that can be set directly.
580579

581580
::::::::::::::::::::::::::::::::::::::::::::::::::

05-creating-histograms.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ display histograms for images.
2525
## First, import the packages needed for this episode
2626

2727
```python
28-
import numpy as np
29-
import matplotlib.pyplot as plt
30-
import ipympl
3128
import imageio.v3 as iio
32-
import skimage
33-
import skimage.draw
29+
import ipympl
30+
import matplotlib.pyplot as plt
31+
import numpy as np
32+
import skimage as ski
33+
3434
%matplotlib widget
3535
```
3636

@@ -61,7 +61,7 @@ Here we load the image in grayscale instead of full colour, and display it:
6161
plant_seedling = iio.imread(uri="data/plant-seedling.jpg", mode="L")
6262

6363
# convert the image to float dtype with a value range from 0 to 1
64-
plant_seedling = skimage.util.img_as_float(plant_seedling)
64+
plant_seedling = ski.util.img_as_float(plant_seedling)
6565

6666
# display the image
6767
fig, ax = plt.subplots()
@@ -73,7 +73,7 @@ plt.imshow(plant_seedling, cmap="gray")
7373
Again, we use the `iio.imread()` function to load our image.
7474
Then, we convert the grayscale image of integer dtype, with 0-255 range, into
7575
a floating-point one with 0-1 range, by calling the function
76-
`skimage.util.img_as_float`.
76+
`ski.util.img_as_float`.
7777
We will keep working with images in the value range 0 to 1 in this lesson.
7878

7979
We now use the function `np.histogram` to compute the histogram of our image
@@ -205,9 +205,9 @@ plant_seedling = iio.imread(uri="data/plant-seedling.jpg", mode="L")
205205
fig, ax = plt.subplots()
206206
plt.imshow(plant_seedling, cmap="gray")
207207

208-
# create mask here, using np.zeros() and skimage.draw.rectangle()
208+
# create mask here, using np.zeros() and ski.draw.rectangle()
209209
mask = np.zeros(shape=plant_seedling.shape, dtype="bool")
210-
rr, cc = skimage.draw.rectangle(start=(199, 410), end=(384, 485))
210+
rr, cc = ski.draw.rectangle(start=(199, 410), end=(384, 485))
211211
mask[rr, cc] = True
212212

213213
# display the mask
@@ -403,7 +403,7 @@ And, the program should produce a colour histogram that looks like this:
403403
```python
404404
# create a circular mask to select the 7th well in the first row
405405
mask = np.zeros(shape=wellplate.shape[0:2], dtype="bool")
406-
circle = skimage.draw.disk(center=(240, 1053), radius=49, shape=wellplate.shape[0:2])
406+
circle = ski.draw.disk(center=(240, 1053), radius=49, shape=wellplate.shape[0:2])
407407
mask[circle] = 1
408408

409409
# just for display:

0 commit comments

Comments
 (0)