Skip to content

Range filter

Fact Value
Also called Local range, Max minus min
Status Implemented and tested
Family Adaptive and statistical filters
Engine Browser
Type Nonlinear, statistical, order statistic difference
Output Features
Operator shown as estimator (exact)
Support radius floor(size / 2)
Code web/src/core/filters/adaptive/local-stats.ts, localRangeFilter
Operator code web/src/core/filters/adaptive/local-stats.ts, localRangeFilter
Channels RGB, Brightness only

Overview

The range filter maps the difference between the largest and the smallest value in the window around each pixel. It is a quick measure of local contrast. With a flat square window it is the same as the morphological gradient: dilation minus erosion.

Two panels: the Noise patches test image and its 5x5 range map, black in the clean quadrant and bright in the three noisy quadrants. Two panels: the Noise patches test image and its 5x5 range map, black in the clean quadrant and bright in the three noisy quadrants.
The range reacts to every single noisy pixel, so the noisy quadrants turn bright.

How it works

  1. Take the window around the pixel.
  2. Find its largest and smallest values.
  3. Write the difference.

Mathematical foundation

\[ R(x,y) = \max_{w \in W_{xy}} w - \min_{w \in W_{xy}} w \]

The formula shows the default parameter values.

where

  • \(W_{xy}\): The window around (x, y)

The largest minus the smallest value of the window. It is the max filter minus the min filter, the morphological gradient with a flat square. A single outlier in the window sets it.

The range is the last order statistic minus the first. One extreme value in the window sets it, so it is much more sensitive to noise than the standard deviation. Its values are in levels, from 0 to 255. 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

  1. Pad the image with the border mode.
  2. Find the min and max of every window with separable row and column passes.
  3. Subtract the min from the max.
  4. 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 Clip to 0 to 255 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

localRangeFilter in web/src/core/filters/adaptive/local-stats.ts subtracts the min plane from the max plane. Both come from extremePlane, the separable code of the min and max filters. 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

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

The largest value is 70 and the smallest 49, so the range is 21.

Complexity

  • Time: \(O(N\,k)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel and k the window width, from the separable min and max passes.

Artifacts and limitations

Limitations

  • Very sensitive to noise, because one extreme value sets the result.
  • Thick responses on edges, as wide as the window.

Artifacts

  • Square blobs around isolated noise pixels.

Tests

The reference is scipy.ndimage.maximum_filter minus scipy.ndimage.minimum_filter, for window sizes 3 and 5 and all five border modes.

References