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.
CollegeCollege image processing
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.
Patch: the left column is 0 (black) and the rest 160, a vertical edge.
Gaussian blur [1 2 1; 2 4 2; 1 2 1] / 16: the left weights (1 + 2 + 1) see 0, the other 12 see 160, so the output is 12 × 160 / 16 = 120. The edge becomes a ramp.
Sobel [−1 0 1; −2 0 2; −1 0 1]: (160 + 320 + 160) − 0 = 640, clipped to 255. A strong left-to-right change.
Laplacian (centre −4, four neighbours 1) with offset 128: 160 + 160 + 160 + 0 − 4 × 160 = −160, plus 128 = −32, clipped to 0. The dark side of the edge.
In a flat area blur returns 160, while Sobel and Laplacian give 0 (128 with offset). Edit a kernel and step to a pixel beside an edge.
Sliding a small grid of weights over the image and replacing each pixel with the weighted sum of its neighbourhood. Different weights blur, sharpen or detect edges.
So that a flat area keeps its brightness. Edge-detection kernels add up to 0, so flat areas become 0 and only changes stand out.
Convolutional neural networks learn their kernel weights from data. Early layers end up finding edges and textures, and deeper layers combine them into shapes and objects.