Sub topic 1
An image is a grid of sampled numbers
Prerequisites
This chapter assumes basic Python programming, familiarity with arrays or lists of numbers, and a basic understanding of binary values and bytes.
From a scene to a pixel grid
A digital image represents a scene by sampling it at regularly spaced positions. Each sample becomes a pixel, which is a small picture element containing one or more numeric values.
A two-dimensional image has a height and a width:
- Height is the number of pixel rows.
- Width is the number of pixels in each row.
- A pixel coordinate is commonly written as
(row, column)or(y, x). - The top-left pixel usually has coordinate
(0, 0).
An image is therefore an array. For a grayscale image, each array element is one brightness value. For a colour image, each element contains several channel values.
Sampling loses information. A camera sensor may record a continuous scene, but the digital image stores measurements only at fixed locations and with finite numeric precision. Increasing the number of samples usually preserves more spatial detail, but it also increases storage and processing costs.
Coordinates and array shape
In Python and NumPy, an image is often represented with shape (height, width) for grayscale data or (height, width, channels) for colour data. The first axis selects a row, the second selects a column, and the optional third axis selects a channel.
import numpy as np
image = np.array([
[10, 20, 30],
[40, 50, 60],
], dtype=np.uint8)
print(image.shape) # (2, 3)
print(image[1, 2]) # 60This image has two rows and three columns, so it contains six pixels. The value 60 is the brightness of the pixel at row 1, column 2. With zero-based indexing, that is the second row and third column.
The array's shape describes its organisation, not necessarily how it will appear on a screen. Display software must interpret the values, choose a colour model, and map numeric values to visible colours.
Spatial resolution
The pair (width, height) describes the image's spatial resolution. A 1920 × 1080 image contains 2,073,600 pixel positions. Resolution alone does not describe detail perfectly: lens quality, focus, motion blur, and sampling conditions also affect what information the image preserves.
When an image is resized, new pixel values may be created by interpolation, or existing samples may be discarded. The result is another grid, not a recovery of details that were never sampled.
Exercise
In a grayscale NumPy image with shape (480, 640), which coordinate accesses the pixel in the last row and last column?
Show answer
Use image[479, 639], because both axes use zero-based indexing.