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.
How it works¶
- In each window, find the mean and variance of the guide and its covariance with the input.
- Compute the slope a and offset b of the best straight line, with a penalty eps on the slope.
- For each pixel, average a and b over all windows that hold it, and write a times the guide plus b.
Mathematical foundation¶
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¶
- Extend the image by twice the radius with the border mode.
- Compute box means of I, p, I times I and I times p.
- Compute a and b for every window from these means.
- 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¶
- He, K., Sun, J. and Tang, X. Guided image filtering. IEEE Transactions on Pattern Analysis and Machine Intelligence 35(6), 1397 to 1409, 2013. DOI 10.1109/TPAMI.2012.213.
- OpenCV documentation, cv::ximgproc::guidedFilter. https://docs.opencv.org/4.x/da/d17/group__ximgproc__filters.html