Skip to content

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

Overview

The contraharmonic mean filter weights each window value by its own power Q. With Q above 0, bright values weigh more and black specks hardly count. With Q below 0, dark values weigh more and white specks hardly count. It removes one kind of impulse noise at a time.

Four panels in two rows: disks with black specks and a clean result with Q = 1.5, then disks with white specks and a clean result with Q = -1.5. Four panels in two rows: disks with black specks and a clean result with Q = 1.5, then disks with white specks and a clean result with Q = -1.5.
Q = 1.5 removes black specks. Q = -1.5 removes white specks.

How it works

  1. Raise every window value to the power Q.
  2. Use those powers as weights for a weighted mean of the window values.
  3. Write the weighted mean.

Mathematical foundation

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

The formula shows the default parameter values.

where

  • \(S_{xy}\): The window around pixel (x, y)
  • \(Q\): The order of the filter

Each value is weighted by its own power Q. With Q above 0 the bright values weigh more, so black specks hardly count. With Q below 0 the dark values weigh more, so white specks hardly count. When Q is below 0 and the window holds a 0, the output is 0, the limit of the formula.

The formula is a weighted mean with the weights I to the power Q. Q = 0 gives the arithmetic mean and Q = -1 gives the harmonic mean. As Q grows toward plus infinity, the largest value dominates and the filter approaches the max filter. Toward minus infinity it approaches the min filter. When Q is below 0, a value of 0 gets an infinite weight. The limit of the formula is then 0, which is what the filter writes.

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. Compute v to the power Q + 1 and v to the power Q for every value v.
  3. Sum both over each window with running sums, and count the zeros when Q is below 0.
  4. Write the ratio of the two sums, or 0 when Q is below 0 and the window holds a 0.

Parameters

Parameter Default Values Meaning
Window size size 3 3 to 15, odd px Width and height of the square window.
Order Q order 1.5 -5 to 5 The power Q. Positive values favor bright values, negative values favor dark ones.
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

contraharmonicPlane in web/src/core/filters/nonlinear/means.ts sums the two powers of every value with running sums in float64, and counts the zeros when Q is below 0. A window whose bottom sum is 0, which means it holds only zeros, gives 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 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 & 0 & 63 \\ 55 & 66 & 60 \end{bmatrix} \]

The center is a pepper pixel. With Q = 1.5, each value is weighted by its own power 1.5, and the pepper pixel gets weight 0. The output is 58.73, close to the 8 ordinary values. With Q = -1.5, the same window gives 0, because the 0 dominates.

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

  • The sign of Q must match the noise. The wrong sign makes the noise much worse.
  • It cannot remove salt and pepper noise at the same time.

Artifacts

  • With the wrong sign of Q, specks grow into squares.
  • Dark edges thin out with positive Q, and bright edges with negative Q.

Tests

The reference computes both power sums of each NumPy window. Before it is used, it is checked against two special cases: Q = 0 must equal scipy.ndimage.uniform_filter, and Q = -1 must equal the harmonic mean. The test covers four orders, including 0 and negative ones, on an input with three zeros, for window sizes 3 and 5 and three border modes.

References

  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 5.3, contraharmonic mean filter.