Skip to content

Guided filter

Fact Value
Status Implemented and tested
Family Nonlinear spatial filters
Engine Browser
Type Nonlinear, spatial, local linear model
Output Image
Operator shown as estimator (exact)
Support radius 2 radius
Code web/src/core/filters/nonlinear/guided.ts, guidedFilter
Operator code web/src/core/filters/nonlinear/guided.ts, guidedPlane
Channels RGB, Brightness only

Overview

The guided filter assumes that, in every small window, the output is a straight-line function of a guide image: a times the guide plus b. It fits a and b to the input in each window. In flat windows the best slope is near 0, so the output is the window mean and the noise is gone. In windows with an edge the slope is near 1, so the edge stays. All steps are box means, so the cost does not depend on the radius. Here the guide is the image itself.

A plot of one row across a noisy step edge. With eps 0.01 the guided filter keeps the step and some noise. With eps 0.1 it smooths more of the noise and rounds the step a little. A plot of one row across a noisy step edge. With eps 0.01 the guided filter keeps the step and some noise. With eps 0.1 it smooths more of the noise and rounds the step a little.
A small eps keeps the edge and some noise. A larger eps smooths more of both.

How it works

  1. In each window, find the mean and variance of the guide and its covariance with the input.
  2. Compute the slope a and offset b of the best straight line, with a penalty eps on the slope.
  3. For each pixel, average a and b over all windows that hold it, and write a times the guide plus b.

Mathematical foundation

\[ a_k = \frac{\operatorname{cov}_k(I, p)}{\sigma_k^2 + \varepsilon}, \qquad b_k = \bar{p}_k - a_k \bar{I}_k, \qquad q_i = \bar{a}_i\, I_i + \bar{b}_i \]

The formula shows the default parameter values.

where

  • \(I\): The guide image, here the input
  • \(p\): The input image
  • \(a_k, b_k\): Slope and offset of the model in window k
  • \(\varepsilon\): The regularization
  • \(\bar{a}_i, \bar{b}_i\): The mean of a and b over the windows that hold pixel i

In each window k, the output is modeled as a I + b, a straight-line function of the guide I. The slope a and offset b that fit the input p best, with a penalty eps on a, come from the window mean, variance and covariance. A pixel lies in many windows, so its output uses the mean of their a and b, written with a bar. Here the guide is the input itself, so cov(I, p) is the variance and a is var / (var + eps). eps is scaled by 255 squared for 8-bit values.

The slope is the ridge regression solution a = cov(I, p) / (var(I) + eps). With the input as its own guide, cov(I, p) is the variance, so a = var / (var + eps). Detail whose variance is well below eps gets a near 0 and is smoothed. Detail with much more variance gets a near 1 and is kept. He, Sun and Tang show that the result equals a filter kernel that depends on the guide, and that it avoids the gradient reversal of the bilateral filter.

Kernel or operator

The Inspector shows the model with the current eps. The worked example below computes a and b for a flat window and an edge window.

Algorithm

  1. Extend the image by twice the radius with the border mode.
  2. Compute box means of I, p, I times I and I times p.
  3. Compute a and b for every window from these means.
  4. Compute box means of a and b, and write a times I plus b. Cut the extension off.

Parameters

Parameter Default Values Meaning
Radius radius 4 1 to 30 px The radius r of the box windows. Each window is 2r + 1 wide.
Regularization eps 0.01 0.0001 to 1 The penalty eps on the slope a, for values from 0 to 1, as in the paper. 0.01 is 0.1 squared.
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 Which part of the color image the filter changes.

Implementation

guidedPlane in web/src/core/filters/nonlinear/guided.ts computes six box means in float64 with running sums. The output needs twice the radius around each pixel, because a and b are averaged once more. So the plane is extended once by twice the radius with the border mode, filtered, and cut. For the reflect modes and wrap, this equals applying the border rule to every box mean. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts. It handles the border mode, the channel scope, alpha and the rounding to 8 bits. 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

With the input as its own guide, a = var / (var + eps). For eps = 0.01, scaled to 8 bits, eps = 0.01 x 255 squared = 650.25. Two 3x3 windows:

Window Mean Variance a b
Flat with noise 50 3.11 0.0048 49.76
Edge, 40 and 200 93.33 5688.9 0.8974 9.57

In the flat window a is near 0, so the model a I + b is almost the constant 50, the mean, and the noise is removed. In the edge window a is near 1 and b is small, so the model keeps I, and the edge stays. The output of a pixel averages a and b over all windows that hold it.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels. Every step is a box mean with running sums, so the radius does not change the cost.

Artifacts and limitations

Limitations

  • Soft halos can appear next to strong edges.
  • Here the guide is the image itself. A separate guide image is not offered yet.

Artifacts

  • Faint halos along high-contrast edges with large radii.

Tests

The reference uses scipy.ndimage.uniform_filter on the extended plane. Before it is used, the script checks it against cv2.ximgproc.guidedFilter, which uses the reflect mode, within 0.01. The test covers three radius and eps settings for all five border modes.

References