LABS · 06  /  CONVOLUTION

The Sliding Stencil.

Slide a tiny 3×3 stencil across an image. Everywhere it lands, it multiplies the pixels underneath by its weights and adds them up — one number, one output pixel. Change the stencil and the same image becomes edges, or blur, or sharpened.

THE GIST · 20 SECONDS

Convolution slides a small kernel (weight grid) over an image. At each spot it does a dot product: multiply each pixel by the overlapping weight, sum them, write one output pixel. The output is a feature map. Swap the kernel and the job changes — center-vs-surround weights find edges, equal weights blur. CNNs just learn these kernels.

  • Kernelthe 3×3 weight grid
  • Slidemove over every pixel
  • Multiply-sumdot product → 1 pixel
  • Feature mapthe filtered output
INPUT OUTPUT

Pick a kernel on the right to transform the image instantly — or hit Play to watch the stencil slide and fill the output pixel by pixel.

UNDER THE HOOD

What you just played, written down

One operation — multiply the overlap and add it up — repeated at every pixel. Change the weights and the same machine becomes an edge finder, a blur, or a sharpener.

How convolution works — four moves

  1. Place the kernel. Center the 3×3 weight grid over an input pixel.
  2. Multiply the overlap. Multiply each pixel under the kernel by the weight sitting on top of it.
  3. Sum to one pixel. Add all nine products into a single number — that's one pixel of the output.
  4. Slide and repeat. Move one pixel over and do it again, across the whole image, to build the feature map.
WHY EDGES LIGHT UP

An edge kernel's weights sum to zero (big positive center, negative surround). Over a flat patch the parts cancel to near-zero (dark); at a boundary, the mismatch doesn't cancel, so the sum spikes (bright). Edges pop, smooth areas vanish.

The whole thing in code

def convolve(img, kernel):
    H, W = len(img), len(img[0])
    out = [[0] * W for _ in range(H)]
    for r in range(H):
        for c in range(W):
            s = 0.0
            for i in (-1, 0, 1):
                for j in (-1, 0, 1):
                    rr = min(H - 1, max(0, r + i))   # edge pad
                    cc = min(W - 1, max(0, c + j))
                    s += img[rr][cc] * kernel[i + 1][j + 1]
            out[r][c] = s                            # one pixel
    return out
EDGEsums to 0flat → dark, edge → bright
BLURsums to 1averages neighbors
CNNs LEARN THE KERNEL

Here you pick the weights by hand. A convolutional neural network learns them from data — early layers discover edge detectors on their own, later layers combine them into textures, parts, and whole objects.

⚠ Edges need padding

At the image border the 3×3 window hangs off the edge. Padding — zeros or replicated edge pixels — fills the gap so the output can stay the same size. Without it, the output shrinks by the kernel's radius.

↔ Stride & channels

Stride is how far the kernel jumps each step; a bigger stride downsamples. Real images have colour channels, so kernels are 3-D (e.g. 3×3×3) and a layer stacks many of them.

★ Where it's used

Photo filters, edge and blur in image editors, feature extraction, and every layer of a CNN for vision — the backbone of image classification and detection.

SOLVE IT YOURSELF

Solve it: one convolution step

This one is yours to write — in Python or TypeScript, running for real in your browser. Take a 3×3 patch and a 3×3 kernel and return the single output pixel. Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are below and Reveal solution is one click away.

YOUR TASK

Implement apply_kernel(patch, kernel): both are 3×3 grids (lists of lists). Multiply each patch value by the matching kernel weight and return the sum — the single output pixel for that window.

HINTS — 4 IDEAS
  1. Loop over rows i and columns j from 0 to 2.
  2. Multiply patch[i][j] by kernel[i][j] and add it to a running total.
  3. Return the total — that's the convolution output for this position.
  4. An edge kernel like [[-1,-1,-1],[-1,8,-1],[-1,-1,-1]] returns 0 on a flat patch.
CPython · WebAssembly
QUICK CHECK

Did it stick?

FAQ

Convolution, answered

What is image convolution?

Sliding a small kernel over an image; at each spot, multiply each pixel by the overlapping weight, sum the products into one output pixel, and repeat. The result is a filtered image (a feature map).

What is a kernel?

The small weight grid (usually 3×3) that defines the filter. Center-heavy-with-negative-surround detects edges; all-equal weights blur; a boosted center sharpens.

Why do edges light up?

An edge kernel's weights sum to zero. On a flat patch the positive and negative parts cancel (dark); at a boundary they don't, so the output spikes (bright).

How does this relate to CNNs?

A CNN uses convolution layers but learns the kernel weights from data instead of hand-picking them — early layers find edges, later ones build up to objects.

What are padding and stride?

Padding adds a border so edge pixels get a full window and the output keeps its size. Stride is the step size; a bigger stride skips pixels and downsamples.

RESULT

Next → stop hand-picking the weights and learn them from labelled images instead, stacking many layers — that's a convolutional neural network.

Finished this one? 0 / 12 Labs done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs