10 #ifndef math_modelsearch_impl_h 11 #define math_modelsearch_impl_h 13 #ifndef math_modelsearch_h 23 template <
typename TModelFit>
25 const TModelFit& p_state,
size_t p_kernelSize,
26 const typename TModelFit::Real& p_fitnessThreshold,
27 typename TModelFit::Model& p_bestModel, std::vector<size_t>& p_inliers)
29 size_t bestScore = std::string::npos;
31 size_t softIterLimit = 1;
32 size_t hardIterLimit = 100;
34 size_t nSamples = p_state.getSampleCount();
35 std::vector<size_t> ind(p_kernelSize);
37 while (iter < softIterLimit && iter < hardIterLimit)
39 bool degenerate =
true;
40 typename TModelFit::Model currentModel;
45 degenerate = !p_state.fitModel(ind, currentModel);
47 if (i > 100)
return false;
50 std::vector<size_t> inliers;
52 for (
size_t j = 0; j < nSamples; j++)
54 if (p_state.testSample(j, currentModel) < p_fitnessThreshold)
60 const size_t ninliers = inliers.size();
61 bool update_estim_num_iters =
65 if (ninliers > bestScore ||
66 (bestScore == std::string::npos && ninliers != 0))
69 p_bestModel = currentModel;
71 update_estim_num_iters =
true;
74 if (update_estim_num_iters)
78 double f = ninliers /
static_cast<double>(nSamples);
79 double p = 1 - pow(f, static_cast<double>(p_kernelSize));
80 const double eps = std::numeric_limits<double>::epsilon();
83 softIterLimit = log(1 -
p) / log(
p);
94 template <
typename TModelFit>
96 const TModelFit& p_state,
size_t p_kernelSize,
97 const typename TModelFit::Real& p_fitnessThreshold,
size_t p_populationSize,
98 size_t p_maxIteration,
typename TModelFit::Model& p_bestModel,
99 std::vector<size_t>& p_inliers)
103 std::vector<Species> storage;
104 std::vector<Species*> population;
105 std::vector<Species*> sortedPopulation;
107 size_t sampleCount = p_state.getSampleCount();
108 int elderCnt = (int)p_populationSize / 3;
109 int siblingCnt = (p_populationSize - elderCnt) / 2;
110 int speciesAlive = 0;
112 storage.resize(p_populationSize);
113 population.reserve(p_populationSize);
114 sortedPopulation.reserve(p_populationSize);
116 it != storage.end(); it++)
119 population.push_back(&*it);
120 sortedPopulation.push_back(&*it);
124 while (iter < p_maxIteration)
133 for (; i < elderCnt; i++)
135 population.push_back(sortedPopulation[i]);
139 int se = (int)speciesAlive;
140 for (; i < elderCnt + siblingCnt; i++)
142 Species* sibling = sortedPopulation[--se];
143 population.push_back(sibling);
152 (p1 > se / 2) ? (r2 % p1) : p1 + 1 + (r2 % (se - p1 - 1));
153 ASSERT_(p1 != p2 && p1 < se && p2 < se);
155 Species*
a = sortedPopulation[p1];
156 Species*
b = sortedPopulation[p2];
159 std::set<size_t> sampleSet;
160 sampleSet.insert(
a->sample.begin(),
a->sample.end());
161 sampleSet.insert(
b->sample.begin(),
b->sample.end());
164 sampleSet.insert(rand() % sampleCount);
169 for (; i < (int)p_populationSize; i++)
171 Species*
s = sortedPopulation[i];
172 population.push_back(
s);
180 it != population.end(); it++)
183 if (p_state.fitModel(
s.sample,
s.model))
186 for (
size_t i = 0; i < p_state.getSampleCount(); i++)
188 typename TModelFit::Real f = p_state.testSample(i,
s.model);
189 if (f < p_fitnessThreshold)
192 s.inliers.push_back(i);
197 s.fitness /=
s.inliers.size();
199 s.fitness *= (sampleCount -
s.inliers.size());
204 std::numeric_limits<typename TModelFit::Real>::max();
215 sortedPopulation.begin(), sortedPopulation.end(), Species::compare);
220 p_bestModel = sortedPopulation[0]->model;
221 p_inliers = sortedPopulation[0]->inliers;
223 return !p_inliers.empty();
227 #endif // math_modelsearch_h
bool ransacSingleModel(const TModelFit &p_state, size_t p_kernelSize, const typename TModelFit::Real &p_fitnessThreshold, typename TModelFit::Model &p_bestModel, std::vector< size_t > &p_inliers)
Run the ransac algorithm searching for a single model.
#define ASSERT_(f)
Defines an assertion mechanism.
This base provides a set of functions for maths stuff.
void pickRandomIndex(size_t p_size, size_t p_pick, std::vector< size_t > &p_ind)
Select random (unique) indices from the 0..p_size sequence.
bool geneticSingleModel(const TModelFit &p_state, size_t p_kernelSize, const typename TModelFit::Real &p_fitnessThreshold, size_t p_populationSize, size_t p_maxIteration, typename TModelFit::Model &p_bestModel, std::vector< size_t > &p_inliers)
Run a generic programming version of ransac searching for a single model.
GLubyte GLubyte GLubyte a