Blur, sharpen, emboss, edge detection: many of the filters found in any graphics program rest on the same mathematical operation, convolution. Once you understand the mechanism, filters stop being mysterious effects and become predictable tools that you can tune sensibly, or even write yourself in a few lines of code.
The idea: every pixel looks at its neighbours
A convolution computes the new value of each pixel as a weighted sum of the pixel itself and its neighbours. The weights are held in a small matrix called a kernel (or mask), usually 3 × 3, 5 × 5 or larger.
The procedure for each pixel is:
- center the kernel on the pixel;
- multiply each weight by the value of the pixel beneath it;
- add up all the products;
- the result is the new value of the central pixel.
This is repeated for every pixel, always reading from the original image and writing to a new one. If you wrote to the original, pixels already processed would affect the following ones.
Blur
The simplest blur kernel is the average (box blur): all weights equal, summing to 1.
1/9 1/9 1/9
1/9 1/9 1/9
1/9 1/9 1/9
Each pixel becomes the average of the nine pixels around it. The result is soft, but with a flaw: it produces boxy artifacts, because all neighbours count equally regardless of distance.
Gaussian blur uses weights that decrease with distance from the center following a bell curve. The radius (or sigma) parameter controls the width of the bell. The result is more natural and free of hard corners. A practical advantage: a two-dimensional Gaussian kernel can be split into two one-dimensional passes, one horizontal and one vertical, saving an enormous amount of computation at large radii.
An important rule: the weights must sum to 1 so as not to change the image's average brightness. If the sum is greater, the image lightens; if smaller, it darkens.
Sharpening
Sharpening means emphasising the differences between a pixel and its neighbours. A classic kernel is:
0 -1 0
-1 5 -1
0 -1 0
The weights still sum to 1, but the central pixel is reinforced and the neighbours subtracted. In flat areas nothing changes; at edges the difference is exaggerated.
The unsharp mask
The most widely used sharpening tool in photography, the unsharp mask, works like this:
- create a blurred copy of the image;
- subtract the blurred copy from the original, leaving only the detail;
- add a fraction of that detail back to the original.
The three typical parameters are:
| Parameter | What it controls |
|---|---|
| Amount | how much detail to add |
| Radius | the size of the detail to emphasise |
| Threshold | the minimum difference for a pixel to be changed, useful to avoid accentuating noise |
A small radius (under one pixel) reinforces fine texture; a large radius increases local contrast. Overdo it and halos appear, light and dark rims around outlines.
Edge detection
A kernel whose weights sum to zero produces zero in flat areas and high values only where intensity changes. This is the principle of edge detection. The Sobel operator uses two kernels, one for horizontal variations and one for vertical ones:
-1 0 1 -1 -2 -1
-2 0 2 0 0 0
-1 0 1 1 2 1
Combining the two results gives the edge's strength and direction at each point. The Laplacian operator instead measures the variation of the variation and responds to edges in all directions at once, but it is very sensitive to noise: that is why it is usually applied after a light Gaussian blur.
The problem of image borders
At the margins the kernel "sticks out" of the image. Common strategies are:
- extend by repeating the edge pixels;
- reflect the image as in a mirror;
- wrap, using pixels from the opposite side (suitable for tileable textures);
- pad with zero, which however creates dark edges.
The choice matters most with large kernels: a strong blur with zero padding produces an obvious dark frame.
Beyond convolution: the median filter
Not all filters are convolutions. The median filter replaces each pixel with the median value of its neighbourhood, not with an average. It is a non-linear filter, very effective against "salt and pepper" noise, isolated pixels that are completely white or black, because those extreme values do not affect the median. Unlike blurring, it preserves sharp edges well.
In my work these tools also have an analytical use: edge-detection and high-frequency extraction filters help highlight differences in sharpness or noise between areas of an image that are not noticeable to the naked eye.
In short
- A convolution computes each pixel as a weighted sum of its neighbours according to a kernel.
- Weights summing to 1 keep average brightness unchanged; summing to 0 yields only the variations.
- Gaussian blur weights neighbours by distance and can be split into two passes.
- The unsharp mask adds to the original the difference between the image and a blurred version.
- The median filter is not a convolution and removes impulse noise while preserving edges.