Skip to content

2D RLS adaptive filter

Fact Value
Also called RLS adaptive filter, Recursive least squares 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, rls2dFilter
Operator code web/src/core/filters/adaptive/scan.ts, rls2dFilter
Channels RGB, Brightness only

Overview

The 2D RLS filter predicts each pixel from its neighbors, like the 2D LMS filter. Its update is recursive least squares (RLS). At every pixel, the weights are the best least-squares fit to all pixels before it, with older pixels counting a little less. It gets there without solving the fit again: it carries the inverse of the correlation matrix, P, from pixel to pixel.

RLS learns within a few pixels, where LMS needs thousands. The price is more work per pixel.

Three panels: the Disks and dots test image with strong noise, the RLS prediction with much less noise, and the RLS error, gray noise with the disk outlines. Three panels: the Disks and dots test image with strong noise, the RLS prediction with much less noise, and the RLS error, gray noise with the disk outlines.
RLS learns within the first rows. Its error shows the noise and the outlines.

How it works

  1. Guess the pixel from its neighbors with the current weights.
  2. Compare the guess with the pixel.
  3. Move the weights to the best fit of all pixels so far, using a running summary instead of solving again.
  4. Go on to the next pixel, row by row.

Mathematical foundation

\[ \hat{d}(n) = \mathbf{w}^{\top} \mathbf{x},\quad \mathbf{k} = \frac{P \mathbf{x}}{\lambda + \mathbf{x}^{\top} P \mathbf{x}},\quad \mathbf{w} \leftarrow \mathbf{w} + \mathbf{k}\, e(n),\quad P \leftarrow \frac{P - \mathbf{k}\, \mathbf{x}^{\top} P}{\lambda} \]

The formula shows the default parameter values.

where

  • \(d(n)\): The pixel, from 0 to 1
  • \(\mathbf{x}\): Its neighbors in the window, without the center
  • \(\mathbf{w}\): The weights
  • \(P\): The inverse correlation matrix, started as I / delta
  • \(\mathbf{k}\): The gain vector
  • \(\lambda\): The forgetting factor

d is the pixel, scaled to 0 to 1, and x holds its neighbors in the window without the center. P tracks the inverse of the weighted correlation matrix of the neighbors. The gain k says how far each weight moves for this error. With lambda = 1 the weights at each pixel solve the least-squares fit to all pixels before it, regularized toward the start weights by delta. In flat areas the neighbors carry little new information, and with forgetting P grows without bound until the weights explode, which is called windup. After each step P is made symmetric again and its trace is capped at its starting trace. This safeguard is PixelNexus's own.

With a forgetting factor below 1, a pixel n steps back counts lambda to the power n. That lets the weights follow changes in the image. It also has a danger. In a flat area the neighbors are all alike, so the pixels add no new information about most directions of w. P then grows by 1 / lambda at every step in those directions. After a few thousand flat pixels it is huge, and the next edge throws the weights far off. This is called windup. In tests, a flat image of 200 x 200 pixels drove the plain update to errors of about 150,000 levels at lambda 0.999.

The studio guards against it. After each step, P is made exactly symmetric again, and its trace is capped at its starting value. With this safeguard, the same flat image stays within a few levels.

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

  1. Start with weights that give the mean of the neighbors, and P = I / delta.
  2. For each pixel in raster order, predict it from its neighbors.
  3. Write the prediction, or the error.
  4. Compute the gain, move the weights by the gain times the error, and update P.
  5. Make P symmetric and cap its trace, against rounding and windup.

Parameters

Parameter Default Values Meaning
Window size size 3 3 to 5, odd px The window of neighbors that predict the center, which itself is left out.
Forgetting factor lambda lambda 0.999 0.95 to 1 How much each older pixel still counts. Pixel n steps back counts lambda to the power n.
Start regularization delta delta 0.01 0.001 to 10 P starts as the identity divided by delta. A small delta trusts the first pixels a lot.
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

rlsPlane in web/src/core/filters/adaptive/scan.ts scans the image in raster order and carries the weights and P from pixel to pixel. Values are scaled to 0 to 1. 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.

RLS does best on the stripes, and stays close to the fixed filters on the disks. It reaches its weights faster than LMS, so less of the image is filtered with poor weights.

Complexity

  • Time: \(O(N k^4)\)
  • Memory: \(O(N + k^4)\)

N is the number of pixels per channel and k the window width. The window has k^2 - 1 neighbors, and P has their square. The scan cannot be split into tiles, so the filter is global.

Artifacts and limitations

Limitations

  • Costs (k^2 - 1)^2 operations per pixel, where LMS costs k^2 - 1.
  • The result depends on the scan order.

Artifacts

  • With lambda well below 1, the weights follow the noise, and the result is noisy.

Tests

A NumPy loop of the same update, with the same safeguard, in tools/fixtures_scan.py, gives the reference. Before it is used, the script checks two things. With lambda 1, the prediction at three pixels must equal the regularized least-squares fit to all pixels before them, solved directly with numpy.linalg.solve. And on a flat image, lambda 0.99 must stay bounded.

References

  • Haykin, S. Adaptive Filter Theory, 4th edition. Prentice Hall, 2002.
  • 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.