Feature Correspondence Filtering using RANSAC
Problem Statement
When matching visual features (like SIFT keypoints) between two images, the resulting list of correspondences often contains outliers due to misidentifications or repetitive patterns. To robustly estimate the geometric transformation (like a homography or affine map) relating the two images, the Random Sample Consensus (RANSAC) algorithm is employed.
RANSAC Algorithm
RANSAC repeatedly:
- Selects a minimal random subset of correspondences (e.g., 4 points for a Homography)
- Computes the transformation parameters based only on this minimal subset
- Tests all other correspondences against the computed model to count inliers
Your Task
Calculate the probability of selecting a "clean" minimal subset given a known outlier ratio and determine the minimum number of RANSAC iterations required to achieve a specified success probability p.
Formulas
The probability Pclean of randomly choosing a minimal subset (size M) consisting entirely of inliers (assuming inlier ratio ε) is:
Pclean=εM
The required number of iterations K to ensure at least one clean sample with probability p is:
K=log(1−Pclean)log(1−p)
Calculate K given the inputs and return it as an integer (ceiling the result).
Example:
M=4, R_outlier=0.3, p=0.99
17
- Calculate Inlier Ratio: ε = 1 - 0.3 = 0.7
- Calculate P_clean: P_clean = 0.7^4 = 0.2401
- Calculate K: K = log(1 - 0.99) / log(1 - 0.2401) = log(0.01) / log(0.7599) ≈ 16.8
- Ceiling: K = 17
Constraints:
- M (minimal subset size) is an integer ≥ 2
- R_outlier (outlier ratio) is a float between 0 and 1
- p (desired success probability) is a float between 0 and 1
- Output K must be an integer (ceiling the result of the log calculation)
1. Background Knowledge
RANSAC (Random Sample Consensus) is a robust estimation algorithm for fitting models to data contaminated by outliers, widely used in computer vision for tasks like homography estimation from feature correspondences (e.g., SIFT, ORB). In feature matching between images, inliers satisfy the geometric model (e.g., homography H mapping points xi↔\mathbf{x}i′ via xi′≈H\mathbf{x}i), while outliers (ratio Routlier) do not due to mismatches.
Key prerequisites:
- Homography: 8 DOF matrix (3×3) estimated from minimal subset M=4 point correspondences (homogeneous coordinates).
- Inlier ratio ε=1−Routlier: Fraction of good matches.
- Success probability p: Confidence that at least one iteration yields a clean subset.
The core probability models assume independent random sampling from a pool with ε inliers.
2. Algorithm Approach
Standard RANSAC for this problem:
- Repeat K iterations:
- Sample M correspondences randomly.
- Fit model (e.g., solve for H via DLT - Direct Linear Transform).
- Count inliers (points within distance threshold t of model).
- Select model with maximum inliers; refit on all inliers.
Theoretical sampling: Computes minimum K iterations needed for probability p of selecting one clean subset (all M inliers), avoiding exhaustive search.
Variants (from literature): PROSAC (quality-guided sampling), USAC (preemptive scoring), but basic probabilistic RANSAC suffices here.
3. Step-by-Step Strategy
Inputs: M (e.g., 4), Routlier∈[0,1), p∈(0,1].
- Compute inlier ratio: ε=1−Routlier.
- Clean subset probability: Pclean=εM (binomial: all M samples are inliers).
- Iterations formula (1 - probability of all K failures):
- Derivation: P(\text{at least one success})=1−(1−Pclean)K=p.
- Ceil to integer: K=⌈K⌉ (ensure ≥ required iterations).
- Edge cases:
- If ε=1, Pclean=1, K=1.
- If εM≈0, K→∞ (handle via max iterations or log bounds).
- Use math.ceil, math.log in code.
Python snippet:
import math
def ransac_iterations(M: int, R_outlier: float, p: float) -> int:
epsilon = 1.0 - R_outlier
if epsilon == 0:
return float('inf') # or large number
P_clean = epsilon ** M
if P_clean == 0:
return float('inf')
K = math.log(1 - p) / math.log(1 - P_clean)
return math.ceil(K)
4. Common Pitfalls
- Floating-point precision: log(1−Pclean) near 0 when Pclean≈1; use math.log1p(-P_clean) for accuracy.
- Division by zero: When Pclean=1 (ε=1), denominator=0; return K=1.
- Negative log arg: Ensure 1−p>0, 1−Pclean>0; validate inputs.
- Non-integer ceiling: Always ceil (e.g., 10.1 → 11) to guarantee probability.
- Assuming total correspondences N: Formula ignores N (valid for large N); for small N, use hypergeometric but overkill here.
- Outlier ratio misinterpretation: Routlier is fraction, not count.
5. Time & Space Complexity
- Time: O(1) per call (constant-time log/power operations). Full RANSAC: O(K⋅N⋅M) where N is total correspondences (model fit + inlier test per iteration).
- Space: O(1) (scalar computations only).
This formulation ensures p-confidence in finding a clean sample, enabling robust model estimation even with 50-90% outliers typical in feature matching.