Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
BIAS::RANSAC< SolutionType > Class Template Reference

Generic abstract base class for RANSAC (RANdom SAmple Consensus) estimators. More...

#include <MathAlgo/RANSAC.hh>

+ Inheritance diagram for BIAS::RANSAC< SolutionType >:
+ Collaboration diagram for BIAS::RANSAC< SolutionType >:

Public Member Functions

void AddDebugLevel (const long int lv)
 
void AddDebugLevel (const std::string &name)
 
bool DebugLevelIsSet (const long int lv) const
 
bool DebugLevelIsSet (const std::string &name) const
 
virtual bool EvaluateSolution (const SolutionType &solution, bool existing_solution_flag, std::vector< bool > &inlier, int &accept_count, double &evaluate_score, bool &ok_to_terminate_flag)
 evaluate the goodness of a solution More...
 
virtual bool GenerateSamples (int sample_index, std::vector< unsigned int > &which_samples)
 pick a set of samples More...
 
bool GenerateSamplesEvenspace (int sample_index, std::vector< unsigned int > &sample_table)
 
bool GenerateSamplesRandom (int sample_index, std::vector< unsigned int > &sampletable)
 
int GetDebugLevel () const
 
std::ostream & GetDebugStream () const
 
void GetDebugStream (std::ostream &os) const
 
double GetFinalScore () const
 
virtual int GetSampleSolutions (std::vector< unsigned int > &which_samples, std::vector< SolutionType > &solutions)
 Compute solution(s) for the given set of samples. More...
 
void Init (unsigned int sample_size, unsigned int max_solutions_per_sample=1, bool refine_solution=false, bool greedy=false)
 
long int Name2DebugLevel (const std::string &name) const
 looks up a debuglevel in the internal map, returns 0 if not found More...
 
long int NewDebugLevel (const std::string &name)
 creates a new debuglevel More...
 
void PrintDebugLevel (std::ostream &os=std::cout) const
 
 RANSAC ()
 
 RANSAC (unsigned int sample_size, unsigned int max_solutions_per_sample=1, bool refine_solution=false, bool greedy=false)
 Construct a RANSAC object that takes samples of size sample_size, returning at most max_solutions_per_sample solutions each time. More...
 
virtual bool RefineSolution (std::vector< unsigned int > &which_samples, SolutionType &solution, std::vector< bool > &new_inliers)
 refine previously computed solution based on its inliers More...
 
void RemoveDebugLevel (const long int lv)
 
void RemoveDebugLevel (const std::string &name)
 
void SetDebugLevel (const long int lv)
 
void SetDebugLevel (const std::string &name)
 
void SetDebugStream (const std::ostream &os)
 
void SetMaxSamples (unsigned int max_samples)
 Explicitly set max number of samples. More...
 
void SetSampleCount (unsigned int uiSamples)
 Explicitly set number of samples. More...
 
void ShowDebugLevel (std::ostream &os=std::cout) const
 prints all internally known debuglevels More...
 
virtual int SolveMaster (double inlying_data_fraction, SolutionType &solution, std::vector< bool > &inliers)
 Main computation function called by user. More...
 
virtual ~RANSAC ()
 

Static Public Member Functions

static long int GetGlobalDebugLevel ()
 
static void SetGlobalDebugLevel (long int lev)
 

Protected Member Functions

long ConsumeNextFreeDebuglevel_ ()
 returns the next available debuglevel More...
 

Static Protected Member Functions

static int ComputeSampleCount (double inlying_data_fraction, double desired_prob_good, unsigned int sample_size)
 

Protected Attributes

bool _bRefineSolution
 do we want to refine a good sample (one for which evaluate solution returned true)? More...
 
double _dBestInlierEvaluateScore
 
double _dBestSampleEvaluateScore
 
double _dLastBestSampleEvaluateScore
 
double _dProbabilityOfGoodSolution
 prob. for computing number of samples, set close to 1 More...
 
double _dSoSmallSolutionChange
 If all other conditions are right, and the fractional change in the solution statistics is less than this, stop sampling. More...
 
bool _got_solution_flag
 if we already have a solution for which EvaluateSolution returned true More...
 
bool _greedy
 if true, the first acceptable solution is returned More...
 
int _iBestInlierAcceptCount
 refined best values More...
 
