You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- "Define the terms bit, byte, kilobyte, megabyte, etc."
9
9
- "Explain how a digital image is composed of pixels."
10
+
- "Recommend using imageio (resp. skimage) for I/O (resp. image processing) tasks."
10
11
- "Explain how images are stored in NumPy arrays."
11
12
- "Explain the left-hand coordinate system used in digital images."
12
13
- "Explain the RGB additive colour model used in digital images."
@@ -91,13 +92,14 @@ First, the necessary imports:
91
92
92
93
~~~
93
94
"""
94
-
* Python libraries for learning and performing image processing.*
95
+
* Python libraries for learning and performing image processing.
96
+
*
95
97
"""
96
98
import numpy as np
97
-
import skimage.io
98
-
import skimage.viewer
99
99
import matplotlib.pyplot as plt
100
100
import ipympl
101
+
import imageio.v3 as iio
102
+
import skimage
101
103
~~~
102
104
{: .language-python}
103
105
@@ -116,8 +118,8 @@ import ipympl
116
118
>
117
119
> ~~~
118
120
> import skimage # form 1, load whole skimage library
119
-
> import skimage.io# form 2, load skimage.io module only
120
-
> from skimage.io import imread # form 3, load only the imread function
121
+
> import skimage.draw # form 2, load skimage.draw module only
122
+
> from skimage.draw import disk # form 3, load only the disk function
121
123
> import numpy as np # form 4, load all of numpy into an object called np
122
124
> ~~~
123
125
> {: .language-python }
@@ -127,30 +129,32 @@ import ipympl
127
129
> > In the example above, form 1 loads the entire `skimage` library into the
128
130
> > program as an object.
129
131
> > Individual modules of the library are then available within that object,
130
-
> > e.g. to access the `imread` function used in the example above,
131
-
> > you would write `skimage.io.imread()`.
132
+
> > e.g., to access the `disk` function used in [the drawing episode]({{ page.root }}{% link _episodes/04-drawing.md %}),
133
+
> > you would write `skimage.draw.disk()`.
132
134
> >
133
-
> > Form 2 loads only the `io` module of `skimage` into the program.
135
+
> > Form 2 loads only the `draw` module of `skimage` into the program.
134
136
> > When we run the code,
135
137
> > the program will take less time and use less memory
136
138
> > because we will not load the whole `skimage` library.
137
139
> > The syntax needed to use the module remains unchanged:
138
-
> > to access the `imread` function,
140
+
> > to access the `disk` function,
139
141
> > we would use the same function call as given for form 1.
140
142
> >
141
143
> > To further reduce the time and memory requirements for your program,
142
144
> > form 3 can be used to import only a specific function/class from a library/module.
143
145
> > Unlike the other forms, when this approach is used,
144
146
> > the imported function or class can be called by its name only,
145
-
> > without prefacing it with the name of the module/library from which it was loaded,
146
-
> > i.e., `imread()` instead of `skimage.io.imread()` using the example above.
147
+
> > without prefixing it with the name of the module/library from which it was loaded,
148
+
> > i.e., `disk()` instead of `skimage.draw.disk()` using the example above.
147
149
> > One hazard of this form is that importing like this will overwrite any
148
150
> > object with the same name that was defined/imported earlier in the program,
149
-
> > i.e., the example above would replace any existing object called `imread`
150
-
> > with the `imread` function from `skimage.io`.
151
+
> > i.e., the example above would replace any existing object called `disk`
152
+
> > with the `disk` function from `skimage.draw`.
151
153
> >
152
154
> > Finally, the `as` keyword can be used when importing,
153
155
> > to define a name to be used as shorthand for the library/module being imported.
156
+
> > This name is referred to as an alias. Typically, using an alias (such as
157
+
> > `np` for the NumPy library) saves us a little typing.
154
158
> > You may see `as` combined with any of the other first three forms of `import` statement.
155
159
> >
156
160
> > Which form is used often depends on
@@ -171,11 +175,28 @@ more efficiently run commands later in the session.
171
175
172
176
With that taken care of,
173
177
let's load our image data from disk using
174
-
the `imread` function from the `skimage.io` library and display it using
175
-
the `imshow` function from the `matplotlib` library.
178
+
the `imread` function from the `imageio.v3` module and display it using
179
+
the `imshow` function from the `matplotlib.pyplot` module.
180
+
`imageio` is a Python library for reading and writing image data.
181
+
`imageio.v3` is specifying that we want to use version 3 of `imageio`. This
182
+
version has the benefit of supporting nD (multidimensional) image data
183
+
natively (think of volumes, movies).
184
+
185
+
> ## Why not use `skimage.io.imread()`
186
+
>
187
+
> The `skimage` library has its own function to read an image,
188
+
> so you might be asking why we don't use it here.
189
+
> Actually, `skimage.io.imread()` uses `iio.imread()` internally when loading an image into Python.
190
+
> It is certainly something you may use as you see fit in your own code.
191
+
> In this lesson, we use the `imageio` library to read or write (save) images,
192
+
> while `skimage` is dedicated to performing operations on the images.
193
+
> Using `imageio` gives us more flexibility, especially when it comes to
194
+
> handling metadata.
195
+
>
196
+
{: .callout}
176
197
177
198
~~~
178
-
image = skimage.io.imread(fname="data/eight.tif")
199
+
image = iio.imread(uri="data/eight.tif")
179
200
plt.imshow(image)
180
201
~~~
181
202
{: .language-python}
@@ -198,7 +219,7 @@ the bulk of a picture file is just arrays of numeric information that,
198
219
when interpreted according to a certain rule set,
199
220
become recognizable as an image to us.
200
221
Our image of an eight is no exception,
201
-
and `skimage.io` stored that image data in an array of arrays making
222
+
and `imageio.v3` stored that image data in an array of arrays making
202
223
a 5 x 3 matrix of 15 pixels.
203
224
We can demonstrate that by calling on the shape property of our image variable
204
225
and see the matrix by printing our image variable to the screen.
@@ -237,7 +258,7 @@ column labeled 1.
237
258
Using array slicing, we can then address and assign a new value to that position.
238
259
239
260
~~~
240
-
zero = skimage.io.imread(fname="data/eight.tif")
261
+
zero = iio.imread(uri="data/eight.tif")
241
262
zero[2,1]= 1.0
242
263
"""
243
264
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
@@ -307,7 +328,7 @@ print(zero)
307
328
> > There are many possible solutions, but one method would be . . .
308
329
> >
309
330
> > ~~~
310
-
> > five = skimage.io.imread(fname="data/eight.tif")
331
+
> > five = iio.imread(uri="data/eight.tif")
311
332
> > five[1,2]= 1.0
312
333
> > five[3,0]= 1.0
313
334
> > fig, ax = plt.subplots()
@@ -338,13 +359,14 @@ One common way is to use the numbers between 0 and 255 to allow for
0 commit comments