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 1column 2column 3
row 1−101
row 2−202
row 3−101

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.

A 4 by 6 input with a vertical edge, a highlighted 3 by 3 window over columns 2 to 4, the Sobel x kernel, the nine products 0, 0, 9, 0, 0, 18, 0, 0, 9 and the output grid 0 36 36 0 with 36 highlighted.
DiagramThe same calculation as a picture. The window, the products and the output position are highlighted.

What the lab computes

Treat a grayscale image as a grid of numbers f[i,j]f[i, j], with row ii and column jj. A 3×3 kernel w[a,b]w[a, b] has offsets a,b∈{−1,0,1}a, b \in \{-1, 0, 1\} around its centre. The filtered value centred on pixel (i,j)(i, j) is

g[i,j]  =  ∑a=−11∑b=−11w[a,b] f[i+a, j+b]g[i, j] \;=\; \sum_{a=-1}^{1} \sum_{b=-1}^{1} w[a, b]\, f[i + a,\, j + b]

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:

(f∗w)[i,j]  =  ∑a=−11∑b=−11w[a,b] f[i−a, j−b](f * w)[i, j] \;=\; \sum_{a=-1}^{1} \sum_{b=-1}^{1} w[a, b]\, f[i - a,\, j - b]

Substituting a→−aa \to -a and b→−bb \to -b shows that convolution is cross-correlation with the kernel rotated by 180 degrees. For a general k×kk \times k kernel the offsets run from −r-r to rr with k=2r+1k = 2r + 1.

The flip is what makes convolution commutative, f∗w=w∗ff * w = w * f; 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 a1a_1 and then a2a_2 equals a single correlation with a1∗a2a_1 * a_2.

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.

The kernel 1 to 9, the cross-correlation output around a single bright pixel showing 9 8 7 / 6 5 4 / 3 2 1, and the convolution output showing 1 2 3 / 4 5 6 / 7 8 9.
DiagramFiltering a single bright pixel copies the kernel into the output. This is the impulse response, and it is the quickest way to see which operation a library actually computes.

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 pixelweightproduct
0 (row 1, col 2)−10
0 (row 1, col 3)00
9 (row 1, col 4)19
0 (row 2, col 2)−20
0 (row 2, col 3)00
9 (row 2, col 4)218
0 (row 3, col 2)−10
0 (row 3, col 3)00
9 (row 3, col 4)19

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.

