{"id":244,"date":"2026-11-13T12:35:43","date_gmt":"2026-11-13T04:35:43","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-ransac-fits-a-model-to-inliers\/"},"modified":"2026-11-13T21:53:34","modified_gmt":"2026-11-13T13:53:34","slug":"how-ransac-fits-a-model-to-inliers","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-ransac-fits-a-model-to-inliers\/","title":{"rendered":"How RANSAC fits a model to inliers"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>RANSAC<\/strong> fits a geometric model when many points are outliers. It draws the smallest sample that determines the model, fits, and counts how many points agree within a threshold. After enough trials it refits on the largest inlier set. The number of trials needed to see at least one clean sample with probability p is <strong>log(1\u2212p) \/ log(1\u2212w^s)<\/strong>, where w is the inlier fraction and s is the sample size. The algorithm is Monte Carlo. It can miss the inliers.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Least squares uses every point, so one distant outlier drags the fit. If the model is determined by s points, a sample that happens to be all inliers produces a hypothesis close to the truth, and the other inliers will agree. Outlier samples produce hypotheses that few points agree with. Counting agreements separates those cases without a prior clustering step.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The trial count comes from a coin-flip argument. The chance a sample is clean is w^s if points are drawn independently and w is the inlier ratio. The chance every trial is dirty after N trials is (1\u2212w^s)^N. Set that below 1\u2212p and solve for N.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">When the inlier ratio and the threshold match the data, N from the formula yields at least one all-inlier sample with probability at least p, and the largest consensus set is then a reasonable refit. The threshold is a parameter, not a theorem. A too-wide threshold treats outliers as inliers. A too-narrow one rejects true noise. Extra memory is the current best inlier set. Each trial costs a fit on s points and a scan of the data.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The formula assumes sampling with the stated independence and a known w. If w is overestimated, N is too small and the success probability drops. Adaptive RANSAC updates N from the best consensus seen so far. That is the practical version.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Choose the model, the sample size s, the inlier threshold, and a success probability p. Estimate the inlier ratio w, or update it adaptively.<\/li>\n<li>Set N = log(1\u2212p) \/ log(1\u2212w^s), and cap it at a budget.<\/li>\n<li>Each trial draws s points, fits the model, and counts points within the threshold.<\/li>\n<li>Keep the model with the largest count. After the trials, refit the model on that consensus set, often by least squares.<\/li>\n<li>If a later trial beats the consensus, recompute N from the new implied w and continue until the updated N is reached.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The probability is about seeing a clean sample, not about the refit being the maximum-likelihood model under a noise law. A single trial is not RANSAC. Least squares on all points is the right fit when there are no outliers. The threshold has to be in the same units as the residual.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the trial count ignores the sample size<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Run a fixed handful of trials for a model that needs many points<\/td>\n<td>w^s shrinks quickly as s grows. A line, s = 2, needs far fewer trials than a model with s = 8 at the same inlier ratio.<\/td>\n<\/tr>\n<tr>\n<td>Fit least squares on every point and then delete large residuals once<\/td>\n<td>One pass can be pulled so far that true inliers look like outliers. RANSAC hypothesizes from clean samples first.<\/td>\n<\/tr>\n<tr>\n<td>Treat the consensus count as a probability<\/td>\n<td>The count ranks hypotheses. The probability p is a statement about the number of trials, computed from w before or during the loop.<\/td>\n<\/tr>\n<tr>\n<td>Skip the final refit<\/td>\n<td>The hypothesis came from s points. The consensus set contains more information, and the refit uses it.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A fit with no outliers is ordinary least squares. A robust loss that downweights residuals without sampling is an M-estimator. RANSAC is the random-sample consensus loop.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment draws the smallest sample that defines a line, counts inliers, and refits on the largest consensus set. Four points lie on y = 2x and two are far away. The fitted slope prints <code>2.0<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import random\npts = [(0, 0), (1, 2), (2, 4), (3, 6), (0, 5), (3, 0)]\nrng = random.Random(0)\nbest = None\nfor _ in range(40):\n    p, q = rng.sample(pts, 2)\n    if p[0] == q[0]:\n        continue\n    slope = (q[1] - p[1]) \/ (q[0] - p[0])\n    intercept = p[1] - slope * p[0]\n    inliers = [r for r in pts if abs(r[1] - (slope * r[0] + intercept)) &lt; 0.5]\n    if best is None or len(inliers) &gt; len(best[0]):\n        best = (inliers, slope)\nxs = [p[0] for p in best[0]]\nys = [p[1] for p in best[0]]\nn = len(xs)\nmean_x, mean_y = sum(xs) \/ n, sum(ys) \/ n\nnum = sum((x - mean_x) * (y - mean_y) for x, y in zip(xs, ys))\nden = sum((x - mean_x) ** 2 for x in xs)\nslope = num \/ den\nassert abs(slope - 2) &lt; 1e-6\nprint(f&quot;{slope:.1f}&quot;)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>OpenCV<\/strong> <code>findHomography<\/code> and image-stitching pipelines fit a model with RANSAC: draw a minimal sample, keep the fit with the most inliers. Structure-from-motion uses the same loop. Least squares on every point is pulled off the model by one bad match.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop RANSAC when the data are clean. A direct fit is simpler and deterministic. Keep it when a large fraction of points do not belong to the model, and set N from the formula rather than from a fixed guess.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for RANSAC returns the trial-count formula and the consensus refit that follows the best sample. <strong>RANSAC fits a model in contaminated data by repeating minimal random samples and keeping the hypothesis with the most inliers, using a trial count set by the inlier ratio.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: RANSAC fits a geometric model when many points are outliers. It draws the smallest sample that determines the model, fits, and counts how many points agree within a threshold. After enough trials it refits on the largest inlier set. The number of trials needed to see at&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,52],"tags":[],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-randomization"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":643,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions\/643"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}