Vector median filter¶
| Fact | Value |
|---|---|
| Status | Implemented and tested |
| Family | Nonlinear spatial filters |
| Engine | Browser |
| Type | Nonlinear, spatial, vector order statistic |
| Output | Image |
| Operator shown as | rank (exact) |
| Support radius | floor(size / 2) |
| Code | web/src/core/filters/nonlinear/vector-median.ts, vectorMedianFilter |
| Operator code | web/src/core/filters/nonlinear/vector-median.ts, vectorMedianIndex |
| Channels | RGB |
Overview¶
The vector median filter treats each pixel as one color, a vector of red, green and blue. For each window it picks the color that is closest, in total, to all the other colors of the window. The output is always a color that is in the window. A per-channel median can mix the red of one pixel with the green of another, which makes new colors at color edges. The vector median cannot.
How it works¶
- Collect the colors of the window.
- For each color, add up its distances to all the other window colors.
- Write the color with the smallest total.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(W\): The color vectors of the window
- \(v_i, v_j\): Color vectors in the window
- \(\lVert\cdot\rVert\): The L1 or L2 distance
W holds the color vectors (R, G, B) of the window. For each one, the filter adds up its distances to all the others. The vector with the smallest sum is the output. On a tie, the first vector in reading order wins. For a gray image, this is the plain median.
For numbers, the median is the value that minimizes the sum of absolute differences to all samples. The vector median of Astola, Haavisto and Neuvo (1990) uses the same idea with vector distances, and keeps the answer among the samples. With the L2 distance it measures straight lines between colors in RGB space, and with L1 it adds up the channel differences. For a gray image, both give the plain median.
Kernel or operator¶
There is no kernel. The Inspector shows the window and the rule. The Learn panel shows a line profile, and the worked example below adds up the distances for one window.
Algorithm¶
- Pad the image with the border mode.
- For each pixel, collect the n color vectors of its window.
- For every pair of vectors, compute their distance and add it to both sums.
- Write the vector with the smallest sum.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Window size size |
3 | 3 to 7, odd px | Width and height of the square window. |
Distance norm |
L2 (straight line) | L2 (straight line), L1 (sum of channel differences) | How the distance between two colors is measured. |
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. |
Implementation¶
vectorMedianFilter in web/src/core/filters/nonlinear/vector-median.ts collects the window colors, computes each pairwise distance once and adds it to both sums. On a tie, the first color in reading order wins. The window is limited to 7x7, because the cost grows with the fourth power of the size. It is a color filter: the framework in web/src/core/filters/run.ts hands it the red, green and blue planes together, with the border mode, and keeps alpha. 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¶
| Position | Color (R, G, B) | Sum of L2 distances |
|---|---|---|
| 1 | (200, 40, 40) | 820.2 |
| 2 | (210, 50, 40) | 830.8 |
| 3 | (190, 40, 50) | 825.5 |
| 4 | (205, 45, 45) | 807.7 |
| 5 | (255, 255, 255) | 2422.2 |
| 6 | (40, 200, 40) | 1661.7 |
| 7 | (195, 45, 40) | 803.9 |
| 8 | (50, 210, 45) | 1650.8 |
| 9 | (200, 50, 50) | 797.3 |
A 3x3 window on a red area, with a white speck in the center and two green pixels. The smallest sum belongs to (200, 50, 50), so that is the output. The per-channel median gives (200, 50, 45), a color that is in none of the nine pixels.
Complexity¶
- Time: \(O(N\,k^4)\)
- Memory: \(O(N)\)
N is the number of pixels and k is the window width. Each window has k^2 vectors and about k^4 / 2 pairs.
Artifacts and limitations¶
Limitations
- Slow for large windows, because it compares every pair of window colors.
- Works on RGB only.
Artifacts
- Small patches of one color in textured areas with large windows.
Tests¶
The reference computes all pairwise distances with NumPy on a color input with white, black and one-channel impulses. Before it is used, the script checks that both distances give scipy.ndimage.median_filter on a gray image. The test covers 3x3 with both distances and 5x5 with L2, for all five border modes.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/nonlinear/nonlinear5b.test.ts
References¶
- Astola, J., Haavisto, P. and Neuvo, Y. Vector median filters. Proceedings of the IEEE 78(4), 678 to 689, 1990. DOI 10.1109/5.54807.