MAGSAC: Marginalising Sample Consensus | VitaVision
← Back to atlas

MAGSAC: Marginalising Sample Consensus

6 min readIntermediateView in graphIn narrative: Forty Years Against Outliers
Based on
MAGSAC: marginalizing sample consensus
Barath, Matas, Noskova · CVPR 2019
arXiv ↗

Goal

Robustly fit a parametric model (homography, fundamental matrix, or essential matrix) to a set PP of data points contaminated by outliers, without a user-tuned inlier threshold ε\varepsilon. Inputs: data set PP; minimal sample size ss; residual function D(θ,p)D(\theta, p); upper noise-scale bound σmax\sigma_\mathrm{max} (replaces the hard threshold ε\varepsilon); confidence target η\eta. Outputs: weighted-least-squares model θ∗\theta^* and marginalised quality score Q∗(θ∗,P)Q^*(\theta^*, P). The defining property is σ\sigma-consensus: the noise scale σ\sigma is treated as a random variable uniformly distributed on [0,σmax][0, \sigma_\mathrm{max}], and the RANSAC quality function Q(θ,σ,P)Q(\theta, \sigma, P) is marginalised over σ\sigma (Eq. 2); the final model is a weighted least-squares fit using the marginalised inlier likelihoods as IRLS weights (Alg. 1), with no hard inlier/outlier partition.

Algorithm

Let PP denote the set of data points and N=∣P∣N = |P| the point count. Let ss denote the minimal sample size required to instantiate the model. Let σ\sigma denote the noise scale and σmax\sigma_\mathrm{max} its upper bound. Let D(θ,p)D(\theta, p) denote the residual of point pp under model θ\theta. Let g(r∣σ)g(r \mid \sigma) denote the chi-squared inlier-residual density with ρ\rho degrees of freedom — a function of radius rr and scale σ\sigma. Let τ(σ)\tau(\sigma) denote the effective inlier threshold at noise scale σ\sigma. Let L(p∣θ)L(p \mid \theta) denote the marginalised inlier likelihood of point pp under model θ\theta, used as the IRLS weight. Let Q∗(θ,P)Q^*(\theta, P) denote the marginalised quality score. Let dd denote the number of uniform partitions of [0,σmax][0, \sigma_\mathrm{max}] used for discretisation (default d=10d = 10). Let η\eta denote the confidence target for the stopping criterion. Let τref\tau_\mathrm{ref} denote the SPRT pre-screen reference threshold (set to 1 pixel).

Definition
Marginalised quality

The RANSAC quality function Q(θ,σ,P)Q(\theta, \sigma, P) averaged over σ\sigma uniformly distributed on [0,σmax][0, \sigma_\mathrm{max}]:

Q∗(θ,P)=1σmax∫0σmaxQ(θ,σ,P) dσ(Eq. 2).Q^*(\theta, P) = \frac{1}{\sigma_\mathrm{max}} \int_0^{\sigma_\mathrm{max}} Q(\theta, \sigma, P)\, d\sigma \quad \text{(Eq. 2)}.
Definition
Inlier threshold τ(σ)\tau(\sigma)

The 0.95 quantile of g(r∣σ)g(r \mid \sigma). For χ2(4)\chi^2(4) inlier residuals (2D point correspondences):

τ(σ)=3.64 σ.\tau(\sigma) = 3.64\,\sigma.
Definition
Per-point weight L(p∣θ)L(p \mid \theta)

The marginalised inlier likelihood — used as the IRLS weight in the final weighted least-squares refit:

L(p∣θ)=∫0σmaxg(D(θ,p)∣σ) p(σ) dσ.L(p \mid \theta) = \int_0^{\sigma_\mathrm{max}} g(D(\theta, p) \mid \sigma)\,p(\sigma)\, d\sigma.

Computed on a grid of d=10d = 10 uniform partitions of [0,σmax][0, \sigma_\mathrm{max}].

Procedure

