Engineering · Simulator 099
Many image effects come from one simple operation: slide a small grid of numbers, the kernel, across the picture, and replace each pixel with a weighted sum of its neighbours. Edit the kernel or pick a classic one, then step through the image pixel by pixel to see each sum being made.
For each output pixel, the kernel K is centred on the matching input pixel, each of the 9 neighbours is multiplied by the kernel weight on top of it, and the products are added:
out(x, y) = Σᵢ Σⱼ K(i, j) · in(x + i, y + j) / divisor + offset, i, j ∈ {−1, 0, 1}
(Strictly, flipping the kernel first makes this a convolution rather than a correlation; for the symmetric kernels used here it makes no difference.) Results are clipped to the range 0 (black) to 255 (white). At the image border, missing neighbours copy the nearest edge pixel.
The sum of the weights tells you what happens in flat areas, where all neighbours are equal: a sum of 1 keeps the brightness (blur, sharpen), a sum of 0 turns flat areas to 0 and keeps only changes (edge detectors), which is why an offset of 128 is often added to show negative values as darker than mid-grey.
Blur kernels average neighbours. Sharpen adds the difference between a pixel and its neighbours, exaggerating edges. The Laplacian responds to edges in every direction; Sobel kernels respond to brightness changing in one direction, the basis of many edge detectors in computer vision and the first layer of convolutional neural networks.
Greyscale 32 × 32 test image, one 3 × 3 kernel at a time, edge pixels replicated, and integer rounding of the output.
A pixel of brightness 100 sits in a flat region of value 100, except that its right neighbour is 200. What does the sharpen kernel (centre 5, four neighbours −1) output there? What does the box blur output?
Sharpen: 5 × 100 − (100 + 100 + 100 + 200) = 500 − 500 = 0, so the pixel turns black: the dark side of the edge is pushed darker. Box blur: (8 × 100 + 200) / 9 ≈ 111, slightly brighter as the edge is spread out. Edit a kernel and step to a pixel next to an edge to check.