int _iBestSampleAcceptCount
 sampled best values More...
 
long int _liDebugLevel
 
long int _liNextDebugLevel
 new concept, debuglevel are managed here in the debug class More...
 
std::map< std::string, long int > _String2Debuglevel
 
unsigned _uiDataSize
 number of data elements needed to compute a solution More...
 
unsigned int _uiExplicitSampleNo
 use this number of samples More...
 
unsigned int _uiMaxSamples
 absolute max number of samples More...
 
unsigned _uiMaxSolutionsPerSample
 max number of solutions that are to be computed for one sample set More...
 
unsigned _uiSampleSize
 number of samples available More...
 

Static Protected Attributes

static std::ostream _zDebugStream
 
static long int GlobalDebugLevel = 0
 

Detailed Description

template<class SolutionType>
class BIAS::RANSAC< SolutionType >

Generic abstract base class for RANSAC (RANdom SAmple Consensus) estimators.

To use this class, derive a class from it in which the following methods are overloaded:

RANSAC::GenerateSamples can be overloaded e.g. if for an instance an even- spaced sampling is desired.

For examples how to use this class see

The function SolveMaster was ported from TargetJR -> PMatrixRANSAC by frahm and from there on further ported to this class from woelk.

Note
Since this is a generic templated class the implementation has to go into the header file.
Author
frahm, woelk

Definition at line 80 of file RANSAC.hh.

Constructor & Destructor Documentation

template<class SolutionType >
BIAS::RANSAC< SolutionType >::RANSAC ( )
inline

Definition at line 317 of file RANSAC.hh.

template<class SolutionType >
BIAS::RANSAC< SolutionType >::RANSAC ( unsigned int  sample_size,
unsigned int  max_solutions_per_sample = 1,
bool  refine_solution = false,
bool  greedy = false 
)
inline

Construct a RANSAC object that takes samples of size sample_size, returning at most max_solutions_per_sample solutions each time.

If the refine_solution flag is true, a routine has been supplied to refine the initial fits before evaluation.

In order to use the RANSAC, a child class from class RANSAC or class RANSACPreKnowledge has to be implemented with the methods Evalulate Solution, GetSampleSolutions, and RefineSolution. A Compute method needs to do all the preparations and call the SolveMaster method. See ExampleRANSACPlane to get an idea of how to use the parameters.

Definition at line 338 of file RANSAC.hh.

template<class SolutionType >
BIAS::RANSAC< SolutionType >::~RANSAC ( )
inlinevirtual

Definition at line 373 of file RANSAC.hh.

Member Function Documentation

void BIAS::Debug::AddDebugLevel ( const long int  lv)
inlineinherited
void BIAS::Debug::AddDebugLevel ( const std::string &  name)
inlineinherited

Definition at line 362 of file Debug.hh.

template<class SolutionType >
int BIAS::RANSAC< SolutionType >::ComputeSampleCount ( double  inlying_data_fraction,
double  desired_prob_good,
unsigned int  sample_size 
)
inlinestaticprotected

Definition at line 863 of file RANSAC.hh.

long BIAS::Debug::ConsumeNextFreeDebuglevel_ ( )
inlineprotectedinherited

returns the next available debuglevel

Author
woelk 09/2006

Definition at line 521 of file Debug.hh.

bool BIAS::Debug::DebugLevelIsSet ( const long int  lv) const
inlineinherited
bool BIAS::Debug::DebugLevelIsSet ( const std::string &  name) const
inlineinherited

Definition at line 350 of file Debug.hh.

template<class SolutionType>
bool BIAS::RANSAC< SolutionType >::EvaluateSolution ( const SolutionType &  solution,
bool  existing_solution_flag,
std::vector< bool > &  inlier,
int &  accept_count,
double &  evaluate_score,
bool &  ok_to_terminate_flag 
)
inlinevirtual

evaluate the goodness of a solution

For the specified solution, set

  • an accept count indicating the number of data which are inliers to the given solution,
  • the inlier flag for each datum to true if the datum is inlying
  • an evaluate score (typically the average residual) indicating the goodness of fit of the inliers - the solver code requires a positive number where zero is a perfect solution, larger positive scores mean a worse solution.
  • an ok_to_terminate flag which is set TRUE if the solution is good enough that the robust solver can discontinue search for a better solution. Only happens, if greedy is set to true.
  • The return value tells the algorithm, if the solution is good enough to be refined further - meaning, if set to true and if RefineSolution_ is set to true, the solution will be refined. In the example below, true is returned even if only one samples has been found to be an inlier. Because refinement usually takes quite a while, it is sensible to use a higher threshold. For example the use of a minimum inlier fraction via a private variable in the child class can be tested on.

