Click or drag to resize
Accord.NET (logo) RANSACTModel Class
Multipurpose RANSAC algorithm.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningRANSACTModel

Namespace:  Accord.MachineLearning
Assembly:  Accord.MachineLearning (in Accord.MachineLearning.dll) Version: 3.4.0
Syntax
public class RANSAC<TModel>
where TModel : class
Request Example View Source

Type Parameters

TModel
The model type to be trained by RANSAC.

The RANSACTModel type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyDegenerate
Degenerative set detection function.
Public propertyDistances
Distance function.
Public propertyFitting
Model fitting function.
Public propertyMaxEvaluations
Maximum number of trials. Default is 1000.
Public propertyMaxSamplings
Maximum number of attempts to select a non-degenerate data set. Default is 100.
Public propertyProbability
Gets or sets the probability of obtaining a random sample of the input points that contains no outliers. Default is 0.99.
Public propertySamples
Gets or sets the minimum number of samples from the data required by the fitting function to fit a model.
Public propertyThreshold
Gets or sets the minimum distance between a data point and the model used to decide whether the point is an inlier or not.
Public propertyTrialsNeeded
Gets the current estimate of trials needed.
Public propertyTrialsPerformed
Gets the current number of trials performed.
Top
Methods
  NameDescription
Public methodCompute(Int32)
Computes the model using the RANSAC algorithm.
Public methodCompute(Int32, Int32)
Computes the model using the RANSAC algorithm.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by Matrix.)
Top
Remarks

RANSAC is an abbreviation for "RANdom SAmple Consensus". It is an iterative method to estimate parameters of a mathematical model from a set of observed data which contains outliers. It is a non-deterministic algorithm in the sense that it produces a reasonable result only with a certain probability, with this probability increasing as more iterations are allowed.

References:

  • P. D. Kovesi. MATLAB and Octave Functions for Computer Vision and Image Processing. School of Computer Science and Software Engineering, The University of Western Australia. Available in: http://www.csse.uwa.edu.au/~pk/research/matlabfns
  • Wikipedia, The Free Encyclopedia. RANSAC. Available on: http://en.wikipedia.org/wiki/RANSAC

Examples
// Fix the random number generator
Accord.Math.Random.Generator.Seed = 0;

double[,] data = // This is the same data used in the RANSAC sample app
{
    {  1.0,  0.79 }, {  3,    2.18 }, {  5,    5.99 }, {  7.0,  7.65 },
    {  9.0,  9.55 }, { 11,   11.89 }, { 13,   13.73 }, { 15.0, 14.77 }, 
    { 17.0, 18.00 }, {  1.2,  1.45 }, {  1.5,  1.18 }, {  1.8,  1.92 },
    {  2.1,  1.47 }, {  2.4,  2.41 }, {  2.7,  2.35 }, {  3.0,  3.41 },
    {  3.3,  3.78 }, {  3.6,  3.21 }, {  3.9,  4.76 }, {  4.2,  5.03 },
    {  4.5,  4.19 }, {  4.8,  3.81 }, {  5.1,  6.07 }, {  5.4,  5.74 },
    {  5.7,  6.39 }, {  6,    6.11 }, {  6.3,  6.86 }, {  6.6,  6.35 },
    {  6.9,  7.9  }, {  7.2,  8.04 }, {  7.5,  8.48 }, {  7.8,  8.07 },
    {  8.1,  8.22 }, {  8.4,  8.41 }, {  8.7,  9.4  }, {  9,    8.8 },
    {  9.3,  8.44 }, {  9.6,  9.32 }, {  9.9,  9.18 }, { 10.2,  9.86 },
    { 10.5, 10.16 }, { 10.8, 10.28 }, { 11.1, 11.07 }, { 11.4,  11.66 },
    { 11.7, 11.13 }, { 12,   11.55 }, { 12.3, 12.62 }, { 12.6,  12.27 },
    { 12.9, 12.33 }, { 13.2, 12.37 }, { 13.5, 12.75 }, { 13.8,  14.44 },
    { 14.1, 14.71 }, { 14.4, 13.72 }, { 14.7, 14.54 }, { 15,    14.67 },
    { 15.3, 16.04 }, { 15.6, 15.21 }, {  1,    3.9  }, {  2,    11.5 },
    {  3.0, 13.0  }, {  4,    0.9  }, {  5,    5.5  }, {  6,    16.2 },
    {  7.0,  0.8  }, {  8,    9.4  }, {  9,    9.5  }, { 10,    17.5 },
    { 11.0,  6.3  }, { 12,   12.6  }, { 13,    1.5  }, { 14,     1.5 },
    {  2.0,  10   }, {  3,    9    }, { 15,    2    }, { 15.5,   1.2 },
};


// First, fit simple linear regression directly for comparison reasons.
double[] x = data.GetColumn(0); // Extract the independent variable
double[] y = data.GetColumn(1); // Extract the dependent variable

// Use Ordinary Least Squares to learn the regression
OrdinaryLeastSquares ols = new OrdinaryLeastSquares();

// Estimate a line passing through the (x, y) points
SimpleLinearRegression regression = ols.Learn(x, y);

// Now, compute the values predicted by the 
// regression for the original input points
double[] commonOutput = regression.Transform(x);


// Now, fit simple linear regression using RANSAC
int maxTrials = 1000;
int minSamples = 20;
double probability = 0.950;
double errorThreshold = 1000;

// Create a RANSAC algorithm to fit a simple linear regression
var ransac = new RANSAC<SimpleLinearRegression>(minSamples)
{
    Probability = probability,
    Threshold = errorThreshold,
    MaxEvaluations = maxTrials,

    // Define a fitting function
    Fitting = (int[] sample) =>
    {
        // Build a Simple Linear Regression model
        return new OrdinaryLeastSquares()
            .Learn(x.Get(sample), y.Get(sample));
    },

    // Define a inlier detector function
    Distances = (SimpleLinearRegression r, double threshold) =>
    {
        var inliers = new List<int>();
        for (int i = 0; i < x.Length; i++)
        {
            // Compute error for each point
            double error = r.Transform(x[i]) - y[i];

            // If the square error is low enough,
            if (error * error < threshold)
                inliers.Add(i); //  the point is considered an inlier.
        }

        return inliers.ToArray();
    }
};


// Now that the RANSAC hyperparameters have been specified, we can 
// compute another regression model using the RANSAC algorithm:

int[] inlierIndices;
SimpleLinearRegression robustRegression = ransac.Compute(data.Rows(), out inlierIndices);

// Compute the output of the model fitted by RANSAC
double[] ransacOutput = robustRegression.Transform(x);
See Also