Skip to content

Commit 9cc0929

Browse files
committed
markdown source builds
Auto-generated via {sandpaper} Source : 49112f9 Branch : main Author : Toby Hodges <tobyhodges@carpentries.org> Time : 2023-12-11 10:01:03 +0000 Message : Merge pull request #311 from mkcor/add-loading-section Create section about image loading.
1 parent 7197e9e commit 9cc0929

File tree

2 files changed

+88
-48
lines changed

2 files changed

+88
-48
lines changed

02-image-basics.md

Lines changed: 87 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,48 +55,110 @@ but that each pixel can have a different colour from its neighbors.
5555
Viewed from a distance,
5656
these pixels seem to blend together to form the image we see.
5757

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.
6663

6764
::::::::::::::::::::::::::::::::::::::::: callout
6865

6966
## Matrices, arrays, images and pixels
7067

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
7572
to a matrix. A NumPy array is a **type** of variable (a simpler example of a type is an integer). For our purposes,
7673
the distinction between matrices and arrays is not important, we don't really care how the computer arranges our data
7774
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.
7976

8077
::::::::::::::::::::::::::::::::::::::::::::::::::
8178

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.
8386

8487
```python
85-
"""Python libraries for learning and performing image processing."""
88+
"""Python library for reading and writing images."""
8689

8790
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+
88153
import ipympl
89154
import matplotlib.pyplot as plt
90155
import numpy as np
91156
import skimage as ski
92157
```
93158

94-
The `v3` module of imageio (`imageio.v3`) is imported as `iio`. This module
95-
enables us to read and write images.
96-
97159
:::::::::::::::::::::::::::::::::::::::: callout
98160

99-
## Import Statements in Python
161+
## Import statements in Python
100162

101163
In Python, the `import` statement is used to
102164
load additional functionality into a program.
@@ -117,7 +179,7 @@ import skimage as ski # form 4, load all of skimage into an object call
117179

118180
:::::::::::::::: spoiler
119181

120-
## Further Explanation
182+
## Further explanation
121183

122184
In the example above, form 1 loads the entire scikit-image library into the
123185
program as an object.
@@ -162,32 +224,10 @@ more efficiently run commands later in the session.
162224
%matplotlib widget
163225
```
164226

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
168228
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-
::::::::::::::::::::::::::::::::::::::::::::::::::
188229

189230
```python
190-
eight = iio.imread(uri="data/eight.tif")
191231
plt.imshow(eight)
192232
```
193233

@@ -407,7 +447,7 @@ Here we only have a single channel in the data and utilize a grayscale color map
407447
to represent the luminance, or intensity of the data and correspondingly
408448
this channel is referred to as the luminance channel.
409449

410-
## Even More Colours
450+
## Even more colours
411451

412452
This is all well and good at this scale,
413453
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.
421461
This ability to manipulate properties of groups of pixels separately will be
422462
key to certain techniques explored in later chapters of this lesson.
423463
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
425465
for the colours red, green, and blue.
426466
Rather than loading it from a file, we will generate this example using NumPy.
427467

@@ -524,7 +564,7 @@ When all three channels are combined though,
524564
the blue light of those squares is being diluted by the relative strength
525565
of red and green being mixed in with them.
526566

527-
## 24-bit RGB Colour
567+
## 24-bit RGB colour
528568

529569
This last colour model we used,
530570
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.
846886
## Examining actual image sizes (optional, not included in timing)
847887

848888
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,
850890
and then saves it as a BMP and as a JPEG image.
851891

852892
```python

md5sum.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"config.yaml" "101b3ac4b679126bb1f437306eb1b836" "site/built/config.yaml" "2023-04-25"
55
"index.md" "6e80c662708984307918adfad711e15f" "site/built/index.md" "2023-07-26"
66
"episodes/01-introduction.md" "9755639c515fdbf752422e2e59128f63" "site/built/01-introduction.md" "2023-07-26"
7-
"episodes/02-image-basics.md" "30630cc141643700495c2038f2cc9f68" "site/built/02-image-basics.md" "2023-12-08"
7+
"episodes/02-image-basics.md" "b17ae758d5d8a2a81348306a97b37067" "site/built/02-image-basics.md" "2023-12-11"
88
"episodes/03-skimage-images.md" "063fa4bb5032702c0196b0d953d94474" "site/built/03-skimage-images.md" "2023-12-08"
99
"episodes/04-drawing.md" "9d78a765f5e9747ffc2aa43a4a5a414d" "site/built/04-drawing.md" "2023-09-05"
1010
"episodes/05-creating-histograms.md" "59c07192c0a6217e8a42d3e7365025f9" "site/built/05-creating-histograms.md" "2023-12-08"

0 commit comments

Comments
 (0)