Lite / Intelligent Robotic Systems / Week 25
Kernels: the moving average goes 2D
Nine numbers in a 3×3 grid can blur a photo, sharpen it or find its edges. How can so few numbers do so much?
Why should I care?
Kernels are the workhorse of image processing and the building block of the convolutional neural networks behind modern vision. Once you can apply one by hand, blurs, sharpening and edge detection stop being magic buttons.
- Apply a 3×3 kernel to a pixel neighbourhood by multiplying matching positions and adding.
- Explain the box blur as a 2D moving average, and what sharpening kernels do.
- Handle image edges and value ranges sensibly.
Predict
A box blur (nine weights of 1/9) is applied to a single bright pixel of 90 surrounded by zeros. What is the output at that pixel?
- 90
- 10
- 0
Guess first, then reveal
Only one of the nine neighbourhood values is non-zero: 90 × 1/9 = 10. The brightness is spread out across the neighbourhood, which is exactly what blurring means.
Discover
Choose Kernel. On the Noisy bar, compare Identity with Box blur: click a pixel and check the readout's sum by hand. Then try Sharpen on the Bright square. What happens at the square's edges, and why can values go above 255 or below 0?
What the interactive shows (text version)
In kernel mode the diagram shows the input grid and the filtered grid. Clicking a pixel lists the nine weight × value products and their sum. On the Noisy bar, the box blur evens out the random variation and spreads the bright bright speck into a dimmer 3 × 3 patch; sharpening the Bright square makes its edge pixels brighter and the surrounding background darker.
Watch (optional)
Watch for: Watch for the kernel being slid across the image one pixel at a time, and for the difference between a mean blur and a Gaussian blur's weights.
Walks a small kernel across an image to blur it, the same multiply-and-add you do by hand in the kernel lessons.
“How Blurs & Filters Work - Computerphile” by Computerphile (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
Understand
Slide, multiply, add
To filter an image, place the kernel over each pixel in turn, multiply each kernel weight by the pixel beneath it, add the nine products, and write the result into a new image at that position. Use a fresh output image: overwriting as you go corrupts the neighbours you still need.
Blur is averaging in 2D
The box blur averages each 3×3 neighbourhood, so independent pixel noise drops by about 3. Like the moving average, it costs sharpness: edges spread over several pixels. A Gaussian kernel weights the centre more than the corners, which blurs more gently for the same noise reduction.
Edges and ranges
At the border the kernel overhangs the image. Common fixes: repeat the nearest pixel (used in the diagram), mirror the image, or skip the border. Outputs can fall outside 0–255 (sharpening, edge kernels), so compute in a wider number type and clip or rescale only for display.
Explain it another way
Intuitive
Each pixel asks its eight neighbours for their opinion and blends them with its own, using the kernel as the voting rules.
Technical
2D discrete correlation g(x, y) = Σᵢⱼ k(i, j)·f(x + i, y + j); convolution uses k(−i, −j). Cost is 9 multiply-adds per pixel for 3×3; separable kernels like the box and Gaussian can be done as a row pass then a column pass.
Mathematical
A kernel whose weights sum to s scales flat regions by s. The box blur reduces the variance of independent noise by Σk² = 9 × (1/9)² = 1/9.
Applied
Convolutional neural networks learn their own kernel weights from data; the first layer of a trained network often ends up containing kernels that look like blurs and edge detectors.
Derivation: Applying a kernel to one pixel
Take the 3×3 neighbourhood centred on the pixel, values p(i, j) for i, j ∈ {−1, 0, 1}.
Take the kernel weights k(i, j) in the same layout.
Output = Σᵢ Σⱼ k(i, j) · p(i, j): nine products, added. (Strict mathematical convolution flips the kernel first, using k(−i, −j); for symmetric kernels like the blur it makes no difference.)
Box blur: all k = 1/9, so the output is the mean of the nine pixels: a 2D moving average. Noise falls by about √9 = 3.
Example patch rows [10, 20, 30] [40, 50, 60] [70, 80, 90] with a box blur: sum = 450, output = 450 ÷ 9 = 50.
Sharpen [[0, −1, 0], [−1, 5, −1], [0, −1, 0]] = identity plus (centre − neighbours). On that patch: 5×50 − (20 + 40 + 60 + 80) = 250 − 200 = 50, unchanged on a smooth ramp; at an edge it exaggerates the difference.
Weights summing to 1 keep average brightness; weights summing to 0 (edge kernels) give 0 on flat regions.
Worked example 1
Apply Sobel x [[−1, 0, 1], [−2, 0, 2], [−1, 0, 1]] to the patch [10, 10, 80] [10, 10, 80] [10, 10, 80].
Row sums: (−10 + 80) + 2(−10 + 80) + (−10 + 80) = 70 + 140 + 70 = 280. A large positive value: brightness rises strongly to the right, so there is a vertical edge.
Common mix-ups
Blurring removes noise without losing anything.
✓ It averages noise down and also smears real edges; it is the image version of the noise–lag trade-off.Kernel outputs always stay between 0 and 255.
✓ Kernels with negative weights or sums above 1 can go outside the range; keep the true values and clip only for display.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Write apply_kernel(img, k) in Python with nested loops and nearest-pixel edges. Test it against two hand-worked patches, then blur a noisy test image and measure how much the standard deviation of a flat region falls.
Reflect
How is a box blur like a moving average, and how is it different?
Write your answer on paper or in your own notes. Lite never stores what you write.