OpenGV
A library for solving calibrated central and non-central geometric vision problems
SampleConsensusProblem.hpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Authors: Laurent Kneip & Paul Furgale *
3  * Contact: kneip.laurent@gmail.com *
4  * License: Copyright (c) 2013 Laurent Kneip, ANU. All rights reserved. *
5  * *
6  * Redistribution and use in source and binary forms, with or without *
7  * modification, are permitted provided that the following conditions *
8  * are met: *
9  * * Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * * Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in the *
13  * documentation and/or other materials provided with the distribution. *
14  * * Neither the name of ANU nor the names of its contributors may be *
15  * used to endorse or promote products derived from this software without *
16  * specific prior written permission. *
17  * *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"*
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
21  * ARE DISCLAIMED. IN NO EVENT SHALL ANU OR THE CONTRIBUTORS BE LIABLE *
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *
28  * SUCH DAMAGE. *
29  ******************************************************************************/
30 
31 //Note: has been derived from ROS
32 
41 #ifndef OPENGV_SAC_SAMPLECONSENSUSPROBLEM_HPP_
42 #define OPENGV_SAC_SAMPLECONSENSUSPROBLEM_HPP_
43 
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <random>
47 #include <functional>
48 #include <memory>
49 #include <ctime>
50 
54 namespace opengv
55 {
59 namespace sac
60 {
61 
66 template<typename MODEL_T>
68 {
69 public:
71  typedef MODEL_T model_t;
72 
78  SampleConsensusProblem( bool randomSeed = true );
82  virtual ~SampleConsensusProblem();
83 
90  virtual void getSamples( int &iterations, std::vector<int> &samples );
91 
98  virtual bool isSampleGood( const std::vector<int> & sample ) const;
99 
104  std::shared_ptr< std::vector<int> > getIndices() const;
105 
110  void drawIndexSample( std::vector<int> & sample );
111 
117  virtual int getSampleSize() const = 0;
118 
126  virtual bool computeModelCoefficients(
127  const std::vector<int> & indices,
128  model_t & outModel) const = 0;
129 
137  virtual void optimizeModelCoefficients(
138  const std::vector<int> & inliers,
139  const model_t & model_coefficients,
140  model_t & optimized_coefficients ) = 0;
141 
150  virtual void getSelectedDistancesToModel(
151  const model_t & model,
152  const std::vector<int> & indices,
153  std::vector<double> & scores ) const = 0;
154 
162  virtual void getDistancesToModel(
163  const model_t & model_coefficients,
164  std::vector<double> &distances );
165 
174  virtual void selectWithinDistance(
175  const model_t &model_coefficients,
176  const double threshold,
177  std::vector<int> &inliers );
178 
187  virtual int countWithinDistance(
188  const model_t &model_coefficients,
189  const double threshold );
190 
195  void setIndices( const std::vector<int> & indices );
196 
201  void setUniformIndices( int N );
202 
207  int rnd();
208 
209 
210 
213 
218  std::shared_ptr< std::vector<int> > indices_;
219 
221  std::vector<int> shuffled_indices_;
222 
224  std::mt19937 rng_alg_;
225 
227  std::shared_ptr< std::uniform_int_distribution<> > rng_dist_;
228 
230  std::shared_ptr< std::function<int()> > rng_gen_;
231 
232 };
233 
234 } // namespace sac
235 } // namespace opengv
236 
237 #include "implementation/SampleConsensusProblem.hpp"
238 
239 #endif /* OPENGV_SAC_SAMPLECONSENSUSPROBLEM_HPP_ */
std::shared_ptr< std::uniform_int_distribution<> > rng_dist_
std-based random number generator distribution.
Definition: SampleConsensusProblem.hpp:227
std::shared_ptr< std::vector< int > > indices_
Definition: SampleConsensusProblem.hpp:218
The namespace of this library.
Definition: AbsoluteAdapterBase.hpp:47
virtual void optimizeModelCoefficients(const std::vector< int > &inliers, const model_t &model_coefficients, model_t &optimized_coefficients)=0
Refine the model coefficients over a given set (inliers). Needs implementation in the child-class...
std::mt19937 rng_alg_
std-based random number generator algorithm.
Definition: SampleConsensusProblem.hpp:224
void setIndices(const std::vector< int > &indices)
Set the indices_ variable (see member-description).
SampleConsensusProblem(bool randomSeed=true)
Contructor.
virtual bool isSampleGood(const std::vector< int > &sample) const
Check if a set of samples for model generation is degenerate.
int rnd()
Get a random number.
virtual void selectWithinDistance(const model_t &model_coefficients, const double threshold, std::vector< int > &inliers)
Select all the inlier samples whith respect to given model coefficients.
std::vector< int > shuffled_indices_
Definition: SampleConsensusProblem.hpp:221
int max_sample_checks_
Definition: SampleConsensusProblem.hpp:212
virtual void getSelectedDistancesToModel(const model_t &model, const std::vector< int > &indices, std::vector< double > &scores) const =0
Compute the distances of all samples whith respect to given model coefficients. Needs implementation ...
Definition: SampleConsensusProblem.hpp:67
virtual ~SampleConsensusProblem()
Destructor.
virtual int getSampleSize() const =0
Get the number of samples needed for a hypothesis generation. Needs implementation in the child class...
virtual int countWithinDistance(const model_t &model_coefficients, const double threshold)
Count all the inlier samples whith respect to given model coefficients.
virtual void getDistancesToModel(const model_t &model_coefficients, std::vector< double > &distances)
Compute the distances of all samples which respect to given model coefficients.
std::shared_ptr< std::vector< int > > getIndices() const
Get a pointer to the vector of indices used.
virtual void getSamples(int &iterations, std::vector< int > &samples)
Get samples for hypothesis generation.
void setUniformIndices(int N)
Use this method if you want to use all samples.
std::shared_ptr< std::function< int()> > rng_gen_
std-based random number generator.
Definition: SampleConsensusProblem.hpp:230
virtual bool computeModelCoefficients(const std::vector< int > &indices, model_t &outModel) const =0
Compute a model from a set of samples. Needs implementation in the child-class.
void drawIndexSample(std::vector< int > &sample)
Sub-function for getting samples for hypothesis generation.
MODEL_T model_t
Definition: SampleConsensusProblem.hpp:71