Sub topic 1
An image is a grid of sampled numbers
Prerequisites
This chapter assumes basic Python, familiarity with NumPy arrays, and the idea that a byte holds 8 bits. Install NumPy with pip install numpy before running the code.
Pixels as samples
A digital image is a rectangular grid of samples. A camera measures light at many points, and each measurement becomes one number stored at one grid position. Each position is a pixel.
Width, height, and resolution
The width and height count the columns and rows of the grid. A 1920 × 1080 image has 1920 columns and 1080 rows, which gives 2,073,600 pixels. Resolution is usually quoted as these counts.
Bit depth: how many values a sample can take
Bit depth is the number of bits used to store one sample. With b bits, a sample can take 2^b distinct values. An 8-bit sample takes 256 values, from 0 to 255. A 1-bit sample takes only 0 or 1.
Channels: several numbers per pixel
A channel is one component stored for each pixel. An RGB pixel has three channels: red, green, and blue. The full image is an array with the shape (height, width, channels).
import numpy as np
img = np.zeros((2, 3, 3), dtype=np.uint8) # 2 rows, 3 columns, 3 channels
img[0, 0] = (255, 0, 0) # top-left pixel is red
print(img.shape, img.dtype, img.nbytes) # (2, 3, 3) uint8 18The raw size is height × width × channels × bytes per sample. Here that is 2 × 3 × 3 × 1 = 18 bytes.
Grayscale and binary images
A grayscale image has one channel. Each pixel stores a single brightness value. A binary image is a grayscale image with 1-bit samples, so each pixel is either black or white.
Exercise
A 640 × 480 RGB image stores 8 bits per channel. How many bytes of raw pixel data does it need?
Show answer
640 × 480 × 3 × 1 = 921,600 bytes.