AliceVision
Photogrammetric Computer Vision Framework
PointFittingRansacKernel.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2019 AliceVision contributors.
3 // This Source Code Form is subject to the terms of the Mozilla Public License,
4 // v. 2.0. If a copy of the MPL was not distributed with this file,
5 // You can obtain one at https://mozilla.org/MPL/2.0/.
6 
7 #pragma once
8 
9 #include <aliceVision/config.hpp>
10 #include <aliceVision/numeric/numeric.hpp>
11 #include <aliceVision/robustEstimation/conditioning.hpp>
12 #include <aliceVision/robustEstimation/ISolver.hpp>
13 #include <aliceVision/robustEstimation/IRansacKernel.hpp>
14 #include <aliceVision/robustEstimation/PointFittingKernel.hpp>
15 
16 namespace aliceVision {
17 namespace robustEstimation {
18 
31 template<typename SolverT_, typename ErrorT_, typename ModelT_, typename SolverLsT_ = robustEstimation::UndefinedSolver<ModelT_>>
33  public robustEstimation::PointFittingKernel<SolverT_, ErrorT_, ModelT_>
34 {
35  public:
37 
38  PointFittingRansacKernel(const Mat& x1, const Mat& x2)
39  : PFKernel(x1, x2)
40  {}
41 
46  std::size_t getMinimumNbRequiredSamples() const override { return PFKernel::getMinimumNbRequiredSamples(); }
47 
48  std::size_t getMinimumNbRequiredSamplesLS() const override { return _solverLs.getMinimumNbRequiredSamples(); }
49 
54  std::size_t getMaximumNbModels() const override { return PFKernel::getMaximumNbModels(); }
55 
63  void fit(const std::vector<std::size_t>& samples, std::vector<ModelT_>& models) const override { PFKernel::fit(samples, models); }
64 
65  void fitLS(const std::vector<std::size_t>& inliers, std::vector<ModelT_>& models, const std::vector<double>* weights = nullptr) const override
66  {
67  const Mat x1 = buildSubsetMatrix(PFKernel::_x1, inliers);
68  const Mat x2 = buildSubsetMatrix(PFKernel::_x2, inliers);
69 
70  if (weights == nullptr)
71  _solverLs.solve(x1, x2, models);
72  else
73  _solverLs.solve(x1, x2, models, *weights);
74  }
75 
76  void computeWeights(const ModelT_& model,
77  const std::vector<std::size_t>& inliers,
78  std::vector<double>& weights,
79  const double eps = 0.001) const override
80  {
81  const auto numInliers = inliers.size();
82  weights.resize(numInliers);
83  for (std::size_t sample = 0; sample < numInliers; ++sample)
84  {
85  const auto idx = inliers[sample];
86  weights[sample] = PFKernel::_errorEstimator.error(model, PFKernel::_x1.col(idx), PFKernel::_x2.col(idx));
87  // avoid division by zero
88  weights[sample] = 1.0 / std::pow(std::max(eps, weights[sample]), 2);
89  }
90  }
91 
98  double error(std::size_t sample, const ModelT_& model) const override { return PFKernel::error(sample, model); }
99 
105  void errors(const ModelT_& model, std::vector<double>& errors) const override { PFKernel::errors(model, errors); }
106 
111  virtual void unnormalize(ModelT_& model) const = 0;
112 
117  std::size_t nbSamples() const override { return PFKernel::nbSamples(); }
118 
123  virtual double logalpha0() const = 0;
124 
125  virtual double errorVectorDimension() const = 0;
126  virtual double unormalizeError(double val) const = 0;
127  virtual Mat3 normalizer1() const = 0;
128  virtual double thresholdNormalizer() const = 0;
129 
130  private:
131  const SolverLsT_ _solverLs{};
132 };
133 
134 } // namespace robustEstimation
135 } // namespace aliceVision
aliceVision::robustEstimation::PointFittingKernel
This is one example (targeted at solvers that operate on correspondences between two views) that show...
Definition: PointFittingKernel.hpp:48
aliceVision::robustEstimation::PointFittingRansacKernel::unnormalize
virtual void unnormalize(ModelT_ &model) const =0
Function used to unnormalize the model.
aliceVision::robustEstimation::PointFittingRansacKernel
A virtual kernel used for the ACRANSAC / LORANSAC framework.
Definition: PointFittingRansacKernel.hpp:32
aliceVision::robustEstimation::PointFittingKernel::errors
virtual void errors(const ModelT &model, std::vector< double > &errors) const
Return the errors associated to the model and each sample point.
Definition: PointFittingKernel.hpp:100
aliceVision::robustEstimation::PointFittingRansacKernel::getMinimumNbRequiredSamples
std::size_t getMinimumNbRequiredSamples() const override
Return the minimum number of required samples for the solver.
Definition: PointFittingRansacKernel.hpp:46
aliceVision::buildSubsetMatrix
TMat buildSubsetMatrix(const TMat &A, const TCols &columns)
It extracts the columns of given indices from the given matrix.
Definition: Container.hpp:124
aliceVision::robustEstimation::PointFittingRansacKernel::getMaximumNbModels
std::size_t getMaximumNbModels() const override
Return the maximum number of models for the solver.
Definition: PointFittingRansacKernel.hpp:54
aliceVision::robustEstimation::PointFittingKernel::getMinimumNbRequiredSamples
std::size_t getMinimumNbRequiredSamples() const
Return the minimum number of required samples.
Definition: PointFittingKernel.hpp:64
aliceVision::robustEstimation::PointFittingRansacKernel::computeWeights
void computeWeights(const ModelT_ &model, const std::vector< std::size_t > &inliers, std::vector< double > &weights, const double eps=0.001) const override
Function used to estimate the weights, typically used by the least square algorithm.
Definition: PointFittingRansacKernel.hpp:76
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::robustEstimation::PointFittingRansacKernel::logalpha0
virtual double logalpha0() const =0
Get logalpha0, Alpha0 is used to make the error adaptive to the image size.
aliceVision::robustEstimation::PointFittingKernel::getMaximumNbModels
std::size_t getMaximumNbModels() const
Return the maximum number of models.
Definition: PointFittingKernel.hpp:70
aliceVision::robustEstimation::PointFittingKernel::nbSamples
std::size_t nbSamples() const
get the number of putative points
Definition: PointFittingKernel.hpp:111
aliceVision::robustEstimation::PointFittingKernel::_x2
const Mat & _x2
right corresponding data
Definition: PointFittingKernel.hpp:117
aliceVision::robustEstimation::PointFittingRansacKernel::fitLS
void fitLS(const std::vector< std::size_t > &inliers, std::vector< ModelT_ > &models, const std::vector< double > *weights=nullptr) const override
This function is called to estimate the model using a least squared algorithm from a minimum of minSa...
Definition: PointFittingRansacKernel.hpp:65
aliceVision::robustEstimation::PointFittingRansacKernel::nbSamples
std::size_t nbSamples() const override
The number of elements in the data.
Definition: PointFittingRansacKernel.hpp:117
aliceVision::robustEstimation::PointFittingKernel::_errorEstimator
const ErrorT _errorEstimator
solver error estimation
Definition: PointFittingKernel.hpp:121
aliceVision::robustEstimation::PointFittingKernel::error
virtual double error(std::size_t sample, const ModelT &model) const
Return the error associated to the model and a sample point.
Definition: PointFittingKernel.hpp:90
aliceVision::robustEstimation::PointFittingKernel::_x1
const Mat & _x1
left corresponding data
Definition: PointFittingKernel.hpp:115
aliceVision::robustEstimation::PointFittingRansacKernel::errors
void errors(const ModelT_ &model, std::vector< double > &errors) const override
Function that computes the estimation error for a given model and all the elements.
Definition: PointFittingRansacKernel.hpp:105
aliceVision::robustEstimation::PointFittingRansacKernel::error
double error(std::size_t sample, const ModelT_ &model) const override
Function that computes the estimation error for a given model and a given element.
Definition: PointFittingRansacKernel.hpp:98
aliceVision::robustEstimation::PointFittingRansacKernel::fit
void fit(const std::vector< std::size_t > &samples, std::vector< ModelT_ > &models) const override
This function is called to estimate the model from the minimum number of sample minSample (i....
Definition: PointFittingRansacKernel.hpp:63
aliceVision::robustEstimation::IRansacKernel
A generic kernel used for the ACRANSAC / LORANSAC framework.
Definition: IRansacKernel.hpp:24
aliceVision::robustEstimation::PointFittingKernel::fit
virtual void fit(const std::vector< std::size_t > &samples, std::vector< ModelT > &models) const
Extract required sample and fit model(s) to the sample.
Definition: PointFittingKernel.hpp:77