-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimpleRansac.h
52 lines (44 loc) · 1.32 KB
/
SimpleRansac.h
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
43
44
45
46
47
48
49
50
51
52
#ifndef SIMPLERANSAC_H
#define SIMPLERANSAC_H
#include <array>
#include <random>
#include <set>
#include <vector>
template <typename Model_, typename DataType>
Model_ ransac(const std::vector<DataType>& data, double threshold, int numIterations,
unsigned int randomSeed = std::random_device{}())
{
if (data.size() < Model_::ModelSize)
throw std::runtime_error("Not enough data");
std::mt19937 engine{ randomSeed };
std::uniform_int_distribution<size_t> dis{ 0, data.size() - 1 };
int bestInliers = -1;
Model_ bestModel;
for (int it = 0; it < numIterations; it++)
{
// select points
int found = 0;
std::array<size_t, Model_::ModelSize> indices;
std::set<size_t> usedIndices;
while (found < Model_::ModelSize)
{
size_t sample = dis(engine);
if (usedIndices.find(sample) != usedIndices.end())
continue;
usedIndices.insert(sample);
indices[found++] = sample;
}
// compute model
Model_ m;
m.compute(data, indices);
int inliers = m.computeInliers(data, threshold);
if (inliers > bestInliers)
{
bestModel = m;
bestInliers = inliers;
}
}
bestModel.refine(data, threshold);
return bestModel;
}
#endif