Mode filter¶
| Fact | Value |
|---|---|
| Also called | Majority filter, Modal 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/mode.ts, modeFilter |
| Operator code | web/src/core/filters/nonlinear/mode.ts, histogramMode |
| Channels | RGB, Brightness only |
Overview¶
The mode filter replaces each pixel with the value that occurs most often in its window. It never makes new values, so it suits images with few distinct values, such as label maps, posterized images and masks. For an image with two values it is a majority vote.
How it works¶
- Take the square window around the pixel.
- Count how often each value occurs.
- Write the most common value. On a tie, write the smallest of the tied values.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(h_{xy}(v)\): How often value v occurs in the window
- \(v, u\): Whole values from 0 to 255
h_xy(v) counts how often the whole value v occurs in the window around (x, y). The output is the value with the highest count. When several values share the highest count, the smallest of them wins, as in skimage.filters.rank.modal.
The mode is the peak of the local histogram. Unlike the mean and the median, it needs no order between the values, so it also works for class labels. On a photo with many distinct values, most values occur once. Then every value ties, and the tie rule picks the smallest, so the filter acts like a min filter. That is why it suits images with few values.
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.
- Keep a 256-bin histogram of the window as it slides along each row.
- Find the bin with the highest count, the lowest one on a tie.
- Write its value to the output pixel.
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¶
histogramPlane in web/src/core/filters/nonlinear/window.ts keeps a 256-bin histogram of the window as it slides along each row. histogramMode scans the 256 bins for the highest count. Values are whole numbers. 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¶
The value 30 occurs 4 times, 220 occurs 4 times and 80 once. 30 and 220 tie, and the smaller value wins, so the output is 30. The median of the same window is 80.
Complexity¶
- Time: \(O(N\,(k + 256))\)
- Memory: \(O(N)\)
N is the number of pixels per channel and k is the window width. Each step updates one column of the histogram and scans its 256 bins.
Artifacts and limitations¶
Limitations
- On photos with many distinct values, most values occur once, and the result is noisy.
- The tie rule favors dark values.
Artifacts
- Blocky or jagged region boundaries with large windows.
Tests¶
The reference counts the values of each window with NumPy, for window sizes 3 and 5 and all five border modes. Before it is used, it is checked against skimage.filters.rank.modal away from the image edge, where both see the same pixels. The results match exactly. A unit test checks the tie rule.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/nonlinear/nonlinear5b.test.ts
References¶
- scikit-image documentation, skimage.filters.rank.modal. https://scikit-image.org/docs/stable/api/skimage.filters.rank.html#skimage.filters.rank.modal
- Huang, T. S., Yang, G. J. and Tang, G. Y. A fast two-dimensional median filtering algorithm. IEEE Transactions on Acoustics, Speech, and Signal Processing, 27(1), 1979. DOI 10.1109/TASSP.1979.1163188.