A picture is a signal too.
Every signal so far has been a single line that rises and falls as time passes. A photograph looks like a different thing altogether, and yet underneath it is the same kind of thing, a long list of numbers. This chapter shows where those numbers come from, and what happens when you slide a shape across them.
A grid of numbers
Stand close to a mosaic and the picture dissolves into small colored tiles. A digital photograph is built the same way. It is a grid of tiny squares called pixels, short for picture elements, and each pixel is one flat color. From a normal distance the squares are too small to see, and they blend into a picture.
A camera produces this grid directly. Behind the lens sits a sensor, a chip covered with millions of tiny light meters set out in rows and columns, like buckets lined up in a field to catch rain. While the shutter is open, each one collects the light that falls on it. Afterward the camera writes down how much each one caught, as a number. A small number means little light arrived, so that pixel is dark. A large number means the pixel is bright. Most pictures use the numbers 0 to 255, where 0 is black and 255 is white.
A color picture stores three numbers for every pixel: how much red, how much green and how much blue. Every color a screen can show is a mix of those three. When this chapter needs a single number for a pixel, it uses the pixel's brightness, which is a blend of the three that leans on green, because green is the color the eye is most sensitive to.
Drag the ring around the picture on the left. The grid on the right shows the pixels inside the small box at the center of the ring, enlarged, with the brightness of each one written on it.
the picture
the pixels in the box, enlarged
Try the edge of the sun, where the numbers fall from the 240s to the 180s within two or three pixels, and then a patch of open sky, where neighboring pixels differ by one or two. A phone camera with twelve megapixels records twelve million of these pixels in every photograph, which comes to thirty-six million numbers. Everything a computer does to a photograph, from brightening it to recognizing a face in it, is arithmetic on those numbers.
One row is a signal
Take a single row of the grid and read its brightness numbers from left to right. Drawn as a chart, they make a line that is high where the picture is bright and low where it is dark. It is the same idea as the elevation profile of a hiking trail, which shows how high the ground is at each point along the route. Here the route is one row of the picture, and the height is brightness.
That line is a signal in exactly the sense of the first four chapters. The only difference is what it runs along. The earlier signals ran through time, one measurement after another. This one runs across a distance, from the left edge of the picture to the right.
Drag the line up and down the picture and watch the chart underneath it.
Three things are worth finding. In the open sky the line slopes gently, because each pixel is nearly the same as its neighbor. Where the row crosses from bright sky into a dark tree, the line drops off a cliff, and this is the important one: an edge in a picture is a sudden jump in the signal. And where the row runs through the ripples on the water, or through the small letters of the eye chart, the line wiggles rapidly up and down. That rapid wiggling is what fine detail looks like.
A picture is a stack of these signals, one for every row. It is also a stack of columns, read from top to bottom, and that is the one real difference between a picture and a sound. A sound has a single direction to move in, forward through time. A picture has two.
Slide a shape across a picture
Chapter 1 slid a short list of weights along a signal, multiplying and adding at every position. A picture gets the same treatment with one change. Because a picture runs in two directions, the list of weights becomes a small grid of weights, usually three pixels wide and three pixels tall.
The grid is laid over one pixel and the eight pixels that surround it. Each of the nine pixels is multiplied by the weight lying on top of it, and the nine results are added together. That total becomes the new value of the pixel in the middle. Then the grid moves over by one pixel and the same thing happens again, and so on until every pixel in the picture has had its turn.
As in chapter 1, the weights decide what the operation does. Choose a set of weights below and drag the divider across the picture to compare before and after. Point at any pixel, or tap it, to see the arithmetic for that one pixel on the right.
the nine weights
Blur uses nine equal weights, so every pixel is replaced by the average of its neighborhood. That is the seven-day average from chapter 1, taken in two directions at once. A single pass is subtle, because three pixels is a small neighborhood. Run it several times and each pass spreads the average a little wider.
Upright edges has negative weights on its left side and positive weights on its right. Where the picture is flat, the two sides cancel and the total is zero, which shows as black. Where the brightness changes from left to right, the two sides no longer cancel, and the total is large. The result is a picture that is bright only along edges that stand upright, such as the sides of the radio mast. Level edges is the same grid turned a quarter turn, and it answers to edges that lie flat, such as the far shoreline. These two grids were described in a talk at Stanford in 1968 by Irwin Sobel and Gary Feldman, and they are still in everyday use under Sobel's name.
Sharpen multiplies the middle pixel by five and subtracts its four nearest neighbors. In a flat area that changes nothing, since five of a number minus four of the same number is the number itself. At an edge it pushes the bright side brighter and the dark side darker, which makes the edge look crisper. Run it several times and you will see why photographers warn against too much sharpening.
You can also type weights of your own into the grid. Whenever the weights add up to something other than zero, the page divides the result by that total, so the picture keeps its overall brightness. When they add up to exactly zero, as the edge finders do, results can be negative as well as positive, and the page drops the sign so that every edge shows as light on black.
Finding edges is usually the first thing a computer does when it has to make sense of a picture, because edges are where one thing ends and another begins. A scanning app on a phone looks for the four long edges of a sheet of paper. Many cars watch the edges of the painted lane lines and warn a driver who drifts across one. A checkout scanner reads a barcode by measuring the distances between the edges of its bars.
The question of size
A grid three pixels wide has a narrow view. It responds to anything that changes within three pixels, and that includes the outline of a mountain, but also every ripple, every star and every speck of grain. To pick out the large outlines alone, the usual method is to blur the picture first, which wipes out everything smaller than the blur, and to look for edges afterward. The amount of blur decides how large a thing has to be before it counts.
At the smallest setting the edge picture is crowded with stars, ripples and the thin wires of the mast. As the slider moves up, those disappear one kind at a time, until only the ridges and the shoreline are left. On the eye chart the effect is even plainer: the rows of letters vanish from the bottom of the chart upward, smallest first.
Every position of that slider is right for something. The small setting shows where the fine detail is and the large setting shows the big shapes, and a real picture has both. The way out is to stop choosing, look at every size, and keep all of the results side by side. That is what a wavelet transform does, and it is the subject of the next two chapters, first on a simple line and then on a photograph.
The loop
Written as a program, the sliding grid is the loop from chapter 1 with one more loop wrapped around it, since the grid has to visit every row as well as every position along a row. A picture is stored as one long list, row after row, so the pixel in column x of row y sits at position y * w + x, where w is the width of the picture.
/* Slide a 3 by 3 grid of weights k across a picture.
img and out hold h rows of w pixels each, stored row after row. */
void filter3x3(const float *img, int w, int h, const float k[9], float *out)
{
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
float sum = 0.0f;
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
int yy = y + j, xx = x + i;
if (yy < 0) yy = 0; /* past the border, reuse the edge pixel */
if (yy >= h) yy = h - 1;
if (xx < 0) xx = 0;
if (xx >= w) xx = w - 1;
sum += k[(j + 1) * 3 + (i + 1)] * img[yy * w + xx];
}
}
out[y * w + x] = sum;
}
}
}#include <algorithm>
#include <array>
#include <vector>
// Slide a 3 by 3 grid of weights across a picture stored row after row.
std::vector<float> filter3x3(const std::vector<float>& img, int w, int h,
const std::array<float, 9>& k)
{
std::vector<float> out(img.size());
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
float sum = 0.0f;
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
int yy = std::clamp(y + j, 0, h - 1); // past the border, reuse the edge pixel
int xx = std::clamp(x + i, 0, w - 1);
sum += k[(j + 1) * 3 + (i + 1)] * img[yy * w + xx];
}
}
out[y * w + x] = sum;
}
}
return out;
}/// Slide a 3 by 3 grid of weights across a picture stored row after row.
pub fn filter3x3(img: &[f32], w: usize, h: usize, k: &[f32; 9]) -> Vec<f32> {
let mut out = vec![0.0; img.len()];
for y in 0..h {
for x in 0..w {
let mut sum = 0.0;
for j in 0..3 {
for i in 0..3 {
// past the border, reuse the edge pixel
let yy = (y + j).saturating_sub(1).min(h - 1);
let xx = (x + i).saturating_sub(1).min(w - 1);
sum += k[j * 3 + i] * img[yy * w + xx];
}
}
out[y * w + x] = sum;
}
}
out
}import numpy as np
from scipy.ndimage import correlate
blur = np.ones((3, 3)) / 9
upright_edges = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
def filter3x3(img, k):
"""Slide a 3 by 3 grid of weights across a picture. Border pixels are repeated."""
h, w = img.shape
padded = np.pad(img, 1, mode="edge")
out = np.zeros((h, w))
for j in range(3):
for i in range(3):
out += k[j, i] * padded[j:j + h, i:i + w]
return out
# the same thing, the way you would really write it:
out = correlate(img, upright_edges, mode="nearest")