Adaptive smoothing¶
| Fact | Value |
|---|---|
| Status | Implemented and tested |
| Family | Adaptive and statistical filters |
| Engine | Browser |
| Type | Nonlinear, iterative, gradient-weighted averaging |
| Output | Image |
| Operator shown as | iterative (exact) |
| Support radius | 2 iterations |
| Code | web/src/core/filters/adaptive/adaptive-smoothing.ts, adaptiveSmoothingFilter |
| Operator code | web/src/core/filters/adaptive/adaptive-smoothing.ts, adaptiveSmoothingStep |
| Channels | RGB, Brightness only |
Overview¶
Adaptive smoothing repeats a small weighted average. In each step, every pixel of the 3x3 window is weighted by how flat the image is at that pixel. Pixels on an edge have a large gradient and get almost no weight, so the averages on the two sides of an edge do not mix. After a number of steps, regions are smooth and their borders are still sharp.
How it works¶
- Compute the gradient at every pixel from central differences.
- Turn each gradient into a weight that is near 1 for small gradients and near 0 for large ones.
- Replace each pixel by the weighted mean of its 3x3 window, and repeat.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(G\): The gradient from central differences
- \(w\): The weight of a pixel
- \(k\): The edge scale
- \(t\): The step number
G is the gradient from central differences, G_x = (I(x + 1, y) - I(x - 1, y)) / 2 and the same for y. Each pixel of the 3x3 window is weighted by its own gradient. Pixels on an edge have a large gradient and almost no weight, so the mean on each side of an edge uses only pixels from that side. Repeating the step spreads the smoothing within regions.
Saint-Marc, Chen and Medioni weight each pixel by exp(-|G|^2 / (2 k^2)). A step is a normalized convolution with data-dependent weights, close in spirit to anisotropic diffusion. With a very large k all weights are 1, and each step is a 3x3 box mean, so many steps give a Gaussian blur. With a finite k the smoothing stops at edges, and many steps give flat regions with sharp borders.
Kernel or operator¶
The Inspector shows the update step with the current k and the number of iterations. The worked example below computes the weights for one pixel next to an edge.
Algorithm¶
- Extend the image by two pixels per iteration with the border mode.
- Compute the gradient of every pixel and its weight.
- Replace each pixel by the weighted mean of its 3x3 window.
- Repeat, then cut the extension off.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Iterations iterations |
10 | 1 to 50 | How many times the step runs. |
Edge scale k k |
10 | 1 to 100 | The gradient size, in levels per pixel, at which the weight falls off. |
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¶
adaptiveSmoothingStep in web/src/core/filters/adaptive/adaptive-smoothing.ts computes the weights of a whole plane, then the weighted means. Each step reads two pixels around each pixel, one for the gradient and one for the mean. So the plane is extended once by two pixels per step with the border mode, all steps run, and the frame is cut off. 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 mapping 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¶
One step for the center pixel, value 51, with k = 10. The weights of its 3x3 window come from each pixel's own gradient:
The middle and right columns touch the edge. Their central differences reach across it, about 75 per pixel, so their weights are almost 0. Only the left column counts, and the output is the weighted mean, 50.333, a dark value.
Complexity¶
- Time: \(O(N\,T)\)
- Memory: \(O(N)\)
N is the number of pixels per channel and T the number of iterations.
Artifacts and limitations¶
Limitations
- Many iterations are slow.
- Noise with large gradients is kept like an edge.
Artifacts
- Flat patches with sharp borders after many iterations.
Tests¶
The reference runs the same steps with NumPy on the extended plane. Before it is used, the script checks that a huge k gives a repeated 3x3 box mean. The test covers two settings for all five border modes.
References¶
- Saint-Marc, P., Chen, J.-S. and Medioni, G. Adaptive smoothing, a general tool for early vision. IEEE Transactions on Pattern Analysis and Machine Intelligence 13(6), 514 to 529, 1991. DOI 10.1109/34.87339.