-
Notifications
You must be signed in to change notification settings - Fork 35
/
ExampleMean.cpp
42 lines (34 loc) · 1.38 KB
/
ExampleMean.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "RTL.hpp"
#include <iostream>
using namespace std;
// A mean calculator
class MeanEstimator : public RTL::Estimator<double, double, vector<double> >
{
public:
// Calculate the mean of data at the sample indices
virtual double ComputeModel(const vector<double>& data, const set<int>& samples)
{
double mean = 0;
for (auto itr = samples.begin(); itr != samples.end(); itr++) mean += data[*itr];
return mean / samples.size();
}
// Calculate error between the mean and given datum
virtual double ComputeError(const double& mean, const double& datum) { return datum - mean; }
};
// The main function
int main(void)
{
// The given data with outliers
vector<double> data = { 2.5, 2.6, 2.7, 2.8, 2.9, 3, 3.1, 3.2, 100, 3.3, 3.4, 3.5 };
// Find the best model using RANSAC
MeanEstimator estimator;
RTL::RANSAC<double, double, vector<double> > ransac(&estimator);
double model;
double loss = ransac.FindBest(model, data, data.size(), 1);
// Determine inliers using the best model if necessary
vector<int> inliers = ransac.FindInliers(model, data, data.size());
// Print the result
cout << "- Found Model: " << model << " (Loss: " << loss << ")" << endl;
cout << "- The Number of Inliers: " << inliers.size() << " (N: " << data.size() << ")" << endl;
return 0;
}