Interactive lab · Introductory · 11 min read
Convolution explained: an interactive image filter lab
See exactly how a 3×3 kernel turns pixels into an output: every multiply-add, the flip that separates convolution from cross-correlation, padding, stride and channels, with exercises and answers for class.
anastig editorial teamPublished
At a glance for instructors
- Suggested class time
- 10 to 15 minutes
- Level
- Introductory undergraduate (image processing, computer vision or an introduction to deep learning)
- Prerequisites
- Multiplying and adding small integers; A grayscale image as a grid of numbers
Learning objectives
- Compute one output of a 3×3 cross-correlation by hand and check it against the lab's multiply-add trace.
- Predict the sign and size of an edge filter's response on flat regions and on edges, and explain why flipping the kernel (true convolution) reverses the sign.
- Choose a padding rule and stride and calculate the output size with ⌊(n + 2p − k)/s⌋ + 1.
Free to use in class without an account. Jump to the teaching notes and worksheet.
A convolution filter slides a small grid of weights, the kernel, across an image. At every position it multiplies each weight by the pixel underneath and adds the products into one output value. That single operation blurs, sharpens and finds edges, and stacked in many layers with learned weights it is the core of a convolutional neural network (CNN). Use the lab below to change the pixels, the kernel, the padding and the stride, predict an output, then reveal it and inspect each of the nine products that make it up.
Static version — the interactive lab loads with JavaScript
Worked example (static version). The input is a 4×6 grayscale patch with a vertical edge: every row is 0 0 0 9 9 9. The kernel is Sobel x:
| column 1 | column 2 | column 3 | |
|---|---|---|---|
| row 1 | −1 | 0 | 1 |
| row 2 | −2 | 0 | 2 |
| row 3 | −1 | 0 | 1 |
Place the 3×3 window over rows 1–3 and columns 2–4 of the input. It sees 0 0 9 in every row. Multiply position by position and add:
(0×−1 + 0×0 + 9×1) + (0×−2 + 0×0 + 9×2) + (0×−1 + 0×0 + 9×1) = 9 + 18 + 9 = 36.
Sliding the window over all valid positions gives the 2×4 output 0 36 36 0 in both rows: zero on the flat regions, +36 wherever the window straddles the dark-to-bright step.
What the lab computes
Treat a grayscale image as a grid of numbers , with row and column . A 3×3 kernel has offsets around its centre. The filtered value centred on pixel is
This is cross-correlation: the kernel is laid over the image exactly as written. In mathematics and signal processing, convolution is defined with the image index reversed:
Substituting and shows that convolution is cross-correlation with the kernel rotated by 180 degrees. For a general kernel the offsets run from to with .
The flip is what makes convolution commutative, ; cross-correlation is not [1]. Both operations are linear and the same everywhere in the image, so a chain of filters collapses into one combined filter (exactly so at stride 1, away from the borders). We checked numerically that correlating with kernel and then equals a single correlation with .
Why flipping changes the sign of an edge response
Rotate Sobel x by 180 degrees and every non-zero weight changes sign: the negative column ends up on the right and the positive column on the left. Applied to the same dark-to-bright edge, true convolution gives −36 where cross-correlation gives +36. This happens for any kernel whose 180-degree rotation is its own negative, that is, antisymmetric about its centre, like Sobel x and Sobel y. A kernel that is only antisymmetric left to right is not enough: 1 0 −1 / 0 0 0 / 2 0 −2 changes under the flip into something other than its negative.
Symmetric kernels such as the box blur and the Laplacian are unchanged by the flip, so for them the two operations give identical results.
Worked example: a Sobel response, term by term
The nine products for the window over columns 2 to 4 of the edge example are:
| input pixel | weight | product |
|---|---|---|
| 0 (row 1, col 2) | −1 | 0 |
| 0 (row 1, col 3) | 0 | 0 |
| 9 (row 1, col 4) | 1 | 9 |
| 0 (row 2, col 2) | −2 | 0 |
| 0 (row 2, col 3) | 0 | 0 |
| 9 (row 2, col 4) | 2 | 18 |
| 0 (row 3, col 2) | −1 | 0 |
| 0 (row 3, col 3) | 0 | 0 |
| 9 (row 3, col 4) | 1 | 9 |
The sum is 36. In the lab, select output row 1, column 2, reveal the outputs and focus each term in the trace: the lab outlines the input pixel and the kernel weight that the term pairs. The middle column of Sobel x is zero and its outer columns have opposite signs. So the filter measures how much brighter the right side of the window is than the left, with double weight on the centre row. That is why its weights add up to zero; what that does to a flat region is the lab's first challenge (answer in the worksheet, question 7).
Common kernels and what they respond to
Every kernel below was applied with cross-correlation to two windows taken from the edge example: one covering columns with values 0 0 9 and one covering 0 9 9. What each kernel does to a flat patch follows from the sum of its weights; the values are in the worksheet answers.
| Kernel | Weights (rows) | Sum of weights | Window 0 0 9 | Window 0 9 9 | What it is for |
|---|---|---|---|---|---|
| Identity | 0 0 0 / 0 1 0 / 0 0 0 | 1 | 0 | 9 | Checking the pipeline: output equals input |
| Box blur | all 1, × 1/9 | 1 | 3 | 6 | Smoothing noise; the edge becomes a ramp |
| Sharpen | 0 −1 0 / −1 5 −1 / 0 −1 0 | 1 | −9 | 18 | Exaggerating edges; overshoots past 0 and 9 |
| Laplacian | 0 1 0 / 1 −4 1 / 0 1 0 | 0 | 9 | −9 | Locating edges as a sign change (zero crossing) |
| Sobel x | −1 0 1 / −2 0 2 / −1 0 1 | 0 | 36 | 36 | Vertical edges, with direction as the sign |
| Sobel y | −1 −2 −1 / 0 0 0 / 1 2 1 | 0 | 0 | 0 | Horizontal edges only; blind to this vertical edge |
Two patterns are worth remembering. A kernel whose weights add up to 1 preserves the brightness of flat regions; a kernel whose weights add up to 0 responds only to change (work out what it gives on a flat region before you look at the answers). The sharpen result shows the other side of the coin: −9 and 18 lie outside the input range of 0 to 9. Those overshoots are the dark and bright halos you see around over-sharpened edges, and a real pipeline must clip or rescale them back into the valid range.
Padding: what happens at the border
Near the border, part of the window falls outside the image. You can skip those positions ("valid" filtering, padding 0) and accept a smaller output, or invent the missing pixels by padding. The four common rules, with the names PyTorch uses, are:
| Padding rule | Invented pixels for a row a b c d | 3×3 box blur, top-left corner of a constant patch of 5s | When to use it |
|---|---|---|---|
| Zeros | 0 0 · a b c d · 0 0 | darker than 5 (the lab's zero-padding challenge; value in the worksheet answers) | CNN layers, where the network learns to handle it |
| Replicate | a a · a b c d · d d | 5 | Image filtering where borders should stay neutral |
| Reflect | c b · a b c d · c b | 5 | Smooth continuation without repeating the edge pixel |
| Circular | c d · a b c d · a b | 5 | Periodic signals only; joins opposite borders |
Zero padding is not neutral. It surrounds the image with black, so a blur darkens the border and an edge filter fires along it even when the image is perfectly flat. Load the "zero padding" challenge in the lab and watch the corner value.
Stride and output size
The stride is how far the window moves between outputs. Stride 2 skips every other position and roughly halves each dimension. For an input of size , kernel size , padding and stride , each output dimension is
The floor follows Dumoulin and Visin's convolution arithmetic [7]; the CS231n notes give the same expression and treat a non-integer result as a configuration where the neurons do not "fit" [8]. Examples: a 7×7 input with a 3×3 kernel gives 5×5 with no padding, 7×7 with padding 1 (often called "same" padding, for odd and stride 1), and 4×4 with padding 1 and stride 2. When the division is not exact, the last rows or columns are never covered by any window and are simply ignored. In the lab, try stride 3 with no padding: column 7 of the input never enters a window.
Colour images and CNN layers: channels
A colour image has three channels: red, green and blue. A filter for a multi-channel input has one 3×3 slice per input channel. It multiplies each slice with its own channel and adds everything, across all channels, into a single number. With input channels, bias and one output channel:
A CNN layer applies such filters side by side, and each one produces one output channel (a feature map). The layer therefore has parameters. A 3×3 layer from 64 to 128 channels has .
The smallest useful multi-channel example is a 1×1 filter. With weights 0.299, 0.587 and 0.114 on red, green and blue, it computes the luma of ITU-R BT.601 [9], which is defined on gamma-encoded values and is commonly used for grayscale conversion. A pixel with red 200, green 100 and blue 50 becomes . A 1×1 convolution mixes channels without looking at any neighbours; CNNs use it to change the number of channels cheaply.
Common misconceptions
- "Convolution in a CNN flips the kernel." In most frameworks it does not (see the callout above).
- "Padding with zeros leaves the image unchanged." It adds a black frame that filters react to: darker blurred borders and false edges.
- "The output always has the same size as the input." Only with padding and stride 1. Otherwise use the formula above.
- "A bigger response means a brighter image region." Edge kernels respond to change, and their sign gives the direction. A flat white wall and a flat black wall both give 0.
Worksheet
Use these with or without the lab. The printed page includes the static example above.
- A 3×3 patch has every row equal to
2 4 6. Compute its Sobel x response with cross-correlation, then with convolution. - Compute the Laplacian (
0 1 0 / 1 −4 1 / 0 1 0) response at the centre of the patch1 1 1 / 1 5 1 / 1 1 1. Is the centre a bright spot or a dark spot? - An input is 32×32. A 5×5 kernel is applied with padding 2 and stride 2. What is the output size?
- A constant image of 8s is filtered with a 3×3 box blur and zero padding 1. What is the value in a corner? What is it with replicate padding?
- How many parameters, including biases, does a 3×3 convolution layer from 64 to 128 channels have?
- A CNN was trained in a framework that computes cross-correlation. (a) Would the same architecture trained from scratch in a framework that computes true convolution be any less capable? (b) What must happen to the trained weights if you load them into that other framework?
- A constant patch where every pixel is 5 is filtered with Sobel x and no padding. What is any output value?
- Take the vertical-edge example (every row
0 0 0 9 9 9) and apply true convolution with Sobel x, which flips the kernel first. What is the output where the window covers columns 2 to 4? - A single pixel of value 1 sits in the middle of a field of zeros. It is filtered with cross-correlation and the kernel numbered 1 to 9 (top row
1 2 3). Which number lands up and to the left of the bright pixel?
Show the answers
- Cross-correlation: (6 − 2) × 1 + (6 − 2) × 2 + (6 − 2) × 1 = 16. Convolution rotates this point-antisymmetric kernel into its negative, giving −16.
- 1 + 1 + 1 + 1 − 4 × 5 = −16. With this sign convention a strongly negative Laplacian marks a bright spot on a darker background.
- ⌊(32 + 4 − 5) / 2⌋ + 1 = ⌊15.5⌋ + 1 = 16, so the output is 16×16.
- Zero padding: 4 of the 9 window pixels are inside the image, so 4 × 8 / 9 ≈ 3.56. Replicate padding: 8.
- 128 × (9 × 64 + 1) = 73,856.
- (a) No. Every cross-correlation kernel has an equivalent convolution kernel (its 180-degree rotation), so both can represent the same functions; training will find some set of weights that fits, not necessarily the rotated copies. (b) Rotate every spatial kernel by 180 degrees (flip both spatial axes) when converting; otherwise the outputs change.
- 0: Sobel x weights add up to zero. On a flat patch every kernel gives (sum of weights) × the patch value, so in the kernel table the identity, box blur and sharpen give 5 on a patch of 5s, and the Laplacian, Sobel x and Sobel y give 0. In the padding table, zero padding gives 4 × 5 / 9 ≈ 2.22 at the corner (the lab's zero-padding challenge). 8. −36: the flip turns Sobel x into its negative. 9. 9: cross-correlation copies the kernel rotated by 180 degrees, so the top-left neighbour receives the bottom-right weight.
Teaching notes
Suggested sequence
- 2 min: the lab opens with outputs hidden. Ask the class to predict the selected output (row 1, column 2) for the vertical edge and Sobel x, then press Reveal outputs.
- 3 min: step through the nine terms in the trace; each term outlines its input pixel and kernel weight. Point out that the middle column of the kernel is zero.
- 3 min: load challenge 3 (true convolution on the same edge); its value stays hidden until the class checks a prediction. Ask why the sign changes. Then load the impulse challenge (answers stay hidden until checked) to show the 180-degree rotation.
- 3 min: switch padding to 1 with zeros on the constant patch and box blur; compare the corner with replicate padding.
- 2 to 4 min: change the stride to 2 and predict the output size with the formula; check it with challenge 6 (the size stays hidden until then), then hand out the printed worksheet.
Discussion questions
- Why does every edge kernel in the table have weights that add up to zero?
- Which kernels give the same result under convolution and cross-correlation, and why?
- When would zero padding be a reasonable choice, and when would it mislead you?
- A 3×3 layer maps 3 channels to 16 channels. How many weights does it have, and what does each output channel represent? (Answer: 3 × 3 × 3 × 16 = 432 weights plus 16 biases; each output channel is one learned filter's response map.)
Print this page for a worksheet: the printed version shows the static exercise, tables and answers.
References
- [1] I. Goodfellow, Y. Bengio and A. Courville. Deep Learning, chapter 9: Convolutional Networks. MIT Press, 2016. www.deeplearningbook.org/contents/convnets.html
- [2] PyTorch 2.11 documentation: torch.nn.Conv2d. Accessed 2026-11-28. docs.pytorch.org/docs/2.11/generated/torch.nn.Conv2d.html
- [3] OpenCV 4.x documentation: Image Filtering (filter2D, border types). Accessed 2026-11-28. docs.opencv.org/4.x/d4/d86/group__imgproc__filter.html
- [4] SciPy documentation: scipy.signal.convolve2d. Accessed 2026-11-28. docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html
- [5] NumPy documentation: numpy.pad (modes reflect and symmetric). Accessed 2026-11-28. numpy.org/doc/stable/reference/generated/numpy.pad.html
- [6] SciPy documentation: Multidimensional image processing (scipy.ndimage), boundary modes. Accessed 2026-11-28. docs.scipy.org/doc/scipy/tutorial/ndimage.html
- [7] V. Dumoulin and F. Visin. A guide to convolution arithmetic for deep learning. arXiv:1603.07285, 2016. arxiv.org/abs/1603.07285
- [8] Stanford CS231n course notes: Convolutional Neural Networks for Visual Recognition, Convolutional Networks. cs231n.github.io/convolutional-networks/
- [9] ITU-R Recommendation BT.601: Studio encoding parameters of digital television for standard 4:3 and wide-screen 16:9 aspect ratios. www.itu.int/rec/R-REC-BT.601