@@ -55,48 +55,110 @@ but that each pixel can have a different colour from its neighbors.
55
55
Viewed from a distance,
56
56
these pixels seem to blend together to form the image we see.
57
57
58
- ## Working with Pixels
59
-
60
- As noted, in practice,
61
- real world images will typically be made up of a vast number of pixels,
62
- and each of these pixels will be one of potentially millions of colours.
63
- While we will deal with pictures of such complexity shortly,
64
- let's start our exploration with 15 pixels in a 5 X 3 matrix with 2 colours and
65
- work our way up to that complexity.
58
+ Real-world images are typically made up of a vast number of pixels,
59
+ and each of these pixels is one of potentially millions of colours.
60
+ While we will deal with pictures of such complexity in this lesson,
61
+ let's start our exploration with just 15 pixels in a 5 x 3 matrix with 2 colours,
62
+ and work our way up to that complexity.
66
63
67
64
::::::::::::::::::::::::::::::::::::::::: callout
68
65
69
66
## Matrices, arrays, images and pixels
70
67
71
- The ** matrix** is mathematical concept - numbers evenly arranged in a rectangle. This can be a two dimensional rectangle,
72
- like the shape of the screen you're looking at now. Or it could be a three dimensional equivalent, a cuboid, or have
73
- even more dimensions, but always keeping the evenly spaced arrangement of numbers. In computing, ** array** refers
74
- to a structure in the computer's memory where data is stored in evenly- spaced ** elements** . This is strongly analogous
68
+ A ** matrix** is a mathematical concept - numbers evenly arranged in a rectangle. This can be a two- dimensional rectangle,
69
+ like the shape of the screen you're looking at now. Or it could be a three- dimensional equivalent, a cuboid, or have
70
+ even more dimensions, but always keeping the evenly spaced arrangement of numbers. In computing, an ** array** refers
71
+ to a structure in the computer's memory where data is stored in evenly spaced ** elements** . This is strongly analogous
75
72
to a matrix. A NumPy array is a ** type** of variable (a simpler example of a type is an integer). For our purposes,
76
73
the distinction between matrices and arrays is not important, we don't really care how the computer arranges our data
77
74
in its memory. The important thing is that the computer stores values describing the pixels in images, as arrays. And
78
- the terms matrix and array can be used interchangeably.
75
+ the terms matrix and array will be used interchangeably.
79
76
80
77
::::::::::::::::::::::::::::::::::::::::::::::::::
81
78
82
- First, the necessary imports:
79
+ ## Loading images
80
+
81
+ As noted, images we want to analyze (process) with Python are loaded into arrays.
82
+ There are multiple ways to load images. In this lesson, we use imageio, a Python
83
+ library for reading (loading) and writing (saving) image data, and more specifically
84
+ its version 3. But, really, we could use any image loader which would return a
85
+ NumPy array.
83
86
84
87
``` python
85
- """ Python libraries for learning and performing image processing ."""
88
+ """ Python library for reading and writing images ."""
86
89
87
90
import imageio.v3 as iio
91
+ ```
92
+
93
+ The ` v3 ` module of imageio (` imageio.v3 ` ) is imported as ` iio ` (see note in
94
+ the next section).
95
+ Version 3 of imageio has the benefit of supporting nD (multidimensional) image data
96
+ natively (think of volumes, movies).
97
+
98
+ Let us load our image data from disk using
99
+ the ` imread ` function from the ` imageio.v3 ` module.
100
+
101
+ ``` python
102
+ eight = iio.imread(uri = " data/eight.tif" )
103
+ print (type (eight))
104
+ ```
105
+
106
+ ``` output
107
+ <class 'numpy.ndarray'>
108
+ ```
109
+
110
+ Note that, using the same image loader or a different one, we could also read in
111
+ remotely hosted data.
112
+
113
+ ::::::::::::::::::::::::::::::::::::::::: callout
114
+
115
+ ## Why not use ` skimage.io.imread() ` ?
116
+
117
+ The scikit-image library has its own function to read an image,
118
+ so you might be asking why we don't use it here.
119
+ Actually, ` skimage.io.imread() ` uses ` iio.imread() ` internally when loading an image into Python.
120
+ It is certainly something you may use as you see fit in your own code.
121
+ In this lesson, we use the imageio library to read or write images,
122
+ while scikit-image is dedicated to performing operations on the images.
123
+ Using imageio gives us more flexibility, especially when it comes to
124
+ handling metadata.
125
+
126
+ ::::::::::::::::::::::::::::::::::::::::::::::::::
127
+
128
+ ::::::::::::::::::::::::::::::::::::::::: callout
129
+
130
+ ## Beyond NumPy arrays
131
+
132
+ Beyond NumPy arrays, there exist other types of variables which are array-like. Notably,
133
+ [ pandas.DataFrame] ( https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html )
134
+ and [ xarray.DataArray] ( https://docs.xarray.dev/en/stable/generated/xarray.DataArray.html )
135
+ can hold labeled, tabular data.
136
+ These are not natively supported in scikit-image, the scientific toolkit we use
137
+ in this lesson for processing image data. However, data stored in these types can
138
+ be converted to ` numpy.ndarray ` with certain assumptions
139
+ (see ` pandas.DataFrame.to_numpy() ` and ` xarray.DataArray.data ` ). Particularly,
140
+ these conversions ignore the sampling coordinates (` DataFrame.index ` ,
141
+ ` DataFrame.columns ` , or ` DataArray.coords ` ), which may result in misrepresented data,
142
+ for instance, when the original data points are irregularly spaced.
143
+
144
+ ::::::::::::::::::::::::::::::::::::::::::::::::::
145
+
146
+ ## Working with pixels
147
+
148
+ First, let us add the necessary imports:
149
+
150
+ ``` python
151
+ """ Python libraries for learning and performing image processing."""
152
+
88
153
import ipympl
89
154
import matplotlib.pyplot as plt
90
155
import numpy as np
91
156
import skimage as ski
92
157
```
93
158
94
- The ` v3 ` module of imageio (` imageio.v3 ` ) is imported as ` iio ` . This module
95
- enables us to read and write images.
96
-
97
159
:::::::::::::::::::::::::::::::::::::::: callout
98
160
99
- ## Import Statements in Python
161
+ ## Import statements in Python
100
162
101
163
In Python, the ` import ` statement is used to
102
164
load additional functionality into a program.
@@ -117,7 +179,7 @@ import skimage as ski # form 4, load all of skimage into an object call
117
179
118
180
:::::::::::::::: spoiler
119
181
120
- ## Further Explanation
182
+ ## Further explanation
121
183
122
184
In the example above, form 1 loads the entire scikit-image library into the
123
185
program as an object.
@@ -162,32 +224,10 @@ more efficiently run commands later in the session.
162
224
% matplotlib widget
163
225
```
164
226
165
- With that taken care of,
166
- let's load our image data from disk using
167
- the ` imread ` function from the ` imageio.v3 ` module and display it using
227
+ With that taken care of, let us display the image we have loaded, using
168
228
the ` imshow ` function from the ` matplotlib.pyplot ` module.
169
- Imageio is a Python library for reading and writing image data.
170
- ` imageio.v3 ` is specifying that we want to use version 3 of imageio. This
171
- version has the benefit of supporting nD (multidimensional) image data
172
- natively (think of volumes, movies).
173
-
174
- ::::::::::::::::::::::::::::::::::::::::: callout
175
-
176
- ## Why not use ` skimage.io.imread() `
177
-
178
- The scikit-image library has its own function to read an image,
179
- so you might be asking why we don't use it here.
180
- Actually, ` skimage.io.imread() ` uses ` iio.imread() ` internally when loading an image into Python.
181
- It is certainly something you may use as you see fit in your own code.
182
- In this lesson, we use the imageio library to read or write (save) images,
183
- while scikit-image is dedicated to performing operations on the images.
184
- Using imageio gives us more flexibility, especially when it comes to
185
- handling metadata.
186
-
187
- ::::::::::::::::::::::::::::::::::::::::::::::::::
188
229
189
230
``` python
190
- eight = iio.imread(uri = " data/eight.tif" )
191
231
plt.imshow(eight)
192
232
```
193
233
@@ -407,7 +447,7 @@ Here we only have a single channel in the data and utilize a grayscale color map
407
447
to represent the luminance, or intensity of the data and correspondingly
408
448
this channel is referred to as the luminance channel.
409
449
410
- ## Even More Colours
450
+ ## Even more colours
411
451
412
452
This is all well and good at this scale,
413
453
but what happens when we instead have a picture of a natural landscape that
@@ -421,7 +461,7 @@ for individual contributions to a pixel to be adjusted independently.
421
461
This ability to manipulate properties of groups of pixels separately will be
422
462
key to certain techniques explored in later chapters of this lesson.
423
463
To get started let's see an example of how different dimensions of information
424
- combine to produce a set of pixels using a 4 X 4 matrix with 3 dimensions
464
+ combine to produce a set of pixels using a 4 x 4 matrix with 3 dimensions
425
465
for the colours red, green, and blue.
426
466
Rather than loading it from a file, we will generate this example using NumPy.
427
467
@@ -524,7 +564,7 @@ When all three channels are combined though,
524
564
the blue light of those squares is being diluted by the relative strength
525
565
of red and green being mixed in with them.
526
566
527
- ## 24-bit RGB Colour
567
+ ## 24-bit RGB colour
528
568
529
569
This last colour model we used,
530
570
known as the * RGB (Red, Green, Blue)* model, is the most common.
@@ -846,7 +886,7 @@ JPEG images can be viewed and manipulated easily on all computing platforms.
846
886
## Examining actual image sizes (optional, not included in timing)
847
887
848
888
Let us see the effects of image compression on image size with actual images.
849
- The following script creates a square white image 5000 X 5000 pixels,
889
+ The following script creates a square white image 5000 x 5000 pixels,
850
890
and then saves it as a BMP and as a JPEG image.
851
891
852
892
``` python
0 commit comments