SimuVeda

Engineering · Simulator 099

Image Convolution

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.

How the model works

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.

Guided experiments

  1. Box blur. All nine weights 1, divisor 9. Prediction: sharp edges become 3-pixel ramps; the one-pixel diagonal line fades to about a third of its contrast.
  2. Sharpen. Centre 5, four neighbours −1 (sum 1). Prediction: flat areas keep their brightness exactly, but each edge gets a bright rim on the light side and a dark rim on the dark side, often clipped to white or black.
  3. Laplacian edges. Centre −4, four neighbours 1 (sum 0), offset 128. Prediction: every flat area becomes mid-grey (128), and only the outlines of the shapes stand out.
  4. Sobel, one direction. Detects left–right changes only. Prediction: the vertical sides of the square light up (one side bright, the other dark), while its top and bottom edges vanish. The background, which brightens gently from left to right, shows as a slightly lighter grey than 128./span>

Challenge

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?

Reveal the explanation

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.

References

  • Gonzalez, R. C. and Woods, R. E., Digital Image Processing, 4th ed. (2018), ch. 3.
  • Sobel, I. and Feldman, G. (1968), "A 3×3 isotropic gradient operator for image processing", presented at the Stanford Artificial Intelligence Project.
  • LeCun, Y., Bengio, Y. and Hinton, G. (2015), "Deep learning", Nature 521: 436–444.