If existing_solution_flag is true, then the best accept count so far is in accept_count. The function can terminate early if it is not going to beat this.

A skeleton implementation of this function would be written as follows:

*  int my_accept_count = 0;
*  for (unsigned int i=0; i<_uiDataSize; i++) {
*    inlier[i] = Consistent(corner_matches[i], solution);
*    if (inlier[i]) {
*      my_accept_count++;
*      evaluate_score += Residual(corner_matches[i], solution);
*      if (existing_solution_flag && my_accept_count>accept_count)
*        break;
*    }
*  }
*  accept_count=my_accept_count;
*  evaluate_score=(accept_count!=0)?(evaluate_score/accept_count):
*    (DBL_MAX);
*  if (evaluate_score < solution_error_threshold &&
*      (double)accept_count/(double)_uiDataSize > min_inlier_fraction)
*    ok_to_terminate_flag=true;
*  else
*    ok_to_terminate_flag=false;
*  return (accept_count!=0) // maybe even (accept_count>inl_frac)
*  

The line if (existing_solution_flag && my_accept_count>accept_count) break; causes the algorithm to not determine the score and acceptcount correctly, only so far as to top the so far best solution. It can be used for very fast solutions, however if the exact inliers are needed and refineSolution_ is set to false, the evaluation should not be omnitted.

Parameters
solution(in) solution to be evaluated
existing_solution_flag(in) if true the best accept_cout so far is given in accept_count
accept_count(in/out) input: best accept_cout so far if existing_solution_flag==true, output: number of inliers to the current solution
inlier(out) inlier[i] ==> datum i is inlying
evaluate_score(out) a score for the solution, 0 means ideal solution, larger score means worse solution
ok_to_termiate_flag(out) if true, RANSACing will stop

Reimplemented in BIAS::PlaneRANSAC, and BIAS::RANSAC_double.

Definition at line 748 of file RANSAC.hh.

template<class SolutionType >
bool BIAS::RANSAC< SolutionType >::GenerateSamples ( int  sample_index,
std::vector< unsigned int > &  which_samples 
)
inlinevirtual

pick a set of samples

Override this to specialize the technique used to generate samples. By default, random sampling is assumed but implementations may wish to use the latin-square sampling provided by generate_sample_evenspace (below), or some other problem-specific sampling strategy.

Definition at line 768 of file RANSAC.hh.

template<class SolutionType >
bool BIAS::RANSAC< SolutionType >::GenerateSamplesEvenspace ( int  sample_index,
std::vector< unsigned int > &  sample_table 
)
inline

Definition at line 775 of file RANSAC.hh.

template<class SolutionType >
bool BIAS::RANSAC< SolutionType >::GenerateSamplesRandom ( int  sample_index,
std::vector< unsigned int > &  sampletable 
)
inline

Definition at line 813 of file RANSAC.hh.

References BIAS::Random::GetUniformDistributedInt().

int BIAS::Debug::GetDebugLevel ( ) const
inlineinherited
std::ostream& BIAS::Debug::GetDebugStream ( ) const
inlineinherited

Definition at line 405 of file Debug.hh.

void BIAS::Debug::GetDebugStream ( std::ostream &  os) const
inlineinherited

Definition at line 414 of file Debug.hh.

template<class SolutionType>
double BIAS::RANSAC< SolutionType >::GetFinalScore ( ) const
inline

Definition at line 260 of file RANSAC.hh.

static long int BIAS::Debug::GetGlobalDebugLevel ( )
inlinestaticinherited

Definition at line 431 of file Debug.hh.

template<class SolutionType>
int BIAS::RANSAC< SolutionType >::GetSampleSolutions ( std::vector< unsigned int > &  which_samples,
std::vector< SolutionType > &  solutions 
)
inlinevirtual

Compute solution(s) for the given set of samples.

Returns
the number of solutions found.

The which_samples array will be of length sample_size, selecting the indices to be fit to from the subclass's data array.

