Skip to content

Joint bilateral filter

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

Overview

The joint bilateral filter, also called the cross bilateral filter, smooths one image along the edges of another. It works like the bilateral filter, but the weight that protects edges compares the pixels of a guide image instead of the image itself. So a noisy photo can be cleaned with the edges of a sharper photo of the same scene, such as a flash photo. Without a guide image, the luma of the image itself guides, which smooths colors along brightness edges.

Three panels: disks with strong noise, a bilateral filter result that is still noisy and blurred because the noise hides the edges, and the joint bilateral result with the clean image as the guide, smooth with sharp disk edges. Three panels: disks with strong noise, a bilateral filter result that is still noisy and blurred because the noise hides the edges, and the joint bilateral result with the clean image as the guide, smooth with sharp disk edges.
The noise hides the edges, so the bilateral filter cannot find them. With the clean image as the guide, the joint bilateral filter keeps them.

How it works

  1. Give each neighbor a weight that falls with its distance.
  2. Multiply it by a weight that falls with the difference between the guide pixels.
  3. Write the weighted mean of the image values.

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(G(p),\, G(q))^2}{2\sigma_r^2}} \]

The formula shows the default parameter values.

where

  • \(G\): The guide image
  • \(I\): The image to filter
  • \(\sigma_s, \sigma_r\): The spatial and range sigmas
  • \(\Omega_p\): The disk-shaped window around p

The same weighted mean as the bilateral filter, but the value weight compares the guide G at the two pixels, not the image. Where the guide has an edge, neighbors across it get almost no weight, even if the image is too noisy to show that edge. The difference d is that of the guide luma, or the distance between guide colors. With the image itself as the guide and the color difference, this is the bilateral filter in its color mode.

Only the value weight changes from the bilateral filter: it uses the guide G, not the image I. So the filter keeps the edges of G, and averages I within the regions of G. With the image itself as the guide and the color difference, it is the bilateral filter in its color mode. Petschnigg and others, and Eisemann and Durand, introduced it in 2004 for flash and no-flash pairs. The flash photo has low noise and clear edges. The no-flash photo has the natural light.

Kernel or operator

The Inspector shows the weight formula with the current parameters. The worked example below shows how a guide edge removes one column of the window.

Algorithm

  1. Pad the image and the guide 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 guide weight of the neighbor.
  4. Write the weighted mean of the image values.

Parameters

Parameter Default Values Meaning
Guide image guide This image, or another open image of the same size Another open image of the same size whose edges steer the smoothing. This image is used when no other is chosen.
Guide difference guideRange Guide brightness Guide brightness, Guide color distance Measure the difference in the guide by its luma or by the distance between its RGB colors.
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 20 1 to 200 The spread of the guide weight, in 8-bit levels of the guide.
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

jointBilateralFilter in web/src/core/filters/nonlinear/bilateral.ts shares bilateralPlanes with the bilateral filter and hands it the guide planes for the value weight. The guide comes from a new image parameter: Properties lists the open images of the same size. The browser engine pads the guide like the image for every strip, sends both to the worker, and the filter framework passes the guide planes to the filter. The live preview scales the guide with the image. The filter runs in the browser engine, in Web Workers.

Worked example

\[ I = \begin{bmatrix} 130 & 60 & 205 \\ 90 & 150 & 195 \\ 110 & 80 & 210 \end{bmatrix}, \qquad G = \begin{bmatrix} 30 & 30 & 220 \\ 30 & 30 & 220 \\ 30 & 30 & 220 \end{bmatrix} \]

The guide G has an edge between its second and third columns. With sigma_s = 1 and sigma_r = 20, the guide difference across the edge is 190, so the value weight of the right column is about 2.5e-20. Only the left two columns count:

Value
Filter output for the center 108.67
Mean of the left two columns 103.33
Mean of all nine values 136.67

The output is a distance-weighted mean of the six values on the dark side of the guide, so it is close to their plain mean. The bright right column does not pull it up, as it would in a plain mean.

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

  • The guide must be aligned with the image, pixel for pixel.
  • Edges in the guide that are not in the scene, such as flash shadows, are copied into the result.

Artifacts

  • Texture from the guide can appear in the result when the range sigma is small.

Tests

The reference computes the guide weights with NumPy. Before it is used, the script checks it against cv2.ximgproc.jointBilateralFilter on gray images, and checks that guiding by the image itself gives the bilateral filter. The test covers both guide modes with a separate guide and one case without a guide, for all five border modes. Engine tests check that a guide gives the same pixels in strips, in regions and in workers as in a whole-image run.

References