Skip to content

Gaussian filter

Fact Value
Also called Gaussian blur, Separable Gaussian, Per-channel Gaussian
Status Implemented and tested
Family Linear spatial filters
Engine Browser
Type Linear, spatial, separable convolution
Output Image
Operator shown as dynamic-kernel (exact)
Support radius floor(truncate * sigma + 0.5)
Code web/src/core/filters/linear/gaussian.ts, gaussianFilter
Operator code web/src/core/filters/linear/gaussian.ts, gaussianKernel1d
Channels RGB, Brightness only, Color only

Overview

The Gaussian filter blurs with weights that follow a bell-shaped curve. Close neighbors count more than far ones, so the blur looks smooth and round. It is the standard way to reduce Gaussian noise and to prepare an image for edge detection.

A line plot of brightness across a sharp edge, before blurring and after Gaussian blurs with sigma 1, 2 and 4. A line plot of brightness across a sharp edge, before blurring and after Gaussian blurs with sigma 1, 2 and 4.
A larger sigma spreads a sharp edge over more pixels.

How it works

  1. Take the window around the pixel. Its radius is about three times sigma.
  2. Give each neighbor a weight from a bell-shaped curve. Close neighbors get more weight.
  3. Multiply each neighbor by its weight and add the results.
  4. Write the sum as the new pixel value. The weights add up to 1, so flat areas keep their brightness.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \sum_{j=-r}^{r} \sum_{i=-r}^{r} g_i \, g_j \, I(x+i,\, y+j), \qquad g_i = \frac{e^{-i^2 / (2\sigma^2)}}{\sum_{k=-r}^{r} e^{-k^2 / (2\sigma^2)}}, \qquad r = \lfloor 3 \, \sigma + 0.5 \rfloor, \quad \sigma = 1 \]

The formula shows the default parameter values.

where

  • \(I\): Input channel value
  • \(I_{\text{out}}\): Output channel value
  • \((x, y)\): Pixel position. x grows to the right and y grows downward.
  • \(\sigma\): Standard deviation of the Gaussian in pixels
  • \(r\): Window radius in pixels
  • \(g_i\): Normalized 1D weight at offset i

Each output pixel is a weighted sum over a square window. The weights come from a Gaussian curve and add up to 1. The 2D weight is the product of two 1D weights, so the filter runs as one pass along rows and one pass along columns.

The 2D Gaussian is the product of a Gaussian along x and a Gaussian along y. That is why the filter can run as two 1D passes. The weights are samples of the curve at whole pixel offsets, divided by their sum. This normalization makes the discrete weights add up to exactly 1. The window stops at the radius r, which cuts off the far tails of the curve.

Kernel or operator

The 1D kernel for sigma 1, and the center of the 2D kernel made from it. The studio's Inspector shows the full matrix for the current sigma.

\[ g = \begin{bmatrix} 0.0044 & 0.0540 & 0.2420 & 0.3991 & 0.2420 & 0.0540 & 0.0044 \end{bmatrix}^{\mathsf{T}} \qquad (\sigma = 1,\ \text{truncate} = 3,\ r = 3) \]
\[ G = g\, g^{\mathsf{T}} = \begin{bmatrix} 0.0586 & 0.0966 & 0.0586 \\ 0.0966 & 0.1592 & 0.0966 \\ 0.0586 & 0.0966 & 0.0586 \end{bmatrix} \quad \text{(center 3x3 of 7x7)} \]

Algorithm

  1. Compute the radius r from sigma and truncate.
  2. Build the 1D weights g_i for i from -r to r and divide them by their sum.
  3. Pad each row with the border mode and filter it with the weights.
  4. Pad each column of that result and filter it with the same weights.
  5. Round to 8 bits. Keep alpha unchanged.

Parameters

Parameter Default Values Meaning
Sigma sigma 1 0.1 to 50 px Standard deviation of the Gaussian curve.
Truncate truncate 3 1 to 6 Window radius in units of sigma.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope RGB RGB, Brightness only, Color only Which part of the color image the filter changes.
Linear light linearLight off On or off Blur linear light values instead of sRGB encoded values.

Implementation

The kernel comes from gaussianKernel1d. The same function feeds the Inspector, so the shown matrix is the one that runs. The radius is floor(truncate times sigma plus 0.5), the rule SciPy uses. With Linear light on, values are decoded from sRGB before the blur and encoded again after it. The filter runs in the browser engine, in Web Workers. The studio splits the image into strips. Each strip gets a halo as wide as the support radius, so the strips give exactly the pixels of a whole-image run. The shared filter framework in web/src/core/filters/run.ts handles the border mode, the channel scope, alpha and the final rounding to 8 bits.

Worked example

Offset i -3 -2 -1 0 1 2 3
Value 40 40 40 40 215 215 215
Weight g_i 0.0044 0.0540 0.2420 0.3991 0.2420 0.0540 0.0044
Value x weight 0.18 2.16 9.68 15.96 52.04 11.61 0.95

The row crosses an edge from 40 to 215. Every row of the test image is the same, so the column pass changes nothing and the result equals the row pass. The products add up to 92.58. The filter code gives 92.58 for the center pixel, which rounds to 93.

Complexity

  • Time: \(O(N\,(2r+1))\)
  • Memory: \(O(N)\)

N is the number of pixels per channel. A direct 2D kernel would cost O(N\,(2r+1)^2).

Artifacts and limitations

Limitations

  • Edges blur as much as noise does.
  • Salt-and-pepper noise spreads instead of going away. Use the median filter for it.
  • Very large sigma values are slow without a recursive approximation.

Artifacts

  • Soft edges.
  • A faint dark band at the image border with the constant border mode.

Tests

The 1D kernel is compared with SciPy for five sigma and truncate pairs within 1e-12. The full filter is compared with scipy.ndimage.gaussian_filter for three settings and all five border modes, within 1e-4 relative to the value. A test also checks that the Inspector kernel gives the same output as the filter.

References