Reimplemented in BIAS::PlaneRANSAC, and BIAS::RANSAC_double.

Definition at line 738 of file RANSAC.hh.

template<class SolutionType >
void BIAS::RANSAC< SolutionType >::Init ( unsigned int  sample_size,
unsigned int  max_solutions_per_sample = 1,
bool  refine_solution = false,
bool  greedy = false 
)
inline

Definition at line 360 of file RANSAC.hh.

Referenced by BIAS::RANSAC_double::Init(), and BIAS::PlaneRANSAC::Init().

long int BIAS::Debug::Name2DebugLevel ( const std::string &  name) const
inlineinherited

looks up a debuglevel in the internal map, returns 0 if not found

Author
woelk 09/2006

Definition at line 454 of file Debug.hh.

long int BIAS::Debug::NewDebugLevel ( const std::string &  name)
inlineinherited
void BIAS::Debug::PrintDebugLevel ( std::ostream &  os = std::cout) const
inlineinherited

Definition at line 383 of file Debug.hh.

template<class SolutionType>
bool BIAS::RANSAC< SolutionType >::RefineSolution ( std::vector< unsigned int > &  which_samples,
SolutionType &  solution,
std::vector< bool > &  new_inliers 
)
inlinevirtual

refine previously computed solution based on its inliers

If implemented, this function should compute a more accurate solution, e.g. a least squares on all inliers, possibly using as an initial estimate the solution in solution. The output array new_inliers should be set to reflect the inliers to the new solution.

Returns
false if no better solution than the initial could be found

Reimplemented in BIAS::PlaneRANSAC, and BIAS::RANSAC_double.

Definition at line 758 of file RANSAC.hh.

void BIAS::Debug::RemoveDebugLevel ( const long int  lv)
inlineinherited

Definition at line 369 of file Debug.hh.

void BIAS::Debug::RemoveDebugLevel ( const std::string &  name)
inlineinherited

Definition at line 376 of file Debug.hh.

void BIAS::Debug::SetDebugLevel ( const long int  lv)
inlineinherited
void BIAS::Debug::SetDebugLevel ( const std::string &  name)
inlineinherited

Definition at line 325 of file Debug.hh.

void BIAS::Debug::SetDebugStream ( const std::ostream &  os)
inlineinherited

Definition at line 398 of file Debug.hh.

static void BIAS::Debug::SetGlobalDebugLevel ( long int  lev)
inlinestaticinherited

Definition at line 424 of file Debug.hh.

template<class SolutionType>
void BIAS::RANSAC< SolutionType >::SetMaxSamples ( unsigned int  max_samples)
inline

Explicitly set max number of samples.

Ransacing will stop after max_samples samples, regardless of solution quality. Normally, more than a few thousand samples signifies a problem.

Definition at line 112 of file RANSAC.hh.

template<class SolutionType>
void BIAS::RANSAC< SolutionType >::SetSampleCount ( unsigned int  uiSamples)
inline

Explicitly set number of samples.

Explicitly set number of samples to make in SolveMaster, i.e. Value of ComputeSampleCount() is ignored if this function is called with a strictly positive value uiSamples. Note: SolveMaster performs at most min(_uiMaxSamples, uiSamples) iterations

Author
koeser

Definition at line 124 of file RANSAC.hh.

void BIAS::Debug::ShowDebugLevel ( std::ostream &  os = std::cout) const
inlineinherited

prints all internally known debuglevels

Author
woelk 09/2006

Definition at line 496 of file Debug.hh.

template<class SolutionType>
int BIAS::RANSAC< SolutionType >::SolveMaster ( double  inlying_data_fraction,
SolutionType &  solution,
std::vector< bool > &  inliers 
)
inlinevirtual

Main computation function called by user.

This function is called to run the computation. On output, solution is set to the best solution found and inliers indicate which data were inliers to that solution. The inlying_data_fraction is responsible for the maximal number of sample sets taken from the data. The smaller the inlying_data_fraction, the more sample sets are taken

Attention
Do NOT override this function in derived classes, unless you want to do modifications in the RANSAC algorithm itself (and usually you dont want to do this ...) input : inlying_data_fraction expected inlier fraction output: solution solution inliers inlier flags
Returns
<0 indicates an error, otherwise number of chosen sample
Author
frahm, woelk

