Detail enhancement¶
| Fact | Value |
|---|---|
| Also called | Bilateral detail enhancement, Guided detail enhancement, Base and detail boost |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Nonlinear, spatial, base and detail decomposition with detail boost |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/enhancement/detail.ts, detailEnhanceFilter |
| Operator code | web/src/core/filters/enhancement/detail.ts, detailEnhanceFilter |
| Channels | RGB, Brightness only |
Overview¶
Detail enhancement smooths the image with a filter that keeps edges, the base layer, and treats what the smoothing removed as detail. Adding the detail back several times makes texture and fine structure stronger. He, Sun and Tang show it with the guided filter and with the bilateral filter.
How it works¶
- Smooth the image with the guided or the bilateral filter. This is the base.
- Subtract the base from the image. This is the detail.
- Multiply the detail by the boost and add it to the base.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(p\): The input
- \(q\): The base layer, an edge-preserving smoothing of p
- \(d\): The detail layer
- \(k\): The boost
q is an edge-preserving smoothing of the input p, the base layer. The difference d is the detail layer, and p' adds it back k times. He, Sun and Tang show this with a detail layer boosted 5 times. Their guided filter uses r = 16 and eps = 0.1^2. Their bilateral filter uses sigma_s = 16 and sigma_r = 0.1, on values from 0 to 1. They note that the bilateral filter can reverse gradients near edges, while the guided filter does not. The bilateral base here is the piecewise-linear bilateral filter of Durand and Dorsey, also used for tone mapping, so its cost does not grow with sigma_s.
The result depends on what the base filter keeps. Whatever stays in the base is not boosted. The guided filter fits a straight line in each window. With eps = 0.01, a window that crosses a strong step still flattens part of it, so that part lands in the detail layer and is boosted. The bilateral filter averages only similar values, so a step much larger than sigma_r stays whole in the base. He, Sun and Tang point out the other side: near soft edges, the bilateral filter can reverse gradients, which the guided filter never does.
Kernel or operator¶
The Inspector shows the base filter with its settings, the detail layer and the boost.
Algorithm¶
- Smooth the input with the guided or the bilateral filter to get the base layer.
- Subtract the base from the input to get the detail layer.
- Add the detail, multiplied by the boost, to the base.
- Clip to 0 to 255.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Base filter base |
Guided filter | Guided filter, Bilateral filter | The edge-preserving filter that makes the base layer. |
Boost boost |
5 | 0 to 20 | The factor k on the detail layer. 1 gives the input back, 0 gives the base. |
Guided radius radius |
16 | 1 to 64 px | The radius r of the guided filter's windows, for the guided base. |
Guided regularization eps |
0.01 | 0.0001 to 1 | The guided filter's penalty eps, for values from 0 to 1. 0.01 is 0.1 squared. |
Bilateral spatial sigma sigmaSpace |
16 | 1 to 100 px | The spatial scale sigma_s of the bilateral filter, for the bilateral base. |
Bilateral range sigma sigmaRange |
25.5 | 1 to 255 | The range scale sigma_r of the bilateral filter, in levels from 0 to 255. 25.5 is 0.1 of the range. |
Bilateral downsampling downsample |
1 | 1 to 16 | The bilateral base is filtered on every z-th pixel, as Durand and Dorsey do for speed. |
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. |
Output mapping outputMapping |
Clip to 0 to 255 | Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 | How signed results, such as gradients, become 8-bit pixel values. |
Channels channelScope |
RGB | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
detailEnhanceFilter in web/src/core/filters/enhancement/detail.ts gets the base from detailBase. The guided base is guidedPlane, the code of the guided filter. The bilateral base is piecewiseBilateral, the code of bilateral tone mapping.
Worked example¶
| Image | Texture deviation | Mean left of the edge | Mean right of the edge |
|---|---|---|---|
| Input | 4.24 | 60 | 180 |
| Guided base, boost 5 | 20.65 | 11.9 | 228.9 |
| Bilateral base, boost 5 | 20.65 | 60 | 180 |
The step of the figure, 160 by 100 pixels, from 60 to 180, with a fine pattern of steps of 3 between -6 and 6 everywhere. The texture deviation is the standard deviation in columns 10 to 39, far from the edge. The edge means average the 3 columns on each side of the edge, where the input means are about 60 and 180. Values past 0 or 255 are clipped. The other settings are the defaults: r = 16 and eps = 0.01 for the guided base, sigma_s = 16 and sigma_r = 25.5 for the bilateral one.
Both bases make the texture about 5 times stronger. Next to the edge, the guided base pushes the dark side from 60 down to about 12. It pushes the bright side from 180 up to about 229. These are the bands in the figure. The bilateral base leaves the edge as it was.
Complexity¶
- Time: \(O(N) or O(N log N)\)
- Memory: \(O(N)\)
N is the number of pixels. The guided base costs O(N) for any radius. The bilateral base filters about 10 levels through the FFT.
Artifacts and limitations¶
Limitations
- Noise is detail too, so it is boosted as well.
- With the guided base, part of a strong step goes into the detail layer and is boosted into dark and bright bands.
Artifacts
- Bands along strong edges with the guided base.
- Gradient reversal near soft edges with the bilateral base, as He, Sun and Tang show.
Tests¶
The fixture cases come from a NumPy reference. Its base is the guided filter of the guided filter's fixtures, checked there against OpenCV, or the piecewise bilateral filter of the tone mapping fixtures. Unit tests check that a boost of 1 gives the input back, a boost of 0 gives the base, and a flat image stays flat.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/enhancement/detail.test.ts
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.
- Durand, F. and Dorsey, J. Fast bilateral filtering for the display of high-dynamic-range images. ACM Transactions on Graphics 21(3), 257 to 266, 2002. DOI 10.1145/566654.566574.