Image convolution

CONVOLUTION

Image kernels

Choose a filter and select a pixel. The kernel multiplies nine nearby values by its weights and adds the results. Padding supplies values outside the image; dilation spreads the nine sampling points farther apart.

01 / INPUT
02 / KERNEL
-1-1-1-18-1-1-1-1

3 × 3 · stride 1 · dilation 1
zero padding

03 / OUTPUT

Pixel [4, 5] → input 1.00 → weighted sum 0.00

Select an input pixel or use the arrow keys to inspect its output. Blue = positive, amber = negative; display scale ±4.60.

Inspect the nine contributions
OffsetInputWeightProduct
-1, -11.000-1.000-1.000
-1, 01.000-1.000-1.000
-1, 11.000-1.000-1.000
0, -11.000-1.000-1.000
0, 01.0008.0008.000
0, 11.000-1.000-1.000
1, -11.000-1.000-1.000
1, 01.000-1.000-1.000
1, 11.000-1.000-1.000
FREQUENCY RESPONSE / HORIZONTAL AXIS0 · DCπ · Nyquistgain 0–12.0

This curve shows how strongly the filter responds to horizontal patterns, from a constant shade on the left to alternating pixels on the right. It assumes an unlimited grid, so padding does not change it. Try dilation to see what wider sample spacing does.

How it works4 min read

An image kernel is a small linear operator. That sounds elementary until a border artifact, a clipped negative response or a downsampling decision changes what a later stage sees. This lab keeps the image small enough that every multiplication remains inspectable, then connects the same operator to its frequency response.

Start with the edge kernel and select a pixel near the rectangle. The output display preserves sign: blue is positive, amber is negative. Expand Inspect the nine contributions to see the exact sampled values, weights and products.

Be precise about the operation #

For the stored kernel KK and dilation dd, this implementation computes

Or,c=∑i=−11∑j=−11Ki+1,j+1Ir+di,c+dj.O_{r,c}=\sum_{i=-1}^{1}\sum_{j=-1}^{1}K_{i+1,j+1}I_{r+di,c+dj}.

This is cross-correlation. Mathematical convolution flips the kernel. The distinction disappears for the symmetric presets here but matters for asymmetric learned filters and directional derivatives. Stride remains one, so every input coordinate has a corresponding output coordinate.

The raw weighted sum can be negative or greater than one. Switching Output display to clamping changes only the visualization, not the arithmetic. Two quite different responses can become the same black pixel after clipping. Signed colors normalize to the current maximum, so compare numeric values when changing kernels.

Padding is a boundary condition #

Choose Constant field and the blur kernel. With zero padding, border pixels darken because part of their neighborhood is assumed to be zero. Reflect or circular padding preserves the constant field. Change to the edge kernel: a constant interior cancels, while zero padding invents an edge at the image boundary.

Reflection extends the image by mirroring it without repeating the edge sample. Circular padding wraps to the opposite boundary. These are different assumptions about unseen data, not interchangeable implementation details. Circular padding is natural for periodic signals; applying it to an ordinary photograph can connect unrelated edges.

The nine-term table samples through the selected boundary rule, including when dilation reaches beyond the image. That makes an off-by-one boundary convention observable instead of leaving it hidden behind a library default.

The same filter has a spectral description #

On an infinite grid, the two-dimensional frequency response is

H(ωx,ωy)=∑i,jKi,jexp⁡ ⁣(id(jωx+iωy)),H(\omega_x,\omega_y)=\sum_{i,j}K_{i,j}\exp\!\left(\mathrm{i}d(j\omega_x+i\omega_y)\right),

with offsets centered at zero. The plot shows its magnitude along the horizontal frequency axis with vertical frequency zero. It is a slice through the filter response, not a Fourier transform of the displayed rectangle.

Identity has unit gain at every frequency. Blur preserves the constant component and attenuates much of the high-frequency content. The edge kernel has zero DC gain because its coefficients sum to zero. Sharpening increases gain for selected higher frequencies, which also amplifies noise in those bands.

Changing padding does not change this infinite-grid curve. It changes the finite image’s boundary behavior. Confusing those two statements is a common way to overinterpret a frequency plot.

Dilation trades density for reach #

Increase dilation from one to two. The kernel still uses nine coefficients, but they now span five pixels per axis. The frequency response acquires additional repetitions: expanding spatial tap spacing does not give the same filter as a densely sampled larger kernel.

Try the checkerboard input. At particular dilation settings, taps repeatedly land on the same checkerboard phase. The response can change dramatically even though the nominal receptive field became larger. In a network, repeated dilation patterns can create gridding artifacts; mixing scales or inserting dense operations changes that behavior.

If the next stage subsamples, inspect the signal bandwidth before discarding positions. A wider receptive field does not guarantee anti-aliasing. The CNN lab extends this calculation to layer stacks, where stride, dilation, channel count and computational cost interact.

What belongs in a correctness check #

Identity should preserve every pixel. A normalized blur with periodic or reflective boundaries should preserve a constant field. A zero-sum kernel should reject that field away from boundary effects. Those invariants catch mistakes more reliably than a screenshot of a plausible edge map.

For larger implementations, check accumulator precision, channel layout and boundary conventions against a trusted reference before optimizing. A separable kernel can reduce arithmetic from a two-dimensional pass to two one-dimensional passes, but intermediate rounding and boundary treatment still need to agree with the intended operator.

Further reading #

Distill’s Computing Receptive Fields connects kernel support to network architecture. The SciPy ndimage boundary-mode documentation is a useful reminder that even familiar words such as “reflect” have precise conventions that must be checked.

← All labs