Build a Filter
Edit a kernel and see what it does to an image. Inspect one neighborhood to understand every multiply and add.
How can nine numbers change the appearance of an image?
A filter combines nearby pixel values. Kernel weights determine the operation, border handling affects edge pixels, and display clipping can hide values outside the visible range.
Make a prediction.
Then move a control.
Build a filter
Edit a 3 × 3 kernel and follow the arithmetic for one pixel.
At the edge, repeat the nearest valid pixel. The output stays 32 × 32.
Convention: cross-correlation. The kernel slides without being flipped, as in PyTorch Conv2d. Mathematical convolution flips the kernel first.
Click either image to inspect a pixel. With an image focused, use the arrow keys. The blue box marks the selected output; the outlined neighborhood shows its source pixels.
Divide, clip to [0, 255], then round for display.
Pixel (13, 10)
Each cell shows source × weight = contribution. The kernel stays in the order you entered.
An educational experiment with illustrative data. The controls run locally in your browser.
The idea,
in Python.
A compact implementation of the core concept. Open the code when you are ready to connect the visual to the algorithm.
Open the core implementation Python ↗
import numpy as np
image = np.array([[0., 0., 0.], [0., 255., 0.], [0., 0., 0.]])
kernel = np.ones((3, 3)) / 9
padded = np.pad(image, 1, mode="constant")
raw = np.zeros_like(image)
for row in range(image.shape[0]):
for col in range(image.shape[1]):
patch = padded[row:row + 3, col:col + 3]
raw[row, col] = np.sum(patch * kernel)
# Unflipped kernel: cross-correlation, as used in many CNN APIs.
display = np.clip(raw, 0, 255).astype(np.uint8)
print(raw)Explain the change.
Test your reasoning.
- Compare blur, edge and sharpen kernels on the same image.
- Inspect a pixel near an edge and explain its raw filtered value.
- Change the border rule. Why do image boundaries behave differently?