OpenGV
A library for solving calibrated central and non-central geometric vision problems
MultiSampleConsensusProblem.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 
43 #ifndef OPENGV_SAC_MULTISAMPLECONSENSUSPROBLEM_HPP_
44 #define OPENGV_SAC_MULTISAMPLECONSENSUSPROBLEM_HPP_
45 
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <memory>
49 #include <functional>
50 #include <random>
51 #include <ctime>
52 
56 namespace opengv
57 {
61 namespace sac
62 {
63 
69 template<typename MODEL_T>
71 {
72 public:
74  typedef MODEL_T model_t;
75 
81  MultiSampleConsensusProblem( bool randomSeed = true );
86 
93  virtual void getSamples(
94  int &iterations,
95  std::vector< std::vector<int> > &samples );
96 
103  virtual bool isSampleGood(
104  const std::vector< std::vector<int> > & sample ) const;
105 
110  std::shared_ptr< std::vector< std::vector<int> > > getIndices() const;
111 
116  void drawIndexSample( std::vector< std::vector<int> > & sample );
117 
123  virtual std::vector<int> getSampleSizes() const = 0;
124 
132  virtual bool computeModelCoefficients(
133  const std::vector< std::vector<int> > & indices,
134  model_t & outModel) const = 0;
135 
143  virtual void optimizeModelCoefficients(
144  const std::vector< std::vector<int> > & inliers,
145  const model_t & model_coefficients,
146  model_t & optimized_coefficients ) = 0;
147 
156  virtual void getSelectedDistancesToModel(
157  const model_t & model,
158  const std::vector< std::vector<int> > & indices,
159  std::vector< std::vector<double> > & scores ) const = 0;
160 
168  virtual void getDistancesToModel(
169  const model_t & model_coefficients,
170  std::vector< std::vector<double> > &distances );
171 
180  virtual void selectWithinDistance(
181  const model_t &model_coefficients,
182  const double threshold,
183  std::vector< std::vector<int> > &inliers );
184 
193  virtual int countWithinDistance(
194  const model_t &model_coefficients,
195  const double threshold);
196 
201  void setIndices(const std::vector< std::vector<int> > & indices);
202 
207  void setUniformIndices(std::vector<int> N);
208 
213  int rnd();
214 
215 
216 
219 
224  std::shared_ptr< std::vector< std::vector<int> > > indices_;
225 
227  std::vector< std::vector<int> > shuffled_indices_;
228 
230  std::mt19937 rng_alg_;
231 
233  std::shared_ptr< std::uniform_int_distribution<> > rng_dist_;
234 
236  std::shared_ptr< std::function<int()> > rng_gen_;
237 
238 };
239 
240 } // namespace sac
241 } // namespace opengv
242 
243 #include "implementation/MultiSampleConsensusProblem.hpp"
244 
245 #endif /* OPENGV_SAC_MULTISAMPLECONSENSUS_PROBLEM_HPP_ */
virtual void optimizeModelCoefficients(const std::vector< 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...
void setUniformIndices(std::vector< int > N)
Use this method if you want to use all samples.
The namespace of this library.
Definition: AbsoluteAdapterBase.hpp:47
virtual void getDistancesToModel(const model_t &model_coefficients, std::vector< std::vector< double > > &distances)
Compute the distances of all samples which respect to given model coefficients.
std::shared_ptr< std::function< int()> > rng_gen_
std-based random number generator.
Definition: MultiSampleConsensusProblem.hpp:236
std::mt19937 rng_alg_
std-based random number generator algorithm.
Definition: MultiSampleConsensusProblem.hpp:230
std::shared_ptr< std::uniform_int_distribution<> > rng_dist_
std-based random number generator distribution.
Definition: MultiSampleConsensusProblem.hpp:233
virtual std::vector< int > getSampleSizes() const =0
Get the number of samples needed for a hypothesis generation. Needs implementation in the child class...
std::shared_ptr< std::vector< std::vector< int > > > indices_
Definition: MultiSampleConsensusProblem.hpp:224
Definition: MultiSampleConsensusProblem.hpp:70
virtual int countWithinDistance(const model_t &model_coefficients, const double threshold)
Count all the inlier samples whith respect to given model coefficients.
virtual ~MultiSampleConsensusProblem()
Destructor.
virtual bool isSampleGood(const std::vector< std::vector< int > > &sample) const
Check if a set of samples for model generation is degenerate.
virtual bool computeModelCoefficients(const std::vector< std::vector< int > > &indices, model_t &outModel) const =0
Compute a model from a set of samples. Needs implementation in the child-class.
virtual void getSelectedDistancesToModel(const model_t &model, const std::vector< std::vector< int > > &indices, std::vector< std::vector< double > > &scores) const =0
Compute the distances of all samples whith respect to given model coefficients. Needs implementation ...
int rnd()
Get a random number.
MODEL_T model_t
Definition: MultiSampleConsensusProblem.hpp:74
virtual void getSamples(int &iterations, std::vector< std::vector< int > > &samples)
Get samples for hypothesis generation.
MultiSampleConsensusProblem(bool randomSeed=true)
Contructor.
std::shared_ptr< std::vector< std::vector< int > > > getIndices() const
Get a pointer to the vector of multi-indices used.
virtual void selectWithinDistance(const model_t &model_coefficients, const double threshold, std::vector< std::vector< int > > &inliers)
Select all the inlier samples whith respect to given model coefficients.
int max_sample_checks_
Definition: MultiSampleConsensusProblem.hpp:218
void setIndices(const std::vector< std::vector< int > > &indices)
Set the indices_ variable (see member-description).
std::vector< std::vector< int > > shuffled_indices_
Definition: MultiSampleConsensusProblem.hpp:227
void drawIndexSample(std::vector< std::vector< int > > &sample)
Sub-function for getting samples for hypothesis generation.