Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RANSACEvaluatorInterface.hh
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003, 2004 (see file CONTACTS for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21 #ifndef __RANSACEvaluatorInterface_hh__
22 #define __RANSACEvaluatorInterface_hh__
23 
24 #include <Base/Math/Random.hh>
25 
26 #include <vector>
27 #include <algorithm>
28 
29 namespace BIAS {
30 
31  /** @class RANSACEvaluatorInterface
32  @brief Interface for computation of solutions and evaluation of
33  measurements. This interface is used by the PreemptiveRANSAC and the
34  COSAC classes and decouples the problem (for example Homography
35  estimation) from the algorithm (for example PreemptiveRANSAC or
36  Complete Sampling Consesus / COSAC).
37 
38  A standard implementation for sample generation is also given.
39 
40  @relates PreemptiveRANSAC, COSAC
41 
42  @author woelk 01/2010 (c) www.vision-n.de */
43  template <class SolutionType>
45  {
46  public:
47  /** Returns the number of available measurements */
48  virtual unsigned GetNumMeasurements() const = 0;
49 
50  /** Returns the minimum required samples for computation of a solution */
51  virtual unsigned GetMinNumSamplesForSolutionComputation() const = 0;
52 
53  /** Checks whether a certain sample (i.e. data) is an inlier and computes
54  the associated score. Good solutions result in a small score, bad
55  solutions result in big scores. Scores may also be negative. It is
56  often a good idea do define the score as the logarithm of some
57  normalized distance measure. */
58  virtual bool IsInlier(const SolutionType& solution,
59  const unsigned data_index,
60  double& score) = 0;
61 
62  /** @brief Compute solution(s) for the given set of samples
63  @return the number of solutions found.
64 
65  The which_samples array will be of length sample_size, selecting the
66  indices to be fit to from the subclass's data array. */
67  virtual int GetSampleSolutions(const std::vector<unsigned> &which_samples,
68  std::vector<SolutionType> &solutions)= 0;
69 
70  /** @brief Refine a solution based on the inliers vector */
71  virtual bool RefineSolution(const std::vector<bool>& inliers,
72  SolutionType& solution) = 0;
73 
74  /** @brief randomly generates the indizes of the samples used for
75  computation of the solution. This function can be overloaded, for
76  example when it is of advantage when the samples span a big
77  area (for example in the image) */
78  virtual bool GenerateSamples(const unsigned sample_index,
79  const unsigned sample_size,
80  const unsigned data_size,
81  std::vector<unsigned>& which_samples);
82 
83  protected:
84  /// purely random generation of sample indizes
85  bool GenerateSamplesRandom_(const unsigned sample_index,
86  const unsigned sample_size,
87  const unsigned data_size,
88  std::vector<unsigned>& which_samples);
89 
90  }; // class
91 
92  //////////////////////////////////////////////////////////////////////////
93  // implementation
94  /////////////////////////////////////////////////////////////////////////
95 
96  template <class SolutionType>
98  GenerateSamples(const unsigned sample_index,
99  const unsigned sample_size,
100  const unsigned data_size,
101  std::vector<unsigned>& which_samples)
102  {
103  return GenerateSamplesRandom_(sample_index, sample_size, data_size,
104  which_samples);
105  }
106 
107 
108  template <class SolutionType>
110  GenerateSamplesRandom_(const unsigned sample_index,
111  const unsigned sample_size,
112  const unsigned data_size,
113  std::vector<unsigned>& which_samples)
114  {
115  BIASASSERT(sample_size<=data_size);
116  BIASASSERT(data_size>0);
117 
118  // initialzing random with 0 and 1 does not seem to work well
119  //Random ran(sample_index+rand());
120  Random ran(sample_index);
121  unsigned count=0;
122  unsigned index;
123  which_samples.clear();
124  which_samples.reserve(sample_size);
125  const unsigned max_tries = 10000;
126  do {
127  index = ran.GetUniformDistributedInt(0u, data_size-1);
128  if (std::find(which_samples.begin(), which_samples.end(), index)==
129  which_samples.end()){
130  which_samples.push_back(index);
131  count++;
132  //std::cout << index << " ";
133  }
134  } while (count<sample_size && count<max_tries);
135  //std::cout << std::endl;
136  if (count>=max_tries){
137  BIASERR("error generating samples (max_tries reached)");
138  return false;
139  }
140 
141  return true;
142  }
143 } // namespace
144 
145 #endif // __RANSACEvaluatorInterface_hh__
Interface for computation of solutions and evaluation of measurements.
virtual bool RefineSolution(const std::vector< bool > &inliers, SolutionType &solution)=0
Refine a solution based on the inliers vector.
virtual bool IsInlier(const SolutionType &solution, const unsigned data_index, double &score)=0
Checks whether a certain sample (i.e.
int GetUniformDistributedInt(const int min, const int max)
get uniform distributed random variable including min/max
Definition: Random.cpp:139
virtual unsigned GetMinNumSamplesForSolutionComputation() const =0
Returns the minimum required samples for computation of a solution.
virtual bool GenerateSamples(const unsigned sample_index, const unsigned sample_size, const unsigned data_size, std::vector< unsigned > &which_samples)
randomly generates the indizes of the samples used for computation of the solution.
virtual int GetSampleSolutions(const std::vector< unsigned > &which_samples, std::vector< SolutionType > &solutions)=0
Compute solution(s) for the given set of samples.
virtual unsigned GetNumMeasurements() const =0
Returns the number of available measurements.
bool GenerateSamplesRandom_(const unsigned sample_index, const unsigned sample_size, const unsigned data_size, std::vector< unsigned > &which_samples)
purely random generation of sample indizes
class for producing random numbers from different distributions
Definition: Random.hh:51