KernelWeights (rows)Sum of weightsWindow 0 0 9Window 0 9 9What it is for
Identity0 0 0 / 0 1 0 / 0 0 0109Checking the pipeline: output equals input
Box blurall 1, × 1/9136Smoothing noise; the edge becomes a ramp
Sharpen0 −1 0 / −1 5 −1 / 0 −1 01−918Exaggerating edges; overshoots past 0 and 9
Laplacian0 1 0 / 1 −4 1 / 0 1 009−9Locating edges as a sign change (zero crossing)
Sobel x−1 0 1 / −2 0 2 / −1 0 103636Vertical edges, with direction as the sign
Sobel y−1 −2 −1 / 0 0 0 / 1 2 1000Horizontal 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 ruleInvented pixels for a row a b c d3×3 box blur, top-left corner of a constant patch of 5sWhen to use it
Zeros0 0 · a b c d · 0 0darker than 5 (the lab's zero-padding challenge; value in the worksheet answers)CNN layers, where the network learns to handle it
Replicatea a · a b c d · d d5Image filtering where borders should stay neutral
Reflectc b · a b c d · c b5Smooth continuation without repeating the edge pixel
Circularc d · a b c d · a b5Periodic 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 nn, kernel size kk, padding pp and stride ss, each output dimension is

nout  =  ⌊n+2p−ks⌋+1n_{\text{out}} \;=\; \left\lfloor \frac{n + 2p - k}{s} \right\rfloor + 1

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, p=(k−1)/2p = (k - 1)/2 for odd kk 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 CinC_\text{in} input channels, bias β\beta and one output channel:

g[i,j]  =  β+∑c=1Cin∑a=−rr∑b=−rrwc[a,b] fc[i+a, j+b]g[i, j] \;=\; \beta + \sum_{c=1}^{C_\text{in}} \sum_{a=-r}^{r} \sum_{b=-r}^{r} w_c[a, b]\, f_c[i + a,\, j + b]

A CNN layer applies CoutC_\text{out} such filters side by side, and each one produces one output channel (a feature map). The layer therefore has Cout×(k2Cin+1)C_\text{out} \times (k^2 C_\text{in} + 1) parameters. A 3×3 layer from 64 to 128 channels has 128×(9×64+1)=73,856128 \times (9 \times 64 + 1) = 73{,}856.

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 0.299×200+0.587×100+0.114×50=124.20.299 \times 200 + 0.587 \times 100 + 0.114 \times 50 = 124.2. 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 (k−1)/2(k-1)/2 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.

  1. A 3×3 patch has every row equal to 2 4 6. Compute its Sobel x response with cross-correlation, then with convolution.
  2. Compute the Laplacian (0 1 0 / 1 −4 1 / 0 1 0) response at the centre of the patch 1 1 1 / 1 5 1 / 1 1 1. Is the centre a bright spot or a dark spot?
  3. An input is 32×32. A 5×5 kernel is applied with padding 2 and stride 2. What is the output size?
  4. 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?
  5. How many parameters, including biases, does a 3×3 convolution layer from 64 to 128 channels have?
  6. 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?
  7. A constant patch where every pixel is 5 is filtered with Sobel x and no padding. What is any output value?
  8. 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?
  9. 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
  1. Cross-correlation: (6 − 2) × 1 + (6 − 2) × 2 + (6 − 2) × 1 = 16. Convolution rotates this point-antisymmetric kernel into its negative, giving −16.
  2. 1 + 1 + 1 + 1 − 4 × 5 = −16. With this sign convention a strongly negative Laplacian marks a bright spot on a darker background.
  3. ⌊(32 + 4 − 5) / 2⌋ + 1 = ⌊15.5⌋ + 1 = 16, so the output is 16×16.
  4. Zero padding: 4 of the 9 window pixels are inside the image, so 4 × 8 / 9 ≈ 3.56. Replicate padding: 8.
  5. 128 × (9 × 64 + 1) = 73,856.
  6. (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.
  7. 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

  1. 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.
  2. 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. 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.
  4. 3 min: switch padding to 1 with zeros on the constant patch and box blur; compare the corner with replicate padding.
  5. 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

  1. Why does every edge kernel in the table have weights that add up to zero?
  2. Which kernels give the same result under convolution and cross-correlation, and why?
  3. When would zero padding be a reasonable choice, and when would it mislead you?
  4. 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. [1] I. Goodfellow, Y. Bengio and A. Courville. Deep Learning, chapter 9: Convolutional Networks. MIT Press, 2016. www.deeplearningbook.org/contents/convnets.html
  2. [2] PyTorch 2.11 documentation: torch.nn.Conv2d. Accessed 2026-11-28. docs.pytorch.org/docs/2.11/generated/torch.nn.Conv2d.html
  3. [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. [4] SciPy documentation: scipy.signal.convolve2d. Accessed 2026-11-28. docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html
  5. [5] NumPy documentation: numpy.pad (modes reflect and symmetric). Accessed 2026-11-28. numpy.org/doc/stable/reference/generated/numpy.pad.html
  6. [6] SciPy documentation: Multidimensional image processing (scipy.ndimage), boundary modes. Accessed 2026-11-28. docs.scipy.org/doc/scipy/tutorial/ndimage.html
  7. [7] V. Dumoulin and F. Visin. A guide to convolution arithmetic for deep learning. arXiv:1603.07285, 2016. arxiv.org/abs/1603.07285
  8. [8] Stanford CS231n course notes: Convolutional Neural Networks for Visual Recognition, Convolutional Networks. cs231n.github.io/convolutional-networks/
  9. [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

Where to go next

How to cite: anastig editorial team (2026). Convolution explained: an interactive image filter lab. anastig Learn. https://anastig.ai/learn/convolution-explained-image-filter-lab

How this resource was prepared and checked: editorial standards. Found an error? Tell us.

Convolution Explained: An Interactive Image Filter Lab | anastig