Algorithm
MAGSAC
Input: Data set PP; minimal sample size ss; residual function D(θ,p)D(\theta, p); upper noise bound σmax\sigma_\mathrm{max}; confidence target η\eta; SPRT reference threshold τref=1\tau_\mathrm{ref} = 1 pixel; partition count d=10d = 10.
Output: Weighted-least-squares model θ∗\theta^* and marginalised quality score Q∗(θ∗,P)Q^*(\theta^*, P).
  1. Sample. Draw a minimal sample of size ss from PP (uniform or PROSAC-ordered).
  2. Fit minimal model. Instantiate θj\theta_j from the sample.
  3. Compute marginalised quality. Discretise σ\sigma over dd uniform partitions of [0,σmax][0, \sigma_\mathrm{max}] and approximate Q∗(θj,P)Q^*(\theta_j, P) via the discretised form (Eq. 5).
  4. SPRT pre-screen with reference threshold τref=1\tau_\mathrm{ref} = 1 pixel — cheap rejection of obviously bad models, reused from USAC.
  5. If Q∗Q^* exceeds the running maximum, refine via IRLS. Compute weights L(p∣θj)L(p \mid \theta_j) for every point. Refit θ∗\theta^* by weighted least squares. Iterate until convergence.
  6. Update best model and termination criterion. The termination count k∗(P,θ)k^*(P, \theta) is also marginalised over σ\sigma (Eq. 8) and updated each time a new best model is found. Repeat from step 1 until η\eta confidence is reached.

Implementation

MAGSAC σ\sigma-consensus quality computation in Rust:

/// Discretised σ-consensus quality (MAGSAC).
/// Returns (Q*, per-point weights) for use in IRLS refit.
pub fn magsac_quality<F>(
    residuals: &[f64],
    sigma_max: f64,
    d: usize,
    g: F,           // chi-squared density g(r | σ)
) -> (f64, Vec<f64>)
where F: Fn(f64, f64) -> f64
{
    let dsigma = sigma_max / d as f64;
    let mut weights = vec![0.0_f64; residuals.len()];
    let mut q_star = 0.0;

    for i in 1..=d {
        let sigma = i as f64 * dsigma;
        let tau = 3.64 * sigma; // χ²(4), 0.95 quantile
        for (j, &r) in residuals.iter().enumerate() {
            let w = if r <= tau { g(r, sigma) } else { 0.0 };
            weights[j] += w * dsigma;
            q_star += w * dsigma;
        }
    }
    // Normalise by σ_max (uniform prior on σ).
    let inv = 1.0 / sigma_max;
    for w in weights.iter_mut() { *w *= inv; }
    (q_star * inv, weights)
}

Remarks

  • σ\sigma-consensus eliminates the user-tuned inlier threshold ε\varepsilon — replacing it with the upper bound σmax\sigma_\mathrm{max} (10 pixels across all experiments), which is much less sensitive to precise tuning than a hard threshold.
  • Discretisation grid d=10d = 10 reduces fitting cost from O(K)O(K) to O(d)O(d) per hypothesis. Values below d=5d = 5 risk underweighting high-σ\sigma components; the paper provides no sensitivity sweep beyond the empirical choice.
  • Per-point weights L(p∣θ)L(p \mid \theta) are marginalised inlier likelihoods. The final model is a weighted-least-squares fit via IRLS, not a hard inlier-set refit.
  • For χ2(4)\chi^2(4) residuals (2D point correspondences), the effective inlier threshold at each σi\sigma_i is τ(σi)=3.64 σi\tau(\sigma_i) = 3.64\,\sigma_i. For higher residual dimensions the constant changes; consult the appropriate χ2\chi^2 quantile table.
  • Float32 underflow safety: exp⁡(−D2/2σi2)\exp(-D^2 / 2\sigma_i^2) underflows in float32 when D>∼13D > {\sim}13 px for σi=1\sigma_i = 1 px — safely beyond the τ(σmax)\tau(\sigma_\mathrm{max}) gate, so float32 arithmetic is workable throughout the σ\sigma loop.
  • See raguram-usac for the unifying RANSAC engineering framework MAGSAC plugs into, including the When to choose USAC over MAGSAC discussion (hosted there per the older + broader-scope tiebreaker). See ransac for the four design axes that organise the modern RANSAC family.
  • Compared with LO-RANSAC: see When to choose LO-RANSAC over MAGSAC on the LO-RANSAC page, which hosts the comparison.

References

  1. D. Barath, J. Matas, J. Noskova. MAGSAC: Marginalizing Sample Consensus. Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), 2019. arxiv.org

Prerequisites

Extends

  • Fischler–Bolles RANSAC

    MAGSAC marginalises the inlier threshold rather than fixing it — orthogonal axis to USAC's framework refactor