Kolton Ndoj

Convolution: how photo apps blur and find edges

When your camera app blurs the background, sharpens a photo, or a filter outlines a face

What problem does it solve?

How can one simple rule blur an image, sharpen it, or find its edges?

The big idea

Slide a small grid of numbers, a 'kernel', over the image, and replace every pixel with a weighted sum of its neighbourhood. All equal weights give a blur (each pixel becomes the average of its neighbours). Weights of −1, 0, +1 across a row measure how fast brightness changes, and big changes are edges. Same machine, different 3×3 numbers, totally different effects. The AI that recognises faces learns its own kernels.

How it works, step by step

  1. 1. Pick a kernel, e.g. a 3×3 grid of 1/9s for a blur.
  2. 2. Centre it on a pixel, multiply each overlapping pixel by its weight, and add them up.
  3. 3. Write that sum into the new image at the same spot.
  4. 4. Slide to the next pixel and repeat across the whole picture.

The key formula

(I∗K)(x,y)=∑i∑jI(x−i, y−j) K(i,j)(I*K)(x,y)=\sum_{i}\sum_{j} I(x-i,\,y-j)\,K(i,j)
II
the image: brightness at each pixel
KK
the kernel, a small grid of weights
∑∑\sum\sum
add up over the whole little neighbourhood; the minus signs flip the kernel, which makes no difference for symmetric kernels like a blur but flips the sign of an edge detector

How fast is it?

For an image with P pixels and a k×k kernel, about P·k² multiplications. Phones do this on special hardware. Big blurs use a trick: by the convolution theorem, convolving becomes simple multiplication after a Fourier transform.

Try it yourself

Fun fact

Blurring three times with a simple box blur gives a very close approximation of a smooth Gaussian blur. That's the central limit theorem at work, the same reason so many things in nature follow a bell curve.

Related

Open it in the Math Lab →

Media · Intermediate