SimuVeda/ Engineering

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.

CollegeCollege image processing

Experiments 0 of 0 Challenge not tried

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.

Related simulators

Worked example

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.

Common questions

What is image convolution?

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.

Why do blur kernels add up to 1?

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.

How is convolution used in neural networks?

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.

Keyboard shortcuts

Space
Play or pause
S
Step
R
Reset
F
Full screen
?
Show this list