2D LMS adaptive filter¶
| Fact | Value |
|---|---|
| Also called | LMS adaptive filter, TDLMS, Two-dimensional LMS predictor |
| Status | Implemented and tested |
| Family | Adaptive and statistical filters |
| Engine | Browser |
| Type | Nonlinear, statistical, adaptive linear prediction |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | the whole image |
| Code | web/src/core/filters/adaptive/scan.ts, lms2dFilter |
| Operator code | web/src/core/filters/adaptive/scan.ts, lms2dFilter |
| Channels | RGB, Brightness only |
Overview¶
The 2D LMS filter predicts each pixel from the pixels around it, with weights that it learns while it scans the image. It starts with the plain mean of the neighbors. After every pixel it compares its guess with the pixel, and nudges the weights to make the next guess better. This is the least mean squares (LMS) update that Hadhoud and Thomas brought to images.
Noise that the neighbors do not share cannot be predicted, so the prediction is a denoised image. Where the image has a direction, such as stripes, the weights learn to follow it, and the filter beats any fixed average.
How it works¶
- Guess the pixel from its neighbors with the current weights. At first the guess is their plain mean.
- Compare the guess with the pixel. The difference is the error.
- Nudge each weight in the direction that makes the error smaller.
- Go on to the next pixel, row by row.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(d(n)\): The pixel, from 0 to 1
- \(\mathbf{x}(n)\): Its neighbors in the window, without the center
- \(\mathbf{w}(n)\): The weights at pixel n
- \(e(n)\): The prediction error
- \(\mu\): The step size
n runs over the pixels in raster order. d is the pixel, scaled to 0 to 1. x holds its neighbors in the window without the center. The weights w start as the plain mean of the neighbors. After each pixel they move by the error e times x. Hadhoud and Thomas brought this LMS update to two dimensions. Here the step is divided by the energy of x, as in normalized LMS. That is our choice, so that one step size suits any image. A tiny epsilon avoids a division by zero.
LMS follows the gradient of the squared error, one pixel at a time. The error is d - w x, so the gradient of its square with respect to w is -2 e x. A small step against it lowers the error for pixels like this one. Dividing the step by the energy of x makes the step size independent of how bright the image is.
The center pixel is left out of the window on purpose. With the center in the window, the best weights would copy it, noise and all.
Kernel or operator¶
There is no fixed kernel. The weights change at every pixel. The Inspector shows the prediction and the update with the current values.
Algorithm¶
- Start with weights that give the mean of the neighbors.
- For each pixel in raster order, predict it from its neighbors.
- Write the prediction, or the error.
- Move the weights by the error times the neighbors, divided by their energy.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Window size size |
3 | 3 to 7, odd px | The window of neighbors that predict the center, which itself is left out. |
Step size mu mu |
0.05 | 0 to 1 | How far the weights move after each pixel. The update is normalized by the energy of the neighbors, so 0 to 1 is stable. |
Output output |
Prediction (denoised) | Prediction (denoised), Prediction error (around mid gray) | The prediction from the neighbors, or the pixel minus its prediction, shown around 128. |
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¶
lmsPlane in web/src/core/filters/adaptive/scan.ts scans the image in raster order, row by row from the top left. The weights carry over from each pixel to the next, also from the end of one row to the start of the next. Values are scaled to 0 to 1 before the update. The scan cannot be cut into tiles, so the filter runs on the whole image at once.
Worked example¶
| Method | Disks (dB) | Stripes (dB) |
|---|---|---|
| Noisy, sigma 20 | 22.60 | 22.15 |
| Box 3x3 | 26.54 | 17.97 |
| Mean of the 8 neighbors (2D LMS, mu 0) | 25.63 | 16.96 |
| 2D LMS, 3x3, mu 0.05 | 25.05 | 24.83 |
| 2D RLS, 3x3, lambda 0.999 | 25.77 | 25.89 |
| Kalman, sigma 20 | 26.48 | 23.15 |
PSNR against the clean images, 96 x 96: the Disks and dots test image, and diagonal stripes with a period of 6 pixels. Gaussian noise of sigma 20, seeds 41 and 42.
On the disks, the fixed box filter wins, since the image is flat except at the edges. On the stripes, every fixed average blurs the pattern away, while LMS learns the direction of the stripes and keeps them. Adaptive filters pay off where the image has a structure that a fixed kernel does not fit.
Complexity¶
- Time: \(O(N k^2)\)
- Memory: \(O(N)\)
N is the number of pixels per channel and k the window width. The scan runs in one pass and cannot be split into tiles, so the filter is global.
Artifacts and limitations¶
Limitations
- The result depends on the scan order. The weights learned at the top shape the result further down.
- It smooths edges that cross the window, like a linear filter.
- It cannot run in tiles, so large images take a while.
Artifacts
- Streaks along the scan direction where the weights change fast.
Tests¶
A NumPy loop of the same update, in tools/fixtures_scan.py, gives the reference for window sizes 3 and 5, both outputs and three border modes. Before it is used, the script checks that a step size of 0 gives the mean of the eight neighbors.
References¶
- Hadhoud, M. M., and Thomas, D. W. The two-dimensional adaptive LMS (TDLMS) algorithm. IEEE Transactions on Circuits and Systems 35(5), 485 to 494, 1988. DOI 10.1109/31.1775.