Skip to content

Harmonic 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, harmonicMeanFilter
Operator code web/src/core/filters/nonlinear/means.ts, harmonicMeanFilter
Channels RGB, Brightness only

Overview

The harmonic mean filter replaces each pixel with n divided by the sum of the inverses of its n window values. Dark values have large inverses, so they dominate. White specks hardly change the result, but a single black pixel makes it 0.

Four panels in two rows: disks with white specks and a clean harmonic mean result, then disks with black specks and a harmonic mean result with black squares. Four panels in two rows: disks with white specks and a clean harmonic mean result, then disks with black specks and a harmonic mean result with black squares.
The harmonic mean removes white specks but turns black specks into black squares.

How it works

  1. Take the square window around the pixel.
  2. Add up 1 divided by each value.
  3. Write the number of values divided by that sum.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \frac{n}{\displaystyle\sum_{(s,t) \in S_{xy}} \frac{1}{I(s,t)}} \]

The formula shows the default parameter values.

where

  • \(S_{xy}\): The window around pixel (x, y)
  • \(n\): Number of values in the window

n divided by the sum of the inverses of the window values. Small values have large inverses, so they dominate, and a bright speck has little effect. If any value is 0, its inverse is infinite and the output is 0.

For positive values, the harmonic mean is never larger than the geometric mean, which is never larger than the arithmetic mean. The harmonic mean is the contraharmonic mean with Q = -1. A value of 0 has an infinite inverse, so a window that holds a 0 gives 0.

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

  1. Pad the image with the border mode.
  2. Take 1 / v for every value v above 0, and mark the zeros.
  3. Sum the inverses and the zero marks over each window with running sums.
  4. Write 0 where a window holds a 0, and n divided by the sum of inverses 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

harmonicMeanFilter in web/src/core/filters/nonlinear/means.ts sums the inverses of the values above 0 and counts the zeros with running sums in float64. 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

\[ W = \begin{bmatrix} 52 & 61 & 58 \\ 49 & 70 & 63 \\ 55 & 66 & 60 \end{bmatrix} \]

The sum of the inverses is 0.15343. Then 9 divided by that sum is 58.66. The geometric mean is 59 and the arithmetic mean 59.33. The harmonic mean is the smallest of the three.

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

  • Fails on pepper noise. A single 0 makes the window 0.
  • Never larger than the geometric mean, so it darkens more than the box filter.

Artifacts

  • Black squares around black pixels. Bright thin lines get thinner.

Tests

The reference divides n by the sum of inverses of each NumPy window. Before it is used, it is checked against scipy.stats.hmean on windows without zeros. The test input has three zeros, for window sizes 3 and 5 and three border modes.

References