Skip to content
Tactiverge

School/○ Innovator/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?

About 40 min · 3 goals · Checked by automated tests

1.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.

2.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?

Guess first. Being wrong here is how the surprise works.

3.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?

Loading the interactive…

Can't use the interactive? Read a description instead

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.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

Box blur (all weights 1/9) on a patch whose nine values add to 1080. What is the output?

5.Watch

→ 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.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

6.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

Each pixel asks its eight neighbours for their opinion and blends them with its own, using the kernel as the voting rules.

Derivation: Applying a kernel to one pixel
  1. Take the 3×3 neighbourhood centred on the pixel, values p(i, j) for i, j ∈ {−1, 0, 1}.

  2. Take the kernel weights k(i, j) in the same layout.

  3. 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.)

  4. 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.

  5. Example patch rows [10, 20, 30] [40, 50, 60] [70, 80, 90] with a box blur: sum = 450, output = 450 ÷ 9 = 50.

  6. 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.

  7. 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.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

Apply the box blur (all nine weights 1/9) kernel to the centre pixel of this 3×3 patch by multiplying matching positions and adding. Patch rows: [210, 50, 40] [210, 230, 0] [250, 10, 70]. What is the output value?

Practice 3 · a fresh version every time

Apply the sharpen [[0,−1,0],[−1,5,−1],[0,−1,0]] kernel to the centre pixel of this 3×3 patch by multiplying matching positions and adding. Patch rows: [130, 150, 140] [110, 40, 10] [180, 250, 170]. What is the output value?

Practice 4

A kernel's weights add up to 2. Applied to a flat grey region of 100, what does it output?

Practice 5

Why write filter results into a new image rather than overwriting the input as you go?

8.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.

9.Reflect

Concepts: Convolution and image filters · Noise and filtering · Images as arrays of numbers