Correlation
The filter , also called the kernel, has its origin in the center, so its offsets and run from to for a filter. Placing it over pixel of the image and adding up the products of weights and pixel values is called correlation:
The same weights are used everywhere, so the filter is linear (filtering with gives the sum of the two results) and shift invariant (filtering a shifted image gives the shifted result). Every operation with these two properties can be written this way.
Convolution
Convolution uses the filter flipped horizontally and vertically, i.e. rotated by 180°:
For symmetric filters such as the box or the Gaussian both are identical. For asymmetric ones they are not: a filter with a single 1 at moves the image by 2 pixels left and 1 up under correlation, and right and down under convolution. The flip gives convolution its useful properties:
- Commutative: , so there is no conceptual difference between filter and signal.
- Associative: , so several filters in a row, , can be combined into one filter . Correlation is not associative.
- Distributes over addition, , and scalars factor out, .
- Identity: the unit impulse gives .
Convolving an image with a single bright pixel produces a copy of the filter, which is why is also called the impulse response.
Common filters
- Box filter (mean filter): all weights are , so the result is the plain average of the neighborhood, .
- Gaussian: weights that fall off with the distance from the center; a smoother blur than the box. The 5 × 5 preset uses binomial weights in each direction.
- Sharpen: the image plus four times the difference between each pixel and the average of its four neighbors, which boosts edges and fine detail.
- Sobel: central differences in one direction combined with smoothing in the other, Their weights sum to 0, so flat areas give 0 and the output has positive and negative values. They are often scaled by to give the slope per pixel.
Filters that average have weights summing to 1, so the overall brightness of the image is preserved.
Boundary handling
Near the edge, part of the filter lies outside the image, and some rule has to supply those values. The left view shows the image extended by that rule:
- Clip filter (black): pixels outside are 0. Averaging filters darken the border.
- Wrap around: the image repeats periodically, so the opposite edge leaks in.
- Copy edge: the edge pixels are replicated outward.
- Reflect: the image is mirrored across the edge.
Applying a filter times acts like a single, times wider filter (associativity), so the border effects creep further into the image with every pass.
Try this
- Select the shift filter, set “apply” to 20 and switch between correlation and convolution: the image moves in opposite directions.
- With the shift filter, compare the border modes. Clip filter pads with black, wrap around brings in the opposite side of the image, copy edge smears the edge pixels.
- Apply the 5 × 5 box 20 times with the clip filter: a dark frame grows from the edges. Copy edge and reflect keep the border bright.
- Choose Sobel and switch between correlation and convolution: dark and light edges swap, because the flipped filter measures the slope with the opposite sign.
- Select a pixel on an edge and check the values panel: the output is the sum of the element-wise products of the two matrices.
- Set the result display to “absolute” with Sobel : now all vertical edges are bright, whatever their direction.
- Emboss is asymmetric too: under convolution the light seems to come from the other side.
- Edit the center weight of the identity filter to 2: the image gets twice as bright and clips. Then try a filter whose weights sum to 0.