Gaussian filter
What is a Gaussian Filter?
In image processing, a Gaussian filter is a type of smoothing filter used to reduce noise in an image.
It performs filtering by convolving an image with a kernel that approximates a Gaussian distribution.
2D Gaussian Distribution
For Gaussian filtering in images, a 2D Gaussian distribution is used.
Typically, in the absence of special considerations, we assume that the x and y directions are independent and follow the same variance.
This means we don’t need to consider general Gaussian distributions with correlations between x and y or different variances.
First, let’s display the probability density function of such a 2D Gaussian distribution.
The marginal distribution in the x-direction is a 1D Gaussian distribution with standard deviation σ:
Similarly, the marginal distribution in the y-direction is a 1D Gaussian distribution with standard deviation σ:
Since these are independent, the joint probability density function is:
Approximation of 2D Gaussian Distribution
This distribution
Since convolving an image (which consists of finite, discrete pixels) with something that extends infinitely is difficult,
This grid is the kernel.
A common example of such a kernel is shown below.
How is this kernel approximated from
In practice, the kernel is computed by evaluating
For instance, in the case of a 3x3 grid with
Summing these values gives:
So, the values of the kernel at each grid point are:
After calculating
If we denote this by
Also,
Then,
Thus, to compute the kernel, determine
Normalizing this yields the kernel.
Conversely, if a kernel is given, you can determine the base Gaussian distribution’s
For example, for the 5x5 grid shown above:
So,
Based on this discussion, an example code to compute a Gaussian filter kernel using numpy is:
import numpy as np
def make_gaussian_kernel(width: int, height: int, sigma: float) -> np.ndarray
# Ratio of the value at the center to one neighbor
r = np.exp(-1 / (2 * sigma**2))
# Center index of the kernel
mw = width // 2
mh = height // 2
# Array of coordinates relative to the center of the kernel
xs = np.arange(-mw, mw + 1)
ys = np.arange(-mh, mh + 1)
x, y = np.meshgrid(xs, ys)
# Calculate the base array by raising r to the squared distance
g = r**(x**2 + y**2)
# Normalize the kernel
return g / np.sum(g)
Convolution
The resulting kernel is then convolved with the original image to apply the Gaussian filter.
Convolution can be thought of as “integrating the surrounding pixel values according to the kernel.”
Let
where
When performing convolution, several variations for handling the edges of the original image can be considered.
For instance, the scipy library’s convolve2d function provides several modes:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html
Filter Example
The following shows an example of applying a 3x3 Gaussian filter with
It can be observed that the original image is smoothed.
Edges are also blurred, so choosing the appropriate filter depends on the nature of the noise.