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.
How it works¶
- Take the window around the pixel. Its radius is about three times sigma.
- Give each neighbor a weight from a bell-shaped curve. Close neighbors get more weight.
- Multiply each neighbor by its weight and add the results.
- Write the sum as the new pixel value. The weights add up to 1, so flat areas keep their brightness.
Mathematical foundation¶
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.
Algorithm¶
- Compute the radius r from sigma and truncate.
- Build the 1D weights g_i for i from -r to r and divide them by their sum.
- Pad each row with the border mode and filter it with the weights.
- Pad each column of that result and filter it with the same weights.
- 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.
web/src/core/filters/linear/gaussian.test.tsweb/src/core/filters/filters.test.tsweb/src/core/filters/run.test.ts
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 3.
- SciPy documentation, scipy.ndimage.gaussian_filter. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.gaussian_filter.html