Geometric mean filter¶
| Fact | Value |
|---|---|
| Status | Implemented and tested |
| Family | Nonlinear spatial filters |
| Engine | Browser |
| Type | Nonlinear, spatial, mean filter |
| Output | Image |
| Operator shown as | estimator (exact) |
| Support radius | floor(size / 2) |
| Code | web/src/core/filters/nonlinear/means.ts, geometricMeanFilter |
| Operator code | web/src/core/filters/nonlinear/means.ts, geometricMeanFilter |
| Channels | RGB, Brightness only |
Overview¶
The geometric mean filter replaces each pixel with the n-th root of the product of its n window values. It smooths about as much as the box filter, and loses a little less detail. A single 0 in the window makes the product 0, so black specks grow into black squares.
How it works¶
- Take the square window around the pixel.
- Multiply its values together.
- Write the n-th root of the product.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(S_{xy}\): The window around pixel (x, y)
- \(n\): Number of values in the window
The product of the n window values, to the power 1 / n. It runs as the exponential of the mean of the logarithms, which does not overflow. If any value in the window is 0, the product is 0, so the output is 0.
The geometric mean is the exponential of the mean of the logarithms. By the inequality of arithmetic and geometric means, it is never larger than the arithmetic mean, and equal only when all values are the same. So the filter darkens slightly where the window varies. The logarithm of 0 is minus infinity, so a window that holds a 0 gives 0. That is also the limit of the product.
Kernel or operator¶
There is no kernel, because the output is not a weighted sum. The Inspector shows the window and the statistic. The Rank stepper in the Learn panel shows the window at any pixel, sorted, next to the value the filter gives there.
Algorithm¶
- Pad the image with the border mode.
- Take the natural logarithm of every value above 0, and mark the zeros.
- Sum the logarithms and the zero marks over each window with running sums.
- Write 0 where a window holds a 0, and the exponential of the mean logarithm elsewhere.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Window size size |
3 | 3 to 15, odd px | Width and height of the square window. |
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¶
geometricMeanFilter in web/src/core/filters/nonlinear/means.ts sums the logarithms of the values above 0 and counts the zeros with running sums in float64, so the cost does not grow with the window. In the constant border mode, the positions outside the image count as zeros. 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¶
The mean of the natural logarithms is 4.0775. Its exponential is 59. The arithmetic mean of the same window is 59.33, slightly larger, as it always is. If one value were 0, the output would be 0.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels per channel. Running sums make the cost per pixel independent of the window size.
Artifacts and limitations¶
Limitations
- A single 0 makes the whole window 0, so pepper noise grows into black squares.
- Always gives a value at or below the arithmetic mean, so it darkens slightly.
Artifacts
- Black squares around black pixels.
Tests¶
The reference takes the exponential of the mean logarithm of each NumPy window. Before it is used, it is checked against scipy.stats.gmean on windows without zeros. The test input has three zeros, so the zero rule is tested too, for window sizes 3 and 5 and three border modes.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/nonlinear/nonlinear5b.test.ts
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 5.3, geometric mean filter.
- SciPy documentation, scipy.stats.gmean. https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gmean.html