This is an example file on how to use the RANSAC algorithm. It tries to find a dominant plane in a number of 3D points.
#include <Base/Common/BIASpragma.hh>
#include "Base/Math/Random.hh"
#include <vector>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <float.h>
#include "ExampleRANSACPlane.hh"
using namespace BIAS;
using namespace std;
void Read3DPointsFromFile(char *filename, std::vector<HomgPoint3D> &points)
{
ifstream infile(filename);
if (!infile) {
BIASERR("Failed to open file " << filename);
return;
}
char c;
int i = 0;
while (!infile.eof()) {
infile >> point[0] >> point[1] >> point[2] >> c;
if (!infile.eof()) {
points.push_back(point);
cout << "- point " << i << " : "<< point << endl;
i++;
}
}
}
void GeneratePlanar3DPoints(std::vector<HomgPoint3D>& points,
unsigned int numInliers, unsigned int numOutliers)
{
double range = 10.0;
double noise = 0.1;
const double z = 2.0;
for (unsigned int i = 0; i < (numInliers + numOutliers); i++){
point3D[3] = 1.0;
if (i >= numInliers) {
}
points.push_back(point3D);
cout << "- point " << i << " : " << point3D << " ("
<< (i >= numInliers ? "outlier" : "inlier") << ")" << endl;
}
}
int main(int argc, char *argv[])
{
int res = 0;
const unsigned int numPoints = 20;
double inlier_fraction = 0.8;
bool refine_solution = true;
double maxsquareddistance = 0.01;
vector<bool> inliers;
bool debug = false;
vector<HomgPoint3D> points;
if (argc < 2) {
cout << endl << "Generating " << numPoints << " random noisy points "
<< "for testing..." << endl;
const unsigned int outliers = (int)rint((1.0 - inlier_fraction) * numPoints);
GeneratePlanar3DPoints(points, numPoints - outliers, outliers);
} else {
cout << endl << "Reading 3d points for testing from file " << argv[1] << "..." << endl;
Read3DPointsFromFile(argv[1], points);
}
if (debug) {
}
ransac.
Init(points, maxsquareddistance, refine_solution, inlier_fraction, -1.0);
ransac.
SolveMaster(inlier_fraction, solution, inliers);
cout << endl << "Inlier/outlier points after computation : " << endl;
for (unsigned int i = 0; i < inliers.size(); i++){
cout << "- point " << i << " is " << (inliers[i] ? "inlier" : "outlier") << endl;
}
cout << endl << "Final solution : " << solution << endl
<< "Ground truth : " << gt_plane << endl
<< "Difference : " << (solution - gt_plane).NormL2() << endl << endl;
return res;
}