MRPT  1.9.9
model_search.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <cstdint>
13 #include <set>
14 
15 namespace mrpt::math
16 {
17 /** Model search implementations: RANSAC and genetic algorithm
18  *
19  * The type \a TModelFit is a user-supplied struct/class that implements this
20  * interface:
21  * - Types:
22  * - \a Real : The numeric type to use (typ: double, float)
23  * - \a Model : The type of the model to be fitted (for example: A matrix, a
24  * TLine2D, a TPlane3D, ...)
25  * - Methods:
26  * - size_t getSampleCount() const : return the number of samples. This
27  * should not change during a model search.
28  * - bool fitModel( const std::vector<size_t>& useIndices, Model& model )
29  * const :
30  * This function fits a model to the data selected by the indices. The return
31  * value indicates the success, hence false means a degenerate case, where no
32  * model was found.
33  * - Real testSample( size_t index, const Model& model ) const : return some
34  * value that indicates how well a sample fits to the model. This way the
35  * thresholding is moved to the searching procedure and the model just tells how
36  * good a sample is.
37  *
38  * There are two methods provided in this class to fit a model:
39  * - \a ransacSingleModel (RANSAC): Just like mrpt::math::RANSAC_Template
40  *
41  * - \a geneticSingleModel (Genetic): Provides a mixture of a genetic and
42  * the ransac algorithm.
43  * Instead of selecting a set of data in each iteration, it takes more
44  * (ex. 10) and order these model
45  * using some fitness function: the average error of the inliers scaled
46  * by the number of outliers (This
47  * fitness might require some fine tuning). Than the (ex 10) new kernel
48  * for the next iteration is created as follows:
49  * - Take the best kernels (as for the original ransac)
50  * - Select two kernels ( with a higher probability for the better
51  * models) and let the new kernel be a subset of the two original kernels (
52  * additionally to leave the local minimums an additional random seed might
53  * appear - mutation)
54  * - Generate some new random samples.
55  *
56  * For an example of usage, see "samples/model_search_test/"
57  * \sa mrpt::math::RANSAC_Template, another RANSAC implementation where models
58  * can be matrices only.
59  *
60  * \author Zoltar Gaal
61  * \ingroup ransac_grp
62  */
64 {
65  private:
66  //! Select random (unique) indices from the 0..p_size sequence
67  void pickRandomIndex(
68  size_t p_size, size_t p_pick, std::vector<size_t>& p_ind);
69 
70  /** Select random (unique) indices from the set.
71  * The set is destroyed during pick */
72  void pickRandomIndex(
73  std::set<size_t> p_set, size_t p_pick, std::vector<size_t>& p_ind);
74 
75  public:
76  template <typename TModelFit>
77  bool ransacSingleModel(
78  const TModelFit& p_state, size_t p_kernelSize,
79  const typename TModelFit::Real& p_fitnessThreshold,
80  typename TModelFit::Model& p_bestModel, std::vector<size_t>& p_inliers);
81 
82  private:
83  template <typename TModelFit>
84  struct TSpecies
85  {
86  typename TModelFit::Model model;
87  std::vector<size_t> sample;
88  std::vector<size_t> inliers;
89  typename TModelFit::Real fitness;
90 
91  static bool compare(const TSpecies* p_a, const TSpecies* p_b)
92  {
93  return p_a->fitness < p_b->fitness;
94  }
95  };
96 
97  public:
98  template <typename TModelFit>
99  bool geneticSingleModel(
100  const TModelFit& p_state, size_t p_kernelSize,
101  const typename TModelFit::Real& p_fitnessThreshold,
102  size_t p_populationSize, size_t p_maxIteration,
103  typename TModelFit::Model& p_bestModel, std::vector<size_t>& p_inliers);
104 }; // end of class
105 
106 } // namespace mrpt::math
107 // Template implementations:
108 #include "model_search_impl.h"
std::vector< size_t > sample
Definition: model_search.h:87
Model search implementations: RANSAC and genetic algorithm.
Definition: model_search.h:63
bool ransacSingleModel(const TModelFit &p_state, size_t p_kernelSize, const typename TModelFit::Real &p_fitnessThreshold, typename TModelFit::Model &p_bestModel, std::vector< size_t > &p_inliers)
Run the ransac algorithm searching for a single model.
std::vector< size_t > inliers
Definition: model_search.h:88
This base provides a set of functions for maths stuff.
void pickRandomIndex(size_t p_size, size_t p_pick, std::vector< size_t > &p_ind)
Select random (unique) indices from the 0..p_size sequence.
bool geneticSingleModel(const TModelFit &p_state, size_t p_kernelSize, const typename TModelFit::Real &p_fitnessThreshold, size_t p_populationSize, size_t p_maxIteration, typename TModelFit::Model &p_bestModel, std::vector< size_t > &p_inliers)
Run a generic programming version of ransac searching for a single model.
static bool compare(const TSpecies *p_a, const TSpecies *p_b)
Definition: model_search.h:91



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019