o
odinpkg.dev
packages / library / RANSAC_2D_line_fit_in_Odin

RANSAC_2D_line_fit_in_Odin

3685d55library

A simple implementation to learn about it.

No license · updated 9 months ago

RANSAC 2D line fit in Odin

A simple implementation to learn about it.

Description

This is a simple implementation of RANSAC ( Random Sample Consensus ), 2D line fitting estimation. RANSAC is an iterative algorithm used to estimate a mathematical model like a line 2D, plane 3D, or transformation from a dataset that contains outliers. It’s extremely useful when you know your data has some “good” ( inliers ) and some “bad” ( outliers ) points, and you want to find a model that best fits the inliers while ignoring the outliers.

Why use it

If you have a bunch of points on a 2D plot that mostly lie along a line, but some are way off ( outliers ).
If you fit a least-squares line, the outliers can skew the result badly.

RANSAC avoids this by:

  • Repeatedly sampling random subsets of the data,
  • Estimating a model from each subset,
  • Measuring how many points fit that model “well enough” ( within a tolerance ),
  • Keeping the model with the largest consensus set ( i.e. most inliers ).

Test Output

./ransac.exe


Begin RANSAC in Odin to fit a model of a line in 2D...


===> Running RANSAC Test Suite


Test Case 1 : Perfect Line ( y = 2x + 1 )
Model: 0.894x + -0.447y + 0.447 = 0
[ PASS ]   Found all 100 inliers
[ PASS ]   Model A is correct
[ PASS ]   Model B is correct

Test Case 2 : Line with 50 % Outliers ( y = -0.5x + 10 )
Model: -0.447x + -0.894y + 8.944 = 0
[ PASS ]   Found ~50 inliers
[ PASS ]   Model A is correct
[ PASS ]   Model B is correct

Test Case 3 : Vertical Line ( x = 10 )
Model: 1.000x + -0.014y + -9.324 = 0
n_in_liners : 42 
[ FAIL ]   Found ~50 inliers
[ PASS ]   Model A is correct ( A ~ +/-1.0 )
[ FAIL ]   Model B is correct ( B ~ 0.0 )
[ FAIL ]   Model C is correct ( C ~ +/-10.0 )

Test Case 4: Not Enough Points
RANSAC Error: Not enough points to fit model.
[ PASS ]   Returns -1
[ PASS ]   Inliers array is nil

Test Case 5: All Outliers ( Random Cloud )
Model: 0.975x + -0.221y + -41.281 = 0
  Found 4 inliers ( expected to be very low )
[ PASS ]   Found a small number of inliers

 ==> Test Suite Complete

Running RANSAC Benchmark

Configuration:
  Points:     10000 ( 7000 inliers, 3000 outliers )
  Iterations: 2000
  Threshold:  0.50

Running RANSAC ( with refit )... 
Done.

 Benchmark Results

  Time taken:   15.624001ms seconds
  Inliers found: 7001 ( out of 7000 expected )
  Best model:   
Model: 0.832x + -0.555y + -3.881 = 0


... end RANSAC in Odin to fit a model of a line in 2D.

License

MIT Open Source License

Have fun

Best regards,
Joao Carvalho