Midpoint filter¶
| Fact | Value |
|---|---|
| Also called | Mid-range filter |
| Status | Implemented and tested |
| Family | Nonlinear spatial filters |
| Engine | Browser |
| Type | Nonlinear, spatial, order statistic |
| Output | Image |
| Operator shown as | rank (exact) |
| Support radius | floor(size / 2) |
| Code | web/src/core/filters/nonlinear/min-max.ts, midpointFilter |
| Operator code | web/src/core/filters/nonlinear/min-max.ts, midpointFilter |
| Channels | RGB, Brightness only |
Overview¶
The midpoint filter replaces each pixel with the value halfway between the smallest and the largest value of its window. It suits noise whose values spread evenly over a range, such as uniform noise. It fails on impulse noise, because one speck decides half of the result.
How it works¶
- Take the square window around the pixel.
- Find its smallest and largest values.
- Write the value halfway between them.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input channel value
- \(I_{\text{out}}\): Output channel value
- \(S_{xy}\): The window around pixel (x, y)
S_xy is the size by size window around (x, y). The output is the mean of the first and last values of the sorted window. It uses only these two values, so a single extreme value moves the result by half of its distance.
The midpoint, also called the mid-range, uses only two order statistics, the first and the last. Take noise that is uniform on an interval around the true value. Then the midpoint of a sample estimates the center better than the sample mean, because the edges of the range carry the most information. For noise with long tails, such as Gaussian or impulse noise, it is worse than the mean.
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.
- Find the minimum of each window with a row pass and a column pass.
- Find the maximum of each window the same way.
- Write the mean of the two to the output pixel.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Window size size |
3 | 3 to 25, 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¶
The filter runs the min and max passes of extremePlane in web/src/core/filters/nonlinear/window.ts and averages them. 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¶
Sorted: 0, 49, 52, 55, 58, 60, 61, 63, 255. The midpoint is (0 + 255) / 2 = 127.5. Both impulses decide the result, and none of the seven ordinary values count. Without the two impulses, the window values run from 49 to 63, and the midpoint would be 56.
Complexity¶
- Time: \(O(N\,k)\)
- Memory: \(O(N)\)
N is the number of pixels per channel and k is the window width. It runs the min and max filters and averages them.
Artifacts and limitations¶
Limitations
- Very sensitive to impulse noise. One salt or pepper pixel pulls the result halfway.
- Blurs edges, because a window on an edge gives the halfway value.
Artifacts
- Blocky patches around isolated bright or dark pixels.
Tests¶
The filter is compared with the mean of scipy.ndimage.minimum_filter and scipy.ndimage.maximum_filter for window sizes 3 and 5 and all five border modes. The results match within float rounding.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 5.3, order-statistic filters.