Reimplemented in BIAS::RANSACPreKnowledge< SolutionType >.

Definition at line 378 of file RANSAC.hh.

Referenced by BIAS::RANSACPreKnowledge< SolutionType >::SolveMaster().

Member Data Documentation

template<class SolutionType>
bool BIAS::RANSAC< SolutionType >::_bRefineSolution
protected

do we want to refine a good sample (one for which evaluate solution returned true)?

Definition at line 273 of file RANSAC.hh.

template<class SolutionType>
double BIAS::RANSAC< SolutionType >::_dBestInlierEvaluateScore
protected

Definition at line 282 of file RANSAC.hh.

template<class SolutionType>
double BIAS::RANSAC< SolutionType >::_dBestSampleEvaluateScore
protected

Definition at line 277 of file RANSAC.hh.

template<class SolutionType>
double BIAS::RANSAC< SolutionType >::_dLastBestSampleEvaluateScore
protected

Definition at line 278 of file RANSAC.hh.

Referenced by BIAS::RANSAC< double >::GetFinalScore().

template<class SolutionType>
double BIAS::RANSAC< SolutionType >::_dProbabilityOfGoodSolution
protected

prob. for computing number of samples, set close to 1

Definition at line 291 of file RANSAC.hh.

template<class SolutionType>
double BIAS::RANSAC< SolutionType >::_dSoSmallSolutionChange
protected

If all other conditions are right, and the fractional change in the solution statistics is less than this, stop sampling.

Definition at line 295 of file RANSAC.hh.

template<class SolutionType>
bool BIAS::RANSAC< SolutionType >::_got_solution_flag
protected

if we already have a solution for which EvaluateSolution returned true

Definition at line 298 of file RANSAC.hh.

template<class SolutionType>
bool BIAS::RANSAC< SolutionType >::_greedy
protected

if true, the first acceptable solution is returned

Definition at line 301 of file RANSAC.hh.

template<class SolutionType>
int BIAS::RANSAC< SolutionType >::_iBestInlierAcceptCount
protected

refined best values

Definition at line 281 of file RANSAC.hh.

template<class SolutionType>
int BIAS::RANSAC< SolutionType >::_iBestSampleAcceptCount
protected

sampled best values

Definition at line 276 of file RANSAC.hh.

long int BIAS::Debug::_liDebugLevel
protectedinherited

Definition at line 510 of file Debug.hh.

Referenced by BIAS::Debug::operator=(), and BIAS::ImageBase::operator=().

long int BIAS::Debug::_liNextDebugLevel
protectedinherited

new concept, debuglevel are managed here in the debug class

Definition at line 516 of file Debug.hh.

Referenced by BIAS::Debug::operator=().

std::map<std::string, long int> BIAS::Debug::_String2Debuglevel
protectedinherited

Definition at line 517 of file Debug.hh.

Referenced by BIAS::Debug::operator=().

template<class SolutionType>
unsigned BIAS::RANSAC< SolutionType >::_uiDataSize
protected

number of data elements needed to compute a solution

Definition at line 270 of file RANSAC.hh.

template<class SolutionType>
unsigned int BIAS::RANSAC< SolutionType >::_uiExplicitSampleNo
protected

use this number of samples

Definition at line 288 of file RANSAC.hh.

Referenced by BIAS::RANSAC< double >::SetSampleCount().

template<class SolutionType>
unsigned int BIAS::RANSAC< SolutionType >::_uiMaxSamples
protected

absolute max number of samples

Definition at line 285 of file RANSAC.hh.

Referenced by BIAS::RANSAC< double >::SetMaxSamples().

template<class SolutionType>
unsigned BIAS::RANSAC< SolutionType >::_uiMaxSolutionsPerSample
protected

max number of solutions that are to be computed for one sample set

Definition at line 267 of file RANSAC.hh.

template<class SolutionType>
unsigned BIAS::RANSAC< SolutionType >::_uiSampleSize
protected

number of samples available

Definition at line 264 of file RANSAC.hh.

std::ostream BIAS::Debug::_zDebugStream
staticprotectedinherited

Definition at line 511 of file Debug.hh.

Referenced by BIAS::Debug::operator=().

long int BIAS::Debug::GlobalDebugLevel = 0
staticprotectedinherited

Definition at line 513 of file Debug.hh.


The documentation for this class was generated from the following file: