Convolutional Filters — the building blocks of CNNs
Design 3×3 kernels and see in real time how they transform images
What is a convolution?
Picture a stencil that you lay over every single pixel of an image. Whatever lies under the stencil gets weighted and summed — and that sum is the new pixel. Move the stencil over, do the same there, and repeat across the whole image.
Different stencils (kernels) yield different effects: a stencil with negative values on the left and positive values on the right finds vertical edges. A stencil with all equal positive values blurs the image. Convolutional Neural Networks (CNNs) are built on exactly this principle.
Analogy:
Picture a stencil that you lay over every single pixel of an image. Whatever lies under the stencil gets weighted and summed — and that sum is the new pixel. Move the stencil over, do the same there, and repeat across the whole image.
Different stencils (kernels) yield different effects: a stencil with negative values on the left and positive values on the right finds vertical edges. A stencil with all equal positive values blurs the image. Convolutional Neural Networks (CNNs) are built on exactly this principle.
Definition:
Mathematically this is a discrete 2D convolution. For every output pixel at position (x,y) you sum products of kernel values K(i,j) and neighbouring image pixels I(x+i, y+j). For a 3×3 kernel, i and j range from −1 to +1.
out(x,y) = ΣᵢΣⱼ K(i,j) · I(x+i, y+j). For normalised kernels you additionally divide by the sum of all kernel values so that overall brightness is preserved. CNNs learn the K(i,j) values from data — instead of setting them by hand like we do here.
How convolution works
A convolution is a handful of simple steps repeated for every pixel of the image:
- Kernel — the stencilThe kernel is a small matrix (here 3×3 — that is, 9 cells). Why 3×3 and not 100×100? Because a tiny window around the target pixel is enough to detect local features like edges — and 9 values are fast to compute. In real CNNs, 3×3, 5×5 or 7×7 are standard. Each value weights a neighbour pixel: positive values boost, negative values subtract. The centre cell is the target pixel (anchor).
- Sliding — pixel by pixelSlide the kernel over every pixel of the image. At each position: multiply each kernel value by the pixel underneath it, sum all 9 products — that sum is the new pixel value. At the border the outermost pixel is replicated so nothing gets cut off.
- Divisor & offset — controlling brightnessAfter summing, the result is adjusted twice more: divided by the divisor (so a blur kernel with nine 1s doesn't make the image 9× as bright) and shifted by the offset (so e.g. the Emboss kernel slides up by 128 into the visible mid-grey range instead of clipping to black). The stats panel on the right shows both values live.
- CNNs learn the kernelsIn a CNN, humans don't fix the kernel values anymore — the network learns them automatically from training data. The first layer learns simple kernels like edges and corners. Later layers combine these into more complex features (textures, facial features, objects).
From this simple operation grows the whole art of image processing: Photoshop filters, edge detection in classical computer vision, and — most importantly — the foundation of modern AI image recognition. Same mathematics, whether you design a Sobel-X stencil by hand or a neural network trains millions of them automatically.
Convolution workshop
What this demo shows
A filter is a tiny grid of nine numbers that travels across an image and redraws it bit by bit — depending on the numbers, the result turns soft, sharp-edged or embossed.
- What you see
- Two images side by side: on the left the original, on the right the filtered result, and between them a grid of nine numbers — the kernel. A glowing frame marks the small 3×3 window currently sitting on the image.
- What happens
- The window travels across the image pixel by pixel. Depending on the numbers in the kernel, the right image turns soft and blurred (blur), or the surfaces vanish and only bright outlines remain (edge filter, Sobel), or edges stand out harder (sharpen), or the motif looks pressed into stone in grey tones (emboss).
- What you can do
- Pick a motif on the left, tap one of the eight preset filters on the right, or type your own numbers into the grid. Press Play to let the window travel on its own; click the image to jump to any spot.
- What to watch for
- It is always the very same calculation — the nine numbers alone decide whether the image is blurred, broken into edges or embossed. Image AIs (CNNs) stack exactly these small filters on top of each other to learn how to see.
Pick a source image on the left, design a 3×3 stencil (kernel) in the middle — the result appears on the right. The default stencil is Sobel-X: it lights up vertical edges.
1 Source image
Pick a preset or upload your own image.
2 3×3 kernel
Each cell weights a neighbour pixel. Click in and edit values — result updates live.
+ positive (boosts) − negative (subtracts) • anchor (target pixel)
Presets
Advanced options
3 Result
Live preview after applying the kernel.
Live convolution — show me how it works
Click anywhere on the source image or start the animation. You'll see exactly how the kernel computes at one position: the 3×3 neighbours get multiplied by the kernel values, all 9 products are summed, divided by the divisor, and the offset added. That produces one single pixel in the output.
Tip: watch how Σ changes when the cursor passes an edge — for Sobel-X it jumps far positive or negative.
Play ideas — try these 5 challenges
- Set all 9 kernel values to +1 (all ones). That's Box-Sum, NOT Box-Blur. Observe: the image is massively over-bright. Now turn on "Normalize" in Advanced Options — boom, brightness back to normal. That's why we normalise.
- Sobel-X finds vertical edges, Sobel-Y finds horizontal. Which preset finds ALL edges at once — regardless of direction? (Hint: It's not Sobel-X+Sobel-Y, it's a different kernel.)
- Turn on "Stack filters". First add Gauss blur, then Sharpen. Compare against just-Sharpen. What do you see? Sharpening only brings back part of the detail the blur erased.
- Upload your own photo (or use the Portrait). Which preset looks best to you — and WHY? Pay attention to edges, textures, colour.
- In the Sobel-X kernel, change only the left column from -1, -2, -1 to -2, -4, -2. That's a stronger version. What happens? What does this teach us about scaling kernels?
Conv filter — discrete 2D convolution
What a convolution actually does
A convolution is a pointwise linear operation: for every output pixel you sum weighted contributions from a small neighbourhood of the source image. The weights live in a small matrix — the kernel.
Mathematically: out(x, y) = ΣᵢΣⱼ K(i, j) · I(x+i, y+j). For a 3×3 kernel, i and j range from −1 to +1. The resulting sum is optionally divided by a divisor (brightness preservation) and shifted by an offset (e.g. +128 for emboss, so negative differences remain visible).
Replicate padding at the border
At the image border, neighbour pixels are missing — the kernel hangs off the edge. Naive approach: insert zeros (zero-padding) → that produces black borders. Better: repeat the outermost pixel of the image (replicate). That's what this demo uses: no visible border artefact.
Complexity
- Time: O(W · H · k²) per layer — a 3×3 kernel on a 192×192 image is roughly 330,000 multiply-adds. Under 10 ms on a modern CPU.
- Memory: O(W · H) for the output buffer. Plus k² values for the kernel.
- Parallelisable: every output position is independent — GPUs (and CNNs) process them all at once.
Classical applications
- Edge detection: Sobel-X/Y, Laplacian — pre-processing for object recognition
- Smoothing: box blur, Gaussian blur — reduce noise before other steps
- Sharpening: unsharp masking
- Embossing: relief-like effects
- CNNs: the same operation, but the kernel values are learned — the foundation of modern computer vision
Try it in the demo! Set the kernel to Identity — the image stays unchanged. Click Sobel-X — suddenly vertical edges glow white on black. Edit individual cells — and see how tiny changes turn the result on its head.
1
# Conv filter — 2D convolution with a 3×3 kernel
2
function convolve(image, kernel, divisor, offset):
3
# For every pixel in the image
4
for y in 0..image.height:
5
for x in 0..image.width:
6
7
# 1. Gather 3x3 neighbours (replicate at border)
8
neighbours = get_patch(image, x, y, replicate=true)
9
10
# 2. Element-wise multiply + sum
11
sum = 0
12
for i in 0..9:
13
sum += neighbours[i] * kernel[i]
14
15
# 3. Apply divisor, add offset
16
out[x, y] = sum / divisor + offset
17
18
# 4. Clamp to valid pixel range
19
out[x, y] = clamp(out[x, y], 0, 255)
20
21
return out
🔲 Pixel iteration
Walk over every pixel of the source image (two nested loops over y and x). For every pixel you compute the new output value separately. That makes convolution trivially parallelisable — on a GPU every pixel runs at once.
# For every pixel in the image
for y in 0..image.height:
for x in 0..image.width:
🔲 Iteration
Loop over every pixel (x, y).
🔍 Gather patch
Collect 3×3 neighbourhood with replicate padding.
✖️ Convolve
Patch · kernel element-wise, sum.
📐 Normalise
Divide by divisor, add offset.
🎚️ Clamp
Snap to [0, 255].
✅ Output
Pixel done — next position.
Test your understanding
What does an edge-detection kernel like Sobel-X fundamentally do?
1. What does an edge-detection kernel like Sobel-X fundamentally do?
- ☐ A) It replaces every pixel by the average of its neighbours.
- ☐ B) It compares brightness on the left and right of the pixel — big differences yield high values, uniform areas yield zero.
- ☐ C) It multiplies the whole image by a random number.
- ☐ D) It copies the source image unchanged into the result.
2. Why are blur kernels (box blur, Gaussian blur) usually normalised so that the kernel sum is 1?
- ☐ A) So the algorithm runs faster.
- ☐ B) Because images only allow values between 0 and 9.
- ☐ C) To preserve brightness — without normalisation a box-blur kernel would make the image nine times as bright and blow out the highlights.
- ☐ D) Because kernels that sum to 1 always behave like Identity.
3. What is the key difference between a classical image filter like the ones here and a Convolutional Neural Network?
- ☐ A) CNNs are slower because they look at more pixels.
- ☐ B) There is no difference — CNN is just a marketing term for image filters.
- ☐ C) In a classical filter a human sets the kernel values. A CNN learns the kernel values automatically from training data.
- ☐ D) CNNs don't use convolution but matrix multiplication.
4. What happens when a CNN stacks multiple convolutional layers in sequence (stacking)?
- ☐ A) The network gets slower but the result stays the same as with one layer.
- ☐ B) Each extra layer halves the image resolution.
- ☐ C) Early layers detect simple features (edges, corners), later layers combine those into more complex features (textures, eyes, whole faces) — hierarchical representation.
- ☐ D) Stacking only works when every layer uses the same kernel.
Related Content
Article
Computer Vision (CNNs): How Machines Learned to See
How machines learned to read images and could suddenly tell dogs from cats — most of the time.
Tensors: The Language of GPUs
Tensors: the term that sounds more intimidating than the thing actually is.
The Spark: Activation Functions
The one small kink in the line without which neural networks wouldn't learn anything at all.
Data Tables & Transformations (Matrices)
The 2D grid of numbers where every ML library primarily thinks.
The Artificial Neuron
Frank Rosenblatt's 1958 idea that suddenly became relevant again more than 50 years later.
Spaces and Directions (Vectors)
Why AI constantly works with arrows in n-dimensional space.