Max filter¶
| Fact | Value |
|---|---|
| Also called | Maximum filter, Local maximum |
| 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, maxFilter |
| Operator code | web/src/core/filters/nonlinear/min-max.ts, extremeRank |
| Channels | RGB, Brightness only |
Overview¶
The max filter replaces each pixel with the largest value of its window. Black specks disappear, because a window almost always holds a brighter value. Bright areas grow by the window radius, and dark areas shrink by the same amount.
How it works¶
- Take the square window around the pixel.
- Find its largest value.
- Write that value as the new pixel.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input channel value
- \(I_{\text{out}}\): Output channel value
- \((x, y)\): Pixel position
- \(r\): Window radius in pixels
The output is the largest value in the window. It is the last value of the sorted window, so it is the rank-order filter with the highest rank. It is also gray dilation with a flat square structuring element.
The maximum is the last order statistic. It is also gray dilation with a flat square structuring element. Like the min filter, it is separable: the max over the columns of the max over the rows. A min filter followed by a max filter of the same size is a morphological opening, which removes bright details smaller than the window.
Kernel or operator¶
The Inspector shows the window, the number of values and the rank that is used. The Rank stepper in the Learn panel shows the sorted window at any pixel and the value the filter gives there. For the max filter the rank is the number of values, the last one.
Algorithm¶
- Pad the image with the border mode.
- For each row, find the largest value in each run of size pixels.
- For each column of that result, find the largest value in each run of size pixels.
- Write it 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¶
extremePlane in web/src/core/filters/nonlinear/window.ts runs a row pass and a column pass. The morphology filters of Phase 5d share this code. Values keep their fractions. 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 max filter takes the last value, 255. The salt pixel 255 wins, so this window turns white. The pepper pixel 0 has no effect.
Complexity¶
- Time: \(O(N\,k)\)
- Memory: \(O(N)\)
N is the number of pixels per channel and k is the window width. A row pass and a column pass of k values each give the maximum of all k^2 values.
Artifacts and limitations¶
Limitations
- Makes salt noise worse. Bright specks grow into squares.
- Shrinks dark objects and thin dark lines.
Artifacts
- Square bright blocks around bright pixels with large windows.
Tests¶
The filter is compared with scipy.ndimage.maximum_filter for window sizes 3 and 5 and all five border modes. A unit test checks that the sliding-histogram rank code gives the same result with the last rank.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/nonlinear/nonlinear5b.test.ts
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 5.3, order-statistic filters.
- SciPy documentation, scipy.ndimage.maximum_filter. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.maximum_filter.html