Convolution and filters
Meaning in images lives in neighborhoods of pixels. Convolution slides a small grid of weights across the image to detect patterns such as edges. Work through one by hand, apply classic filters to a real image, and learn how stride and padding set the output size.
In this lecture you will
- Compute a convolution by hand for one position
- Explain why a kernel acts as a pattern detector
- Recognize classic kernels such as blur, sharpen and edge detectors
- Calculate output sizes from input size, kernel size, stride and padding
A single pixel says almost nothing — is this bluish value part of the sky, a window or a car? Meaning comes from neighborhoods: an edge is a place where neighbors differ, a corner is where two edges meet, a texture is a repeating pattern of neighbors. The operation that reads neighborhoods, and the heart of modern computer vision, is convolution.
One convolution, by hand
A convolution uses a small grid of weights called a kernel or filter. Place it over a patch of the image, multiply each weight by the pixel under it, add up the nine products, and write the sum into the output. Then slide one pixel and repeat.
- An image and a kernelThe input is a 7 × 7 grayscale image: a bright square (9) on a dark background (1). The kernel subtracts the left column from the right column.
- The first positionPlace the kernel on the top-left patch. Each row contributes right − left; adding the three rows gives 8. That number is the first output value.
- Slide and repeatMove one pixel to the right and do the same arithmetic. Where the patch straddles the square’s left edge, the right column is much brighter and the output is large.
- Every positionSliding across all 25 valid positions fills the 5 × 5 output. The same nine weights are used everywhere.
- An edge mapPositive values mark the square’s left edge, negative values its right edge, and flat regions give 0. The kernel detected vertical edges.
Two properties make convolution powerful:
- Locality. Each output depends only on a small neighborhood, matching how visual structure works.
- Weight sharing. The same kernel is applied everywhere, so a pattern is detected wherever it appears — top-left or bottom-right — using only nine numbers.
Classic filters
Long before neural networks, image-processing engineers designed kernels by hand. Try them on the scene — or type your own weights into the grid.
Edge kernels respond strongly on the roof line, the tree trunk and the car’s outline, and stay near zero in the flat sky. Try a kernel whose weights sum to 1 (keeps brightness) and one whose weights sum to 0 (finds changes).
- Kernel (filter)
- A small grid of weights, typically 3 × 3 or 5 × 5, slid across the image.
- Feature map
- The output of applying one kernel to the whole image — a map of where its pattern appears.
- Blur
- Averages each neighborhood (weights sum to 1). Reduces noise and fine detail.
- Edge detector
- Weights sum to 0: flat regions cancel, changes in brightness stand out. Sobel kernels detect horizontal or vertical edges.
- Receptive field
- The region of the input that affects one output value — here, 3 × 3 pixels.
In the next lecture you will see the key idea of convolutional neural networks: instead of designing kernels by hand, the network learns them from data — and its first layer usually ends up with edge and color detectors much like these.
Stride, padding and output size
Two settings control how the kernel moves:
- Stride is how many pixels the kernel jumps between positions. Stride 2 skips every other position, roughly halving the output size.
- Padding adds a border of extra pixels (usually zeros) around the input, so the kernel can be centered on edge pixels too. A 3 × 3 kernel with padding 1 keeps the size unchanged — “same” padding.
The amber window visits every kernel position in turn; its twin on the right marks the output value that position produces.
import torch
from torch import nn
conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
x = torch.randn(1, 3, 224, 224) # one RGB image
print(conv(x).shape) # torch.Size([1, 16, 224, 224]) — same size, 16 feature maps
print(sum(p.numel() for p in conv.parameters())) # 3*3*3*16 + 16 = 448 learnable weights
Key takeaways
- A convolution multiplies a small kernel by each neighborhood of the image and sums the results.
- The same weights are reused at every position, so a kernel finds its pattern anywhere in the image.
- Different kernels detect different patterns — edges in different directions, blobs, texture — and blurring or sharpening are convolutions too.
- Output size = ⌊(n + 2p − k) / s⌋ + 1; padding preserves size, stride shrinks it.
Check your understanding
4 questions · answers explainedQ1A 3 × 3 kernel of all 1/9 is applied to an image. What does it do?
Q2Why is a kernel whose weights sum to 0 useful for edge detection?
Q3A 32 × 32 input is convolved with a 5 × 5 kernel, stride 1, no padding. What is the output size?
Q4Which setting halves the spatial size of the output?