Skip to content

Bilateral filter

Fact Value
Also called Vector bilateral filter, Edge-preserving blur
Status Implemented and tested
Family Nonlinear spatial filters
Engine Browser
Type Nonlinear, spatial, weighted mean with data-dependent weights
Output Image
Operator shown as dynamic-kernel (exact)
Support radius ceil(3 sigma_s)
Code web/src/core/filters/nonlinear/bilateral.ts, bilateralFilter
Operator code web/src/core/filters/nonlinear/bilateral.ts, bilateralWeights
Channels RGB

Overview

The bilateral filter is a blur that only mixes a pixel with neighbors that look like it. Each neighbor gets two weights: one that falls with its distance, as in a Gaussian blur, and one that falls with its difference in value. Neighbors across an edge are very different, so they get almost no weight, and the edge stays sharp. The color mode measures the difference between whole colors, the vector bilateral filter.

A plot of one row across a noisy step edge. The Gaussian blur smooths the noise and turns the step into a slope. The bilateral filter smooths the noise and keeps the step. A plot of one row across a noisy step edge. The Gaussian blur smooths the noise and turns the step into a slope. The bilateral filter smooths the noise and keeps the step.
The Gaussian blur smooths the noise and the edge. The bilateral filter smooths the noise and keeps the edge.

How it works

  1. Give each neighbor a weight that falls with its distance from the pixel.
  2. Multiply it by a weight that falls with the difference in value.
  3. Write the weighted mean of the neighbors.

Mathematical foundation

\[ I_{\text{out}}(p) = \frac{\sum_{q \in \Omega_p} w(p,q)\, I(q)}{\sum_{q \in \Omega_p} w(p,q)}, \qquad w(p,q) = e^{-\frac{\lVert p - q \rVert^2}{2\sigma_s^2}}\; e^{-\frac{d(I(p),\, I(q))^2}{2\sigma_r^2}} \]

The formula shows the default parameter values.

where

  • \(\sigma_s\): The spatial sigma in pixels
  • \(\sigma_r\): The range sigma in 8-bit levels
  • \(d\): The difference in one channel, or the distance between RGB colors
  • \(\Omega_p\): The disk-shaped window around p

The weight of a neighbor q is the product of two Gaussians. The first falls with the distance between the pixels, the second with the difference d between their values. Neighbors across an edge have a large d, so they get almost no weight, and the edge stays. Omega_p is the disk of radius ceil(3 sigma_s) around p, the window of OpenCV's bilateralFilter.

The weights depend on the image, so the kernel changes from pixel to pixel and the filter is nonlinear. Differences much larger than the range sigma get a weight near 0. When the range sigma is very large, every value weight is near 1, and the filter becomes a Gaussian blur on a disk. The window is the disk of radius ceil(3 sigma_s), as in OpenCV, where the distance weight has fallen below 1.2 percent.

Kernel or operator

The Inspector shows the weight formula with the current parameters. The Weight viewer in the Learn panel draws the distance weight, the value weight and their normalized product at any pixel, from the same weight function the filter uses.

Algorithm

  1. Pad the image with the border mode.
  2. For each pixel, visit every neighbor in the disk of radius ceil(3 sigma_s).
  3. Multiply the distance weight by the value weight of the neighbor.
  4. Write the weighted mean of the neighbors, divided by the sum of the weights.

Parameters

Parameter Default Values Meaning
Spatial sigma sigmaSpace 3 0.5 to 10 px The spread of the distance weight. The window is the disk of radius 3 sigma, rounded up.
Range sigma sigmaRange 30 1 to 200 The spread of the value weight, in 8-bit levels.
Difference range Each channel Each channel, Color distance Measure the difference in each channel on its own, or as the distance between colors.
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.

Implementation

bilateralPlanes in web/src/core/filters/nonlinear/bilateral.ts visits every neighbor in the disk for every pixel. In the channel mode, each channel has its own value weights. In the color mode, the value weight comes from the RGB distance and is shared. The same function takes a separate guide for the joint bilateral filter. It is a color filter: the framework in web/src/core/filters/run.ts hands it the red, green and blue planes together, with the border mode, and keeps alpha. Each strip of the image gets a halo as wide as the support radius, so strips give exactly the pixels of a whole-image run.

Worked example

\[ W = \begin{bmatrix} 50 & 52 & 200 \\ 48 & 50 & 202 \\ 51 & 49 & 199 \end{bmatrix} \]

The center 50 sits next to an edge. With sigma_s = 1 and sigma_r = 30, compare its left and right neighbors, both at distance 1:

Neighbor Value Distance weight Value weight Product
Left 48 0.6065 0.9978 0.6052
Right 202 0.6065 3.73e-6 2.26e-6

The right neighbor is across the edge, so its weight is about 268 thousand times smaller. The filter gives 49.91 for the center of this 3x3 image, close to the dark values. A Gaussian blur of the same window would be pulled toward 200.

Complexity

  • Time: \(O(N\,\sigma_s^2)\)
  • Memory: \(O(N)\)

N is the number of pixels. The disk holds about 28 sigma_s squared pixels.

Artifacts and limitations

Limitations

  • Costs much more than a Gaussian blur for large spatial sigmas.
  • Noise with a spread like the edge contrast is kept as if it were an edge.

Artifacts

  • Staircase steps on smooth ramps, called gradient reversal, with small range sigmas.
  • Halos are rare but appear when the range sigma is close to the edge contrast.

Tests

The reference computes all weights of each NumPy window. Before it is used, the script checks the channel mode against cv2.bilateralFilter on an 8-bit image, within 1 level for rounding. The test covers the channel mode and two settings of the color mode on a color input, for all five border modes.

References