Image Filters

An image filter transforms pixels in a digital image from values in a local neighbourhood. A filter suppresses or strengthens selected image detail.

Filter Kernel

Also known as mask, template or window. A kernel is a grid of coefficients centred on the pixel being processed.

For a linear filter:

P(x,y)=∑i=−NN∑j=−MMH(i,j) I(x−i,y−j)P(x,y) = \sum_{i=-N}^{N}\sum_{j=-M}^{M} H(i,j)\,I(x-i,y-j)
Here:
I(x,y)I(x,y)
input image intensity
P(x,y)P(x,y)
filtered output intensity
H(i,j)H(i,j)
kernel coefficient at offset (i,j)(i,j)

The kernel moves over every pixel. The weighted sum becomes the output pixel.

Correlation and Convolution

Correlation places the kernel over the image without reversing it.

Pcorr(x,y)=∑i=−NN∑j=−MMH(i,j) I(x+i,y+j)P_{\text{corr}}(x,y) = \sum_{i=-N}^{N}\sum_{j=-M}^{M} H(i,j)\,I(x+i,y+j)

Convolution reverses the kernel by 180∘180^\circ before taking the weighted sum. The filter-kernel equation above is convolution.

Pconv(x,y)=∑i=−NN∑j=−MMH(i,j) I(x−i,y−j)P_{\text{conv}}(x,y) = \sum_{i=-N}^{N}\sum_{j=-M}^{M} H(i,j)\,I(x-i,y-j)

Symmetric kernels give the same result for both operations. A non-symmetric kernel gives the correlation result of its 180∘180^\circ rotation under convolution.

Both operations are linear:

H∗(aI+bJ)=a(H∗I)+b(H∗J)H * (aI + bJ) = a(H * I) + b(H * J)

Convolution is commutative, associative and distributive over addition. A cascade of linear filters, such as Gaussian smoothing followed by a Laplacian, remains a linear operation.

Image Boundaries

The full neighbourhood is unavailable at an image boundary. A filtering implementation must choose a boundary rule.

  • Replication
    Repeat the nearest edge row or column outside the image.
  • Wrapping
    Take missing pixels from the opposite edge. Suitable for periodic images.
  • Reduced support
    Change the kernel shape or its normalization near the boundary.

Kernel Properties

  • Normalized kernel
    Coefficients sum to 11. Constant-intensity regions retain their intensity.
  • Symmetric kernel
    The kernel gives equal weight to opposite offsets. Correlation and convolution produce the same result.
  • Separable kernel
    A 2-D kernel factors into two 1-D kernels. Filter rows, then columns, with fewer operations.

Kernel Classification

For a linear kernel, first add all coefficients:

S=∑i,jH(i,j)S = \sum_{i,j} H(i,j)

On a constant-intensity region with value cc, every covered pixel is cc. The filter output is ScSc.

  • S=1S=1
    Constant intensity passes unchanged. The kernel preserves the zero-frequency component.
  • S=0S=0
    Constant intensity becomes 00. The kernel rejects the zero-frequency component.
  • S≠0,1S \ne 0,1
    Constant intensity changes. The kernel scales or inverts the zero-frequency component.

Low-Pass Kernels

A low-pass kernel usually has these features:

  • All or nearly all coefficients are positive
  • The greatest coefficient is at the centre
  • Coefficients decrease with distance from the centre
  • Coefficients sum to 11

The mean and Gaussian kernels have this pattern. They average a neighbourhood, so neighbouring differences cancel.

High-Pass Kernels

A high-pass kernel usually has these features:

  • Positive and negative coefficients
  • Coefficients that sum to 00
  • A positive centre surrounded by negative coefficients, or the reverse

The kernel returns 00 on constant regions. It produces a large magnitude where neighbouring pixels differ.

Derivative Kernels

A derivative kernel has opposite signs on opposite sides of the pivot. It detects change in one direction.

[−101][−101]\begin{bmatrix} -1 & 0 & 1 \end{bmatrix} \qquad \begin{bmatrix} -1 \\ 0 \\ 1 \end{bmatrix}

The first kernel responds to horizontal intensity change. The second responds to vertical intensity change.

Sharpening Kernels

A sharpening kernel often has a positive centre and negative neighbours, but its coefficients sum to 11. It preserves constant intensity while adding a high-pass response to the original image.

[0−10−15−10−10]\begin{bmatrix} 0 & -1 & 0 \\ -1 & 5 & -1 \\ 0 & -1 & 0 \end{bmatrix}

It is not a pure high-pass filter, since constant regions remain visible.

Exact Classification

Coefficient patterns give a quick classification. The exact result comes from the frequency response:

H(ωx,ωy)=∑i,jH(i,j) e−j(ωxi+ωyj)\mathcal{H}(\omega_x,\omega_y) = \sum_{i,j} H(i,j)\,e^{-j(\omega_x i + \omega_y j)}

A low-pass filter has a large magnitude near (0,0)(0,0) and a small magnitude near the highest frequencies. A high-pass filter has the reverse. A band-pass filter suppresses both regions and keeps an intermediate frequency band.

Low-Pass Filters

A low-pass filter keeps slowly varying intensity and attenuates rapid change. It smooths noise and fine detail.

Used before downsampling to prevent aliasing. It also reduces noise before feature extraction or lossy coding.

Mean Filter

The mean filter assigns equal weight to every pixel in its neighbourhood:

H=19[111111111]H = \frac{1}{9} \begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix}

It strongly blurs edges. Use it for zero-mean random noise when edge preservation is not important.

Gaussian Filter

