AliceVision
Photogrammetric Computer Vision Framework
EssentialKernel.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2016 AliceVision contributors.
3 // Copyright (c) 2012 openMVG contributors.
4 // Copyright (c) 2010 libmv contributors.
5 // This Source Code Form is subject to the terms of the Mozilla Public License,
6 // v. 2.0. If a copy of the MPL was not distributed with this file,
7 // You can obtain one at https://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <aliceVision/robustEstimation/conditioning.hpp>
12 #include <aliceVision/robustEstimation/PointFittingKernel.hpp>
13 #include <aliceVision/multiview/relativePose/Essential5PSolver.hpp>
14 #include <aliceVision/multiview/relativePose/Essential8PSolver.hpp>
15 #include <aliceVision/multiview/relativePose/FundamentalError.hpp>
16 #include <aliceVision/multiview/essential.hpp>
17 #include <aliceVision/multiview/Unnormalizer.hpp>
18 
19 namespace aliceVision {
20 namespace multiview {
21 namespace relativePose {
22 
29 template<typename SolverT, typename ErrorT, typename ModelT = robustEstimation::Mat3Model>
30 class EssentialKernel : public robustEstimation::PointFittingKernel<SolverT, ErrorT, ModelT>
31 {
32  public:
34 
35  EssentialKernel(const Mat& x1, const Mat& x2, const Mat3& K1, const Mat3& K2)
37  _K1(K1),
38  _K2(K2)
39  {}
40 
41  void fit(const std::vector<std::size_t>& samples, std::vector<ModelT>& models) const override
42  {
43  const Mat x1 = buildSubsetMatrix(KernelBase::_x1, samples);
44  const Mat x2 = buildSubsetMatrix(KernelBase::_x2, samples);
45 
46  assert(2 == x1.rows());
48  assert(x1.rows() == x2.rows());
49  assert(x1.cols() == x2.cols());
50 
51  // normalize the data (image coords to camera coords).
52  const Mat3 K1Inverse = _K1.inverse();
53  const Mat3 K2Inverse = _K2.inverse();
54 
55  Mat x1_normalized;
56  Mat x2_normalized;
57 
58  robustEstimation::applyTransformationToPoints(x1, K1Inverse, &x1_normalized);
59  robustEstimation::applyTransformationToPoints(x2, K2Inverse, &x2_normalized);
60 
61  KernelBase::_kernelSolver.solve(x1_normalized, x2_normalized, models);
62  }
63 
64  double error(std::size_t sample, const ModelT& model) const
65  {
66  Mat3 F;
67  fundamentalFromEssential(model.getMatrix(), _K1, _K2, &F);
69  return KernelBase::_errorEstimator.error(modelF, KernelBase::_x1.col(sample), KernelBase::_x2.col(sample));
70  }
71 
72  protected:
73  // The two camera calibrated camera matrix
74  Mat3 _K1, _K2;
75 };
76 
80 typedef EssentialKernel<Essential8PSolver, FundamentalSampsonError, robustEstimation::Mat3Model> Essential8PKernel;
81 
85 typedef EssentialKernel<Essential5PSolver, FundamentalSampsonError, robustEstimation::Mat3Model> Essential5PKernel;
86 
87 } // namespace relativePose
88 } // namespace multiview
89 } // 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::multiview::relativePose::EssentialKernel::fit
void fit(const std::vector< std::size_t > &samples, std::vector< ModelT > &models) const override
Extract required sample and fit model(s) to the sample.
Definition: EssentialKernel.hpp:41
aliceVision::multiview::relativePose::EssentialKernel::error
double error(std::size_t sample, const ModelT &model) const
Return the error associated to the model and a sample point.
Definition: EssentialKernel.hpp:64
aliceVision::multiview::relativePose::EssentialKernel
Generic Solver for the 5pt Essential Matrix Estimation.
Definition: EssentialKernel.hpp:30
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::PointFittingKernel::_kernelSolver
const SolverT _kernelSolver
two view solver
Definition: PointFittingKernel.hpp:119
aliceVision::robustEstimation::PointFittingKernel< SolverT, ErrorT, robustEstimation::Mat3Model >::getMinimumNbRequiredSamples
std::size_t getMinimumNbRequiredSamples() const
Return the minimum number of required samples.
Definition: PointFittingKernel.hpp:64
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::robustEstimation::MatrixModel
Matrix based model to be used in a solver.
Definition: ISolver.hpp:88
aliceVision::robustEstimation::PointFittingKernel::_x2
const Mat & _x2
right corresponding data
Definition: PointFittingKernel.hpp:117
aliceVision::robustEstimation::PointFittingKernel::_errorEstimator
const ErrorT _errorEstimator
solver error estimation
Definition: PointFittingKernel.hpp:121
aliceVision::robustEstimation::PointFittingKernel::_x1
const Mat & _x1
left corresponding data
Definition: PointFittingKernel.hpp:115
aliceVision::fundamentalFromEssential
void fundamentalFromEssential(const Mat3 &E, const Mat3 &K1, const Mat3 &K2, Mat3 *F)
Given E, Left/Right K matrix it compute the Fundamental matrix.
Definition: essential.cpp:21