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.
How it works¶
- Take the window around the pixel.
- Find its largest and smallest values.
- Write the difference.
Mathematical foundation¶
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¶
- Pad the image with the border mode.
- Find the min and max of every window with separable row and column passes.
- Subtract the min from the max.
- 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¶
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¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 9.6, morphological gradient.
- SciPy documentation, scipy.ndimage.maximum_filter and scipy.ndimage.minimum_filter. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.maximum_filter.html