The Gaussian filter gives the greatest weight to the pivot pixel. Weights decrease smoothly with distance:

h(x,y)=exp⁡[−x2+y22σ2]h(x,y) = \exp\left[-\frac{x^2+y^2}{2\sigma^2}\right]

σ\sigma controls the blur width. Larger σ\sigma spreads weight farther from the pivot and removes more high-frequency detail.

Kernel Construction

The continuous function must be sampled to make a finite kernel. For a 5×55 \times 5 kernel, use the offsets −2,−1,0,1,2-2,-1,0,1,2.

Choose σ\sigma, then calculate the unnormalized 1-D weights:

g(i)=exp⁡[−i22σ2]i∈{−2,−1,0,1,2}g(i) = \exp\left[-\frac{i^2}{2\sigma^2}\right] \qquad i \in \{-2,-1,0,1,2\}

Normalize them so their sum is 11:

w(i)=g(i)∑k=−22g(k)w(i) = \frac{g(i)}{\sum_{k=-2}^{2}g(k)}

For σ=1\sigma=1, the unnormalized weights are approximately:

[0.1350.60710.6070.135]\begin{bmatrix} 0.135 & 0.607 & 1 & 0.607 & 0.135 \end{bmatrix}

Their normalized values are approximately:

[0.0540.2440.4030.2440.054]\begin{bmatrix} 0.054 & 0.244 & 0.403 & 0.244 & 0.054 \end{bmatrix}

Form the 2-D kernel by multiplying 1 horizontal weight by 1 vertical weight:

H(i,j)=w(i)w(j)H(i,j) = w(i)w(j)

The centre coefficient is largest. Corner coefficients are smallest.

Binomial Approximation

A common integer approximation for σ≈1\sigma \approx 1 is:

w=116[14641]w = \frac{1}{16}\begin{bmatrix}1 & 4 & 6 & 4 & 1\end{bmatrix}

The numerator coefficients sum to 1616, so division by 1616 normalizes the 1-D kernel. Its outer product forms the 2-D kernel:

H=wTw=1256[1464141624164624362464162416414641]H = w^\mathsf{T}w = \frac{1}{256} \begin{bmatrix} 1 & 4 & 6 & 4 & 1 \\ 4 & 16 & 24 & 16 & 4 \\ 6 & 24 & 36 & 24 & 6 \\ 4 & 16 & 24 & 16 & 4 \\ 1 & 4 & 6 & 4 & 1 \end{bmatrix}

The top-left corner coefficient is 116116=1256\frac{1}{16}\frac{1}{16} = \frac{1}{256}. The coefficients sum to 11. Apply ww horizontally, then vertically. This is equivalent to convolution with HH.

At each output pixel, multiply every value in the covered 5×55 \times 5 neighbourhood by the matching HH coefficient, then sum the 2525 products.

Use the Gaussian filter before downsampling, in Gaussian pyramids and to smooth noise before differentiation.

High-Pass Filters

A high-pass filter attenuates slowly varying intensity and preserves rapid change. It strengthens edges and fine detail.

A simple high-pass kernel is:

[0−10−14−10−10]\begin{bmatrix} 0 & -1 & 0 \\ -1 & 4 & -1 \\ 0 & -1 & 0 \end{bmatrix}

Its response is near 00 in constant regions and large at intensity transitions. High-pass filters also amplify noise.

Non-Linear Filters

Non-linear filters do not form a weighted sum. The median filter replaces the pivot with the median neighbourhood intensity.

A median filter has no numeric kernel. It uses a neighbourhood window, commonly 3×33 \times 3. The window identifies the 99 values to sort. No coefficient is multiplied by a pixel value.

It removes isolated salt-and-pepper noise while preserving edges better than mean or Gaussian filtering.

Worked Examples

Classifying Kernels

Classify the following kernels.

H1=19[111111111]H2=[0−10−14−10−10]H3=[0−10−15−10−10]H_1 = \frac{1}{9} \begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix} \qquad H_2 = \begin{bmatrix} 0 & -1 & 0 \\ -1 & 4 & -1 \\ 0 & -1 & 0 \end{bmatrix} \qquad H_3 = \begin{bmatrix} 0 & -1 & 0 \\ -1 & 5 & -1 \\ 0 & -1 & 0 \end{bmatrix}
Solution Reveal Hide
∑i,jH1(i,j)=1\sum_{i,j}H_1(i,j) = 1

H1H_1 has only positive, equal weights. It is a mean low-pass filter.

∑i,jH2(i,j)=4−1−1−1−1=0\sum_{i,j}H_2(i,j) = 4 - 1 - 1 - 1 - 1 = 0

H2H_2 rejects constant regions. It is a high-pass Laplacian kernel.

∑i,jH3(i,j)=5−1−1−1−1=1\sum_{i,j}H_3(i,j) = 5 - 1 - 1 - 1 - 1 = 1

H3H_3 preserves constant regions and strengthens differences from the pivot. It is a sharpening kernel.

Median Filter

Apply a 3×33 \times 3 median filter to the neighbourhood below.

[1011101225511101211]\begin{bmatrix} 10 & 11 & 10 \\ 12 & 255 & 11 \\ 10 & 12 & 11 \end{bmatrix}
Solution Reveal Hide

The filter does not multiply values by kernel coefficients. Sort the 99 neighbourhood values:

{10,10,10,11,11,11,12,12,255}\{10,10,10,11,11,11,12,12,255\}

The middle value is 1111. The filtered pivot value is 1111.

Written by November 3, 2026 6 min read
Was this helpful?