close
Skip to content
ML Visualization

Convolution

Deep LearningIntermediate~7 min

Convolution — Convolution slides a small grid of weights (a kernel) across an input, computing a weighted sum at every position. The same kernel is reused everywhere, so it detects one local pattern — an edge, a blob, a texture — wherever it appears in the image.

A convolution is a tiny stencil that slides across an image, and at every stop it multiplies-and-adds the pixels underneath it. Reuse one small kernel across the whole picture and you get a feature detector that finds its pattern anywhere — the core operation that makes CNNs see.

Input 9×9

Output 7×7 (click a cell)

Under the window at (0, 0)

1 × 0= 00 × 0= 0-1 × 0= 02 × 0= 00 × 0= 0-2 × 0= 01 × 0= 00 × 0= 0-1 × 0= 0

Convolution controls

Data
Input image

A filled disc with a sharp rim — edges all the way round, in every orientation.

Model
Kernel
Kernel weights — edit any of them

Weights sum to 0 — sum 1 preserves brightness, sum 0 keeps only change.

0.00
1
0

⌊(9 + 2·0 − 3) / 1⌋ + 1 = 7, so the feature map is 7×7.

Playback
Step 0 / 146
Speed
  1. Place the window
  2. Multiply
  3. Sum + bias

Step 0 of 146 — output cell (0, 0) reads input rows 0–2, cols 0–2

Break it

The idea in plain words

A convolution is a tiny stencil — a small grid of weights called a kernel — that slides across an image. At every stop it multiplies the pixels underneath it by its weights and adds them up, writing one number into an output grid called a feature map.

The trick is that the same kernel is reused at every position. So a kernel that responds to a vertical edge finds vertical edges anywhere in the picture — this reuse is called weight sharing, and it’s what makes convolutional networks efficient. Swap the kernel and the same machinery becomes an edge detector, a sharpener, or a blur.

Now, the math

Each output pixel is the weighted sum of a small window of the input (cross-correlation, the operation CNNs actually use):

yr,c=ijKi,jxr+i,c+j+by_{r,c} = \sum_{i}\sum_{j} K_{i,j}\, x_{r+i,\,c+j} + b
xx
the input image (or previous feature map).
Ki,jK_{i,j}
the kernel weights — the same grid reused at every position.
yr,cy_{r,c}
one pixel of the output feature map.
bb
a bias added to every position.
▸ Show the derivation

With a k×kk\times k kernel and no padding, an H×WH\times W input produces an (Hk+1)×(Wk+1)(H-k+1)\times(W-k+1) output — the kernel can’t hang off the edge, so the map shrinks by k1k-1. Adding a border of zeros (padding) keeps the size the same; taking bigger steps (stride) shrinks it faster. Because every output reuses one small set of weights, a convolution has far fewer parameters than a fully-connected layer over the same pixels.

Trace it by hand

A 4×4 input patch holding a bright 2×2 blob (1 = bright, 0 = dark) convolved with the interactive’s Edge (Sobel) kernel preset. Every value is exact integer arithmetic.

Step 1 — the input patch and the kernel

Input x (4×4)
0000
0110
0110
0000
Kernel K (3×3, Edge/Sobel)
10-1
20-2
10-1

Positive weights on the left column, negative on the right: this kernel responds to horizontal changes in brightness — vertical edges.

Step 2 — one output cell, term by term

Place the kernel over the top-left 3×3 window and multiply each weight by the pixel under it (reading row by row):

y0,0=10+00+(1)0  +  20+01+(2)1  +  10+01+(1)1=3y_{0,0} = 1{\cdot}0 + 0{\cdot}0 + (-1){\cdot}0 \;+\; 2{\cdot}0 + 0{\cdot}1 + (-2){\cdot}1 \;+\; 1{\cdot}0 + 0{\cdot}1 + (-1){\cdot}1 = -3

Only three pixels in the window are bright, and all sit under negative weights — nine multiplies collapse into one number that says “brightness increases to the right here.”

Step 3 — slide the same kernel to fill the map

(43+1)×(43+1)=2×2(4 - 3 + 1) \times (4 - 3 + 1) = 2 \times 2
Output feature map y (2×2)
-33
-33

The same nine weights slid one pixel at a time produce all four cells: −3 along the blob’s left edge, +3 along its right edge — opposite signs for opposite edge directions, and 0 anywhere flat.

What just happened: one 3×3 stencil of 9 shared weights scanned the patch and rewrote it as an edge report. That reuse is why the kernel finds its pattern anywhere in the image — and a convolutional network simply learns these 9 numbers instead of hand-picking them.

Now Break It

Try this: A blur kernel on a sharp edge washes out the very structure the next layer needs.

Control: Kernel preset (switch to blur)

What happens: Wrong kernel! A blur smooths away the edges — the feature the next layer was counting on is gone.

Where convolution is used

Convolution is the workhorse behind almost everything computers do with images. Photo apps use learned kernels to sharpen, denoise, and blur; medical-imaging systems convolve scans to highlight tumors and fractures; self-driving cars run convolutions over camera frames to find lane lines and pedestrians. The same operation powers document scanning (detecting text edges), satellite analysis, and the first layers of every convolutional network that classifies or generates images.

A common misconception is that a convolution “looks at the whole image.” It does not — each output pixel only sees a tiny local window (the receptive field). Global understanding is built up by stacking convolutions, so deeper layers indirectly see more of the image. Another trap is confusing convolution with the fully-connected layers of an MLP: because a kernel reuses the same weights everywhere, it has far fewer parameters and — crucially — detects its pattern anywhere it appears, which a dense layer cannot do. Finally, note the sign convention: deep-learning “convolution” is really cross-correlation (no kernel flip), which is why the interactive slides the kernel without mirroring it.

Frequently asked questions

What is a kernel (or filter) in convolution?
A kernel is a small grid of weights — often 3×3 — that slides across the input. At each position it multiplies the pixels underneath it by its weights and sums them into one output value. The same kernel is reused at every position, so it acts as a detector for one specific local pattern, such as a vertical edge, wherever that pattern appears.
What is the difference between convolution and cross-correlation?
True mathematical convolution flips the kernel before sliding it; cross-correlation does not. Deep-learning frameworks actually implement cross-correlation but call it convolution. Since the kernel weights are learned, the flip makes no practical difference to what the network can represent, so the distinction rarely matters in practice.
What do padding and stride do?
Padding adds a border of zeros around the input so the kernel can sit over edge pixels; with the right padding the output stays the same size as the input instead of shrinking. Stride is how far the kernel jumps between positions — a stride of 2 skips every other position, halving the output size and reducing computation.
Why does the output feature map get smaller than the input?
Without padding, the kernel cannot hang off the edge of the image, so its center can only visit interior positions. A k×k kernel on an H×W input yields an (H−k+1)×(W−k+1) output — it shrinks by k−1 in each dimension. Padding with zeros restores the original size.
Why use convolution instead of a fully-connected layer for images?
Weight sharing. A convolution reuses one small set of weights across the whole image, so it has far fewer parameters than a dense layer wiring every pixel to every neuron, and it detects a feature regardless of where it appears. This makes it both efficient and translation-tolerant — properties a fully-connected layer lacks.

Written & reviewed by the ML Visualization team · Last updated .