Open the image-convolution demo →
An image filter can be built from a small matrix of weights. At each output pixel, those weights are multiplied by a neighborhood of input pixels and the products are added together.
The demo uses a kernel, a stride of one pixel, and zero padding. Selecting a pixel shows its original value and the computed weighted sum.
The calculation #
For an input image and a kernel , the operation used here is
Pixels outside the input image are treated as zero. This is cross-correlation: the kernel is applied in its stored orientation. Mathematical convolution flips the kernel first. Most CNN libraries use the cross-correlation convention while calling the operation convolution.
Choosing a kernel #
Identity keeps only the center pixel, so the output matches the input. It is a useful check that the selected coordinate and readout are aligned.
Blur assigns a weight of to each of the nine positions. It averages a neighborhood, smoothing sharp changes. Zero padding makes the average darker near an image boundary.
Edges compares a pixel with its neighbors. In a uniform region, the contributions cancel. Near the bright rectangle’s boundary, that cancellation breaks and the response becomes visible.
Sharpen reinforces the center relative to its neighbors. It increases local contrast, which can also amplify noise in a real image.
Reading the result #
The output colors are clamped to the interval for display. The numeric readout preserves the raw weighted sum, including negative values and values above one. A dark output pixel can therefore represent either a zero response or a negative response; use the readout to distinguish them.
Click an input pixel or use the arrow keys to move the selection. Compare a pixel well inside the rectangle with one on its boundary, then switch kernels without moving the selection.
From image filters to CNNs #
The kernels in this demo are fixed. A convolutional neural network learns its kernel weights from data. It also combines multiple input channels, adds biases, and applies nonlinearities between layers. The local CNN Explainer exposes those extra steps, and its companion write-up explains them.