26 #ifndef __RANSACPREKNOWLEDGE_hh__
27 #define __RANSACPREKNOWLEDGE_hh__
41 template <
class SolutionType>
68 unsigned int max_solutions_per_sample = 1,
69 bool refine_solution =
false):
70 RANSAC<SolutionType>(sample_size, max_solutions_per_sample,
78 virtual inline void AddGoodGuesses(
const std::vector<SolutionType> &Guesses) {
80 for (
typename std::vector<SolutionType>::const_iterator
81 it = Guesses.begin(); it!=Guesses.end(); it++)
92 virtual inline int SolveMaster(
double inlying_data_fraction,
93 SolutionType &solution,
94 std::vector<bool> &inliers);
102 template <
class SolutionType>
inline int
104 SolutionType &solution,
105 std::vector<bool> &inliers) {
106 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
107 "RANSACPreKnowledge::SolveMaster() -----------------------------"
108 "-----------------\n");
109 BCDOUT(D_RANSAC_SAMPLE_QUALITY,
"|||||||||||||||||||||\n\n");
112 _iBestSampleAcceptCount = -1;
113 _dBestSampleEvaluateScore = -1;
114 _iBestInlierAcceptCount = -1;
115 _dBestInlierEvaluateScore = -1;
116 _got_solution_flag =
false;
119 std::vector<unsigned int> which_samples;
122 std::vector<bool> inlier_set(_uiDataSize,
false);
125 int sample_accept_count;
128 double sample_evaluate_score;
131 bool ok_to_terminate_flag;
137 int inlier_accept_count;
140 double inlier_evaluate_score;
143 bool best_so_far_flag;
147 bool termination_check_active_flag =
false;
149 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
"checking "<<UserGuesses_.size()<<
152 for (
typename std::vector<SolutionType>::iterator
153 it = UserGuesses_.begin(); it!=UserGuesses_.end(); it++) {
156 ok_to_terminate_flag =
false;
157 sample_evaluate_score = DBL_MAX;
158 good_flag = this->EvaluateSolution(*it, _got_solution_flag,
159 inlier_set, sample_accept_count,
160 sample_evaluate_score,
161 ok_to_terminate_flag);
163 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
"PreGuess. "
164 <<
" solution: "<<(*it)
165 <<
" inliers: " <<sample_accept_count<<
"/"<<_uiDataSize
166 <<
" score: "<< sample_evaluate_score<<std::endl);
168 inlier_accept_count=-1;
169 inlier_evaluate_score = 1e20;
170 if (good_flag && _bRefineSolution) {
172 SolutionType samplesolution = *it;
173 bool ok_to_terminate_with_linear = ok_to_terminate_flag;
175 which_samples.clear();
176 for (
unsigned int i=0; i<_uiDataSize; i++)
177 if (inlier_set[i]) which_samples.push_back(i);
178 if (this->RefineSolution(which_samples, *it,
180 if (_got_solution_flag)
181 inlier_accept_count = _iBestInlierAcceptCount;
183 this->EvaluateSolution (*it, _got_solution_flag,
184 inlier_set, inlier_accept_count,
185 inlier_evaluate_score, ok_to_terminate_flag);
187 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
188 "RANSACPreKnowledge: minimal/refined "
189 <<sample_accept_count<<
"/"
190 <<inlier_accept_count<<
" "<<sample_evaluate_score<<
"/"
191 <<inlier_evaluate_score<<std::endl);
196 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
197 "RANSACPreKnowledge: (note) refine_solution returned false"
202 if (!good_flag || (inlier_accept_count < sample_accept_count)) {
203 BCDOUT(D_RANSAC_SOLVE_MASTER,
"RANSAC: (note) Linear solution"
204 <<
" overrides refined !!! \n");
206 inlier_accept_count = sample_accept_count;
207 inlier_evaluate_score = sample_evaluate_score;
208 *it = samplesolution;
209 ok_to_terminate_flag = ok_to_terminate_with_linear;
212 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
213 "RANSACPreKnowledge: (note) either good_flag=false or "
214 <<
"_bRefineSolution=false"<<std::endl);
222 BCDOUT(D_RANSAC_SAMPLE_QUALITY,
"."<<std::flush);
223 best_so_far_flag =
false;
225 if (!_got_solution_flag) {
226 _got_solution_flag =
true;
227 best_so_far_flag =
true;
229 if (!_bRefineSolution) {
230 if (sample_accept_count > _iBestSampleAcceptCount ||
231 (sample_accept_count == _iBestSampleAcceptCount &&
232 sample_evaluate_score < _dBestSampleEvaluateScore))
233 best_so_far_flag =
true;
235 if (inlier_accept_count > _iBestInlierAcceptCount ||
236 (inlier_accept_count == _iBestInlierAcceptCount &&
237 inlier_evaluate_score < _dBestInlierEvaluateScore))
238 best_so_far_flag =
true;
244 if (best_so_far_flag) {
245 _iBestSampleAcceptCount = sample_accept_count;
246 _dBestSampleEvaluateScore =
247 (sample_evaluate_score<inlier_evaluate_score) ?
248 sample_evaluate_score : inlier_evaluate_score;
249 _iBestInlierAcceptCount = inlier_accept_count;
250 _dBestInlierEvaluateScore = inlier_evaluate_score;
253 inliers = inlier_set;
256 if (_bRefineSolution){
257 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
"RANSACPreKnowledge: PreGuess "
258 <<
" is best so far. " <<
"Inliers/score "
259 <<_iBestInlierAcceptCount <<
"/"
260 <<_dBestInlierEvaluateScore<<
": "<<solution<<std::endl);
262 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
"RANSACPreKnowledge: PreGuess "
263 <<
" is best so far. " <<
"Inliers/score "
264 << _iBestSampleAcceptCount <<
"/"
265 <<_dBestSampleEvaluateScore<<
": "<<solution<<std::endl);
268 if (ok_to_terminate_flag){
269 termination_check_active_flag =
true;
270 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
271 "RANSACPreKnowledge: termination_check_active_flag = true");
276 BCDOUT(D_RANSAC_SAMPLE_QUALITY,
"*"<<std::flush);
281 if (termination_check_active_flag) {
282 BCDOUT(D_RANSAC_SAMPLE_QUALITY,
"P" << std::endl);
283 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
284 "VERY GOOD preguess! Skipping RANSAC." << std::endl);
288 BCDOUT(D_RANSAC_PRE_KNOWLEDGE,
289 "RANSACPreKnowledge: No good preguess. Calling RANSAC without "
298 #endif // header defined
virtual void ClearGuesses()
clear all guesses
RANSACPreKnowledge(unsigned int sample_size, unsigned int max_solutions_per_sample=1, bool refine_solution=false)
Construct a RANSAC object that takes samples of size sample_size, returning at most max_solutions_per...
virtual int SolveMaster(double inlying_data_fraction, SolutionType &solution, std::vector< bool > &inliers)
Main computation function called by user.
Extension of RANSAC algorithm: user-supplied guesses are evaluated and refined using RANSAC criterion...
virtual void AddGoodGuesses(const std::vector< SolutionType > &Guesses)
add some guessed solutions which are tested by RANSAC later
virtual int SolveMaster(double inlying_data_fraction, SolutionType &solution, std::vector< bool > &inliers)
checks UserGuesses and calls RANSAC::SolveMaster
virtual ~RANSACPreKnowledge()
default destructor does nothing
long int NewDebugLevel(const std::string &name)
creates a new debuglevel
Generic abstract base class for RANSAC (RANdom SAmple Consensus) estimators.
std::vector< SolutionType > UserGuesses_
vector of guesses to be checked, which were manually supplied by user