Skip to content

RANSAC surface fit

Fact Value
Also called RANSAC-based robust estimation, RANSAC background removal
Status Implemented and tested
Family Adaptive and statistical filters
Engine Science engine
Type Nonlinear, statistical, robust fit of a surface
Output Image
Operator shown as algorithm (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/robust.py, ransac_surface
Operator code engine/src/pixelnexus_engine/filters/robust.py, ransac_surface_representation
Packages scikit-image

Overview

The RANSAC surface fit removes uneven lighting. It fits a smooth polynomial surface to the brightness of the background with random sample consensus, after Fischler and Bolles, and subtracts it. Objects on the background are outliers, so they do not bend the surface, as they would with a plain fit or a large blur. The filter runs in the PixelNexus engine, with scikit-image.

Four panels. Two rectangles, one light and one dark, on a gray background that brightens to the right. The corrected image has an even gray background with both rectangles kept. The background panel is a smooth gray ramp with no trace of the rectangles. The inliers panel shows a grid of yellow dots over the darkened background, with no dots on the rectangles. Four panels. Two rectangles, one light and one dark, on a gray background that brightens to the right. The corrected image has an even gray background with both rectangles kept. The background panel is a smooth gray ramp with no trace of the rectangles. The inliers panel shows a grid of yellow dots over the darkened background, with no dots on the rectangles.
Two rectangles on a background that brightens to the right, with a slight bend. Then the corrected image, the fitted background, and the grid samples taken as background in yellow.

How it works

  1. Sample the luma on a grid.
  2. Fit the surface to random sets of samples, and count the samples close to each fit.
  3. Keep the fit with the most inliers, and fit it again to all of them.
  4. Subtract the surface minus its mean from every channel.

Mathematical foundation

\[ B(x, y) = \sum_{i + j \le d} a_{ij}\, x^i y^j, \qquad I'(p) = I(p) - \bigl(B(p) - \bar B\bigr) \]

The formula shows the default parameter values.

where

  • \(B\): The fitted background surface
  • \(a_{ij}\): The coefficients of the surface
  • \(d\): The degree
  • \(\bar B\): The mean of the surface over the image

B is a polynomial surface of degree d, with x and y scaled to -1 to 1. Each RANSAC trial picks as many random grid samples as B has coefficients, and fits B to them by least squares. It then counts the inliers, the samples within the threshold of B. Fischler and Bolles keep the trial with the most inliers. scikit-image then fits B to all of its inliers. The corrected image I' subtracts B minus its mean from each channel, so the background becomes even and the mean brightness stays. Objects are outliers, so they do not bend the fit.

A plain least squares fit lets every sample pull, so a large bright object lifts the surface under it. RANSAC instead fits many surfaces to small random sets of samples, and keeps the one that the most samples agree with. Samples on objects disagree with the background, so they end up outside the consensus. The final fit uses only the samples that agree.

Kernel or operator

There is no fixed kernel. The Inspector asks the engine for the stages with the current settings.

Parameters

Parameter Default Values Meaning
Output output Corrected image Corrected image, Fitted background, Inlier samples The image with the background removed, the fitted surface as gray, or the image darkened with the grid samples that fit the surface marked in yellow.
Degree degree 2 1 to 3 The degree of the polynomial in x and y. 1 is a tilted plane, 2 can bend once, 3 twice.
Inlier threshold threshold 10 1 to 100 levels A sample is an inlier when its luma is within this distance of the surface. Our choice.
Grid step step 8 2 to 64 px The samples are the pixels of a grid with this spacing. Our choice.
Trials trials 200 10 to 2000 How many random samples RANSAC tries. The random seed is fixed, so the result repeats. Our choice.

Implementation

ransac_surface in engine/src/pixelnexus_engine/filters/robust.py samples the luma on a grid and passes the samples to skimage.measure.ransac with a polynomial surface model of our own. The coordinates x and y are scaled to -1 to 1 first, which keeps the least squares well conditioned. The random seed is fixed at 0, so the same image always gives the same result. The correction subtracts the same amount from all three channels.

Worked example

Degree Threshold Inliers Largest error (levels)
1 5 176 16.7
1 10 236 12.5
1 30 256 9.9
2 5 212 3.3
2 10 253 0.6
2 30 256 0.9
3 5 164 17.3
3 10 229 4.9
3 30 256 1.5

The image of the figure, with a grid step of 8 (300 samples) and 200 trials. The background is 80 + 0.4 x + 0.2 y + 0.002 (x - 80)^2, a surface of degree 2. The largest error compares the fitted surface with it over the whole image.

The true background has degree 2. With degree 2 and the default threshold of 10, the fitted surface is within 0.6 levels of it everywhere. Degree 1 cannot follow the bend, and misses by 9.9 to 16.7 levels. Degree 3 can follow it, but with a tight threshold of 5 only 164 samples agree, and the fit misses by 17 levels. The threshold has to sit above the noise, which is sigma 3 here.

Complexity

  • Time: \(O(T M + N)\)
  • Memory: \(O(N)\)

N is the number of pixels, M the number of samples and T the number of trials.

Artifacts and limitations

Limitations

  • The background must be smooth enough for a low degree polynomial.
  • When objects cover most of the image, RANSAC can take them as the background.

Artifacts

  • A faint ramp left where the surface cannot follow the lighting.

Tests

The monomials must come out in a fixed order. On a lit scene with two rectangles, degree 2 must recover the background within 3 levels, take no sample on the bright rectangle, and leave an even ground. Degree 1 must miss by more than 5 levels. The output must repeat from run to run, and too small a grid for the degree must stop with a message. The representation must have its four stages.

References

  • Fischler, M. A., and Bolles, R. C. Random sample consensus, a paradigm for model fitting with applications to image analysis and automated cartography. Communications of the ACM 24(6), 381 to 395, 1981. DOI 10.1145/358669.358692.
  • scikit-image documentation, skimage.measure.ransac.