Local variance filter¶
| Fact | Value |
|---|---|
| Also called | Variance filter |
| Status | Implemented and tested |
| Family | Adaptive and statistical filters |
| Engine | Browser |
| Type | Nonlinear, statistical, local second moment |
| Output | Features |
| Operator shown as | estimator (exact) |
| Support radius | floor(size / 2) |
| Code | web/src/core/filters/adaptive/local-stats.ts, localVarianceFilter |
| Operator code | web/src/core/filters/adaptive/local-stats.ts, localVarianceFilter |
| Channels | RGB, Brightness only |
Overview¶
The local variance filter maps how much the values in the window around each pixel spread around their mean. Flat areas give 0. Edges, texture and noise give high values. It is the quantity that the adaptive Wiener and Lee filters use to decide how much to smooth.
How it works¶
- Take the window around the pixel.
- Find the mean of its values and the mean of their squares.
- Write the mean of the squares minus the square of the mean.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(W_{xy}\): The window around (x, y)
- \(n\): Number of values in the window
The population variance of the n values in the window W_xy: the mean of the squares minus the square of the mean. It is 0 in flat areas and large on edges and texture. The unit is levels squared.
The variance is the second central moment. Written as E[w^2] - E[w]^2, it needs only two running sums, so the cost does not depend on the window size. It is the population variance, divided by n and not by n - 1. Its unit is levels squared, so values reach 16256 for a window that is half black and half white. The result is a new quantity, not an image, so it goes through the output mapping. With the brightness scope the map is gray.
Kernel or operator¶
There is no kernel. The Inspector shows the window and the statistic. The Rank stepper in the Learn panel shows the window values at any pixel, sorted, next to the value the filter gives there.
Algorithm¶
- Pad the image with the border mode.
- Sum the values and their squares over each window with running sums in float64.
- Divide both by n, and subtract the squared mean from the mean square.
- Map the result to 8 bits with the output mapping.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Window size size |
5 | 3 to 25, odd px | Width and height of the square window. |
Output mapping outputMapping |
Stretch to full range | 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. |
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¶
localMoments in web/src/core/filters/adaptive/local-stats.ts sums the values and their squares with running sums in float64, which avoids the rounding loss of float32 when the two means are subtracted. Small negative results from rounding are set to 0. 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¶
The mean is 59.3333 and the mean of the squares is 3560. The variance is 3560 - 59.3333^2 = 39.556 squared levels.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels per channel. Running sums make the cost independent of the window size.
Artifacts and limitations¶
Limitations
- Mixes edges and texture. Both have a high variance.
- The unit is levels squared, so values are hard to read without the mapping.
Artifacts
- Bright bands along edges, as wide as the window.
Tests¶
The reference takes numpy.var of each window. Before it is used, the script checks it against scipy.ndimage.generic_filter with numpy.var. The test covers window sizes 3 and 5 for all five border modes.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 3.3, local histogram statistics.
- SciPy documentation, scipy.ndimage.generic_filter. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.generic_filter.html