Visual Servoing Platform  version 3.6.1 under development (2024-05-26)
vpHomography.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Homography transformation.
32  */
33 
41 #ifndef vpHomography_hh
42 #define vpHomography_hh
43 
44 #include <list>
45 #include <vector>
46 
47 #include <visp3/core/vpCameraParameters.h>
48 #include <visp3/core/vpHomogeneousMatrix.h>
49 #include <visp3/core/vpImagePoint.h>
50 #include <visp3/core/vpMatrix.h>
51 #include <visp3/core/vpPlane.h>
52 #include <visp3/core/vpPoint.h>
53 
167 class VISP_EXPORT vpHomography : public vpArray2D<double>
168 {
169 public:
173  vpHomography();
177  vpHomography(const vpHomography &H);
179  vpHomography(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
181  vpHomography(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
183  vpHomography(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
185  vpHomography(const vpPoseVector &arb, const vpPlane &bP);
186 
187 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
189  void buildFrom(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
191  void buildFrom(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
193  void buildFrom(const vpPoseVector &arb, const vpPlane &bP);
195  void buildFrom(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
196 #endif
198  vpHomography &build(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
200  vpHomography &build(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
202  vpHomography &build(const vpPoseVector &arb, const vpPlane &bP);
204  vpHomography &build(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
205 
224  vpHomography collineation2homography(const vpCameraParameters &cam) const;
225 
230  vpMatrix convert() const;
231 
240  void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
241 
257  void computeDisplacement(const vpColVector &nd, vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
258 
262  double det() const;
263 
268  void eye();
269 
288  vpHomography homography2collineation(const vpCameraParameters &cam) const;
289 
301  vpHomography inverse(double sv_threshold = 1e-16, unsigned int *rank = nullptr) const;
302 
308  void inverse(vpHomography &bHa) const;
309 
318  void load(std::ifstream &f);
319 
331  vpHomography operator*(const vpHomography &H) const;
332 
345  vpHomography operator*(const double &v) const;
346 
352  vpColVector operator*(const vpColVector &b) const;
353 
363  vpPoint operator*(const vpPoint &b_P) const;
364 
376  vpHomography operator/(const double &v) const;
377 
381  vpHomography &operator/=(double v);
382 
390 
397  vpHomography &operator=(const vpMatrix &H);
398 
406  vpImagePoint projection(const vpImagePoint &ipb);
407 
413  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
414  {
415  (void)nrows;
416  (void)ncols;
417  (void)flagNullify;
418  throw(vpException(vpException::fatalError, "Cannot resize an homography matrix"));
419  };
420 
428  void save(std::ofstream &f) const;
429 
497  static void DLT(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
498  const std::vector<double> &ya, vpHomography &aHb, bool normalization = true);
499 
524  static void HLM(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
525  const std::vector<double> &ya, bool isplanar, vpHomography &aHb);
526 
554  static bool ransac(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
555  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
556  unsigned int nbInliersConsensus, double threshold, bool normalization = true);
557 
570  static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa);
571 
584  static vpPoint project(const vpHomography &bHa, const vpPoint &Pa);
585 
619  static void robust(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
620  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
621  double weights_threshold = 0.4, unsigned int niter = 4, bool normalization = true);
622 
623 #ifndef DOXYGEN_SHOULD_SKIP_THIS
624  static void build(vpHomography &aHb, const vpHomogeneousMatrix &aMb, const vpPlane &bP);
625 
626  static void computeDisplacement(const vpHomography &aHb, const vpColVector &nd, vpRotationMatrix &aRb,
628 
629  static void computeDisplacement(const vpHomography &aHb, vpRotationMatrix &aRb, vpTranslationVector &atb,
630  vpColVector &n);
631 
632  static void computeDisplacement(const vpHomography &H, double x, double y, std::list<vpRotationMatrix> &vR,
633  std::list<vpTranslationVector> &vT, std::list<vpColVector> &vN);
634  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane &oN,
635  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
636  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane *oN,
637  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
638  static double computeResidual(vpColVector &x, vpColVector &M, vpColVector &d);
639  // VVS
640  static double computeRotation(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpHomogeneousMatrix &c2Mc1,
641  int userobust);
642  static void computeTransformation(vpColVector &x, unsigned int *ind, vpColVector &M);
643  static bool degenerateConfiguration(const vpColVector &x, unsigned int *ind);
644  static bool degenerateConfiguration(const vpColVector &x, unsigned int *ind, double threshold_area);
645  static bool degenerateConfiguration(const std::vector<double> &xb, const std::vector<double> &yb,
646  const std::vector<double> &xa, const std::vector<double> &ya);
647  static void hartleyNormalization(unsigned int n, const double *x, const double *y, double *xn, double *yn, double &xg,
648  double &yg, double &coef);
649  static void hartleyNormalization(const std::vector<double> &x, const std::vector<double> &y, std::vector<double> &xn,
650  std::vector<double> &yn, double &xg, double &yg, double &coef);
651  static void hartleyDenormalization(vpHomography &aHbn, vpHomography &aHb, double xg1, double yg1, double coef1,
652  double xg2, double yg2, double coef2);
653 
654 #endif // DOXYGEN_SHOULD_SKIP_THIS
655 
656 private:
657  static const double m_sing_threshold; /* equals 0.0001 */
658  static const double m_threshold_rotation;
659  static const double m_threshold_displacement;
660  vpHomogeneousMatrix m_aMb;
661 
663  vpPlane m_bP;
664 
666  void build();
667 
672  void insert(const vpHomogeneousMatrix &aRb);
673 
678  void insert(const vpRotationMatrix &aRb);
679 
684  void insert(const vpThetaUVector &tu);
685 
690  void insert(const vpTranslationVector &atb);
691 
696  void insert(const vpPlane &bP);
697 
698  static void initRansac(unsigned int n, double *xb, double *yb, double *xa, double *ya, vpColVector &x);
699 };
700 
701 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:126
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:486
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=nullptr)
Definition: vpArray2D.h:658
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:515
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
Definition: vpArray2D.h:860
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:71
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:168
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true)
Definition: vpHomography.h:413
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:54
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:190
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
vpMatrix operator*(const double &x, const vpMatrix &A)