Images as Numbers: Pixels, Arrays & Colour
Computer vision has one foundational secret, and once you see it the whole field clicks: an image is just a grid of numbers. Every technique — filters, edge detection, face recognition, a self-driving car''s perception — is arithmetic on that grid. This lesson makes images concrete so the rest of the course is manipulation you can follow, not magic.
A grayscale image is a matrix
A grayscale image is a 2-D matrix of pixel intensities. Each pixel is a number, usually $0$ (black) to $255$ (white), with greys in between. A tiny $2\times3$ image is literally:
$$\begin{bmatrix} 0 & 128 & 255 \\ 64 & 200 & 32 \end{bmatrix}$$
Its shape is (height, width) = (2, 3). The row index is the $y$-coordinate (top to bottom), the column index is $x$ (left to right). A real 1080p photo is just a much bigger version — a $1080 \times 1920$ matrix of these numbers.
Colour adds a third dimension
A colour image stacks three such matrices — one each for Red, Green, Blue — into a 3-D array (a tensor) of shape (height, width, 3). Pixel $(y, x)$ is now a triple like $[255, 0, 0]$ (pure red) or $[255, 255, 255]$ (white). Mixing the three channels produces every colour you see. So a colour photo is (H, W, 3); a batch of them (what a model trains on) is (N, H, W, 3). Reading and reasoning about these shapes is the single most important CV skill.
Operations are just arithmetic on the grid
Because an image is numbers, editing it is arithmetic:
- Brighten by adding a constant to every pixel (clamped at $255$).
- Invert with $255 - \text{pixel}$ (a photographic negative).
- Grayscale from colour with a weighted average of the channels, e.g. $0.299R + 0.587G + 0.114B$ (green is weighted most because our eyes are most sensitive to it).
Worked example. Inverting the pixel $128$ gives $255 - 128 = 127$. Converting the colour pixel $[100, 150, 200]$ to grey gives $0.299(100) + 0.587(150) + 0.114(200) = 29.9 + 88.05 + 22.8 = 140.75 \approx 141$.
Reading a shape in practice
The first thing to check when you load an image is its shape. img.shape == (720, 1280) is a 720-pixel-tall, 1280-pixel-wide grayscale image; (720, 1280, 3) is the same size in colour. Height times width gives the pixel count — a 720×1280 image is about 0.9 megapixels, i.e. 921,600 numbers to process (times 3 for colour). Knowing the shape tells you how much data you hold and whether two arrays will line up for an operation.
Common pitfalls
- Row/column vs x/y. Arrays are indexed
[row, col]=[y, x]— the opposite order to the $(x, y)$ you might expect from a graph. A frequent source of flipped or mislocated results. - Value range and type. Pixels are usually 8-bit integers $0$–$255$; adding past $255$ overflows unless you clamp or use floats.
- Channel order. Some libraries load colour as RGB, others (OpenCV) as BGR — mixing them swaps red and blue.
Why this is your on-ramp
Every later lesson operates on this grid: a filter slides a small matrix over it, a neural network learns weights that combine pixels, object detection draws boxes in $(x, y)$ coordinates. Hold onto the one idea — an image is an array of numbers — and computer vision becomes visible.
