Visual Servoing Platform  version 3.6.1 under development (2024-04-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 
188  void buildFrom(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
190  void buildFrom(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
192  void buildFrom(const vpPoseVector &arb, const vpPlane &bP);
194  void buildFrom(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
195 
214  vpHomography collineation2homography(const vpCameraParameters &cam) const;
215 
220  vpMatrix convert() const;
221 
230  void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
231 
247  void computeDisplacement(const vpColVector &nd, vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
248 
252  double det() const;
253 
258  void eye();
259 
278  vpHomography homography2collineation(const vpCameraParameters &cam) const;
279 
291  vpHomography inverse(double sv_threshold = 1e-16, unsigned int *rank = nullptr) const;
292 
298  void inverse(vpHomography &bHa) const;
299 
308  void load(std::ifstream &f);
309 
321  vpHomography operator*(const vpHomography &H) const;
322 
335  vpHomography operator*(const double &v) const;
336 
342  vpColVector operator*(const vpColVector &b) const;
343 
353  vpPoint operator*(const vpPoint &b_P) const;
354 
366  vpHomography operator/(const double &v) const;
367 
371  vpHomography &operator/=(double v);
372 
380 
387  vpHomography &operator=(const vpMatrix &H);
388 
396  vpImagePoint projection(const vpImagePoint &ipb);
397 
403  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
404  {
405  (void)nrows;
406  (void)ncols;
407  (void)flagNullify;
408  throw(vpException(vpException::fatalError, "Cannot resize an homography matrix"));
409  };
410 
418  void save(std::ofstream &f) const;
419 
487  static void DLT(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
488  const std::vector<double> &ya, vpHomography &aHb, bool normalization = true);
489 
514  static void HLM(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
515  const std::vector<double> &ya, bool isplanar, vpHomography &aHb);
516 
544  static bool ransac(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
545  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
546  unsigned int nbInliersConsensus, double threshold, bool normalization = true);
547 
560  static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa);
561 
574  static vpPoint project(const vpHomography &bHa, const vpPoint &Pa);
575 
609  static void robust(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
610  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
611  double weights_threshold = 0.4, unsigned int niter = 4, bool normalization = true);
612 
613 #ifndef DOXYGEN_SHOULD_SKIP_THIS
615  static void build(vpHomography &aHb, const vpHomogeneousMatrix &aMb, const vpPlane &bP);
616 
617  static void computeDisplacement(const vpHomography &aHb, const vpColVector &nd, vpRotationMatrix &aRb,
619 
620  static void computeDisplacement(const vpHomography &aHb, vpRotationMatrix &aRb, vpTranslationVector &atb,
621  vpColVector &n);
622 
623  static void computeDisplacement(const vpHomography &H, double x, double y, std::list<vpRotationMatrix> &vR,
624  std::list<vpTranslationVector> &vT, std::list<vpColVector> &vN);
625  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane &oN,
626  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
627  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane *oN,
628  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
629  static double computeResidual(vpColVector &x, vpColVector &M, vpColVector &d);
630  // VVS
631  static double computeRotation(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpHomogeneousMatrix &c2Mc1,
632  int userobust);
633  static void computeTransformation(vpColVector &x, unsigned int *ind, vpColVector &M);
634  static bool degenerateConfiguration(const vpColVector &x, unsigned int *ind);
635  static bool degenerateConfiguration(const vpColVector &x, unsigned int *ind, double threshold_area);
636  static bool degenerateConfiguration(const std::vector<double> &xb, const std::vector<double> &yb,
637  const std::vector<double> &xa, const std::vector<double> &ya);
638  static void hartleyNormalization(unsigned int n, const double *x, const double *y, double *xn, double *yn, double &xg,
639  double &yg, double &coef);
640  static void hartleyNormalization(const std::vector<double> &x, const std::vector<double> &y, std::vector<double> &xn,
641  std::vector<double> &yn, double &xg, double &yg, double &coef);
642  static void hartleyDenormalization(vpHomography &aHbn, vpHomography &aHb, double xg1, double yg1, double coef1,
643  double xg2, double yg2, double coef2);
644 
645 #endif // DOXYGEN_SHOULD_SKIP_THIS
646 
647 private:
648  static const double m_sing_threshold; // = 0.0001;
649  static const double m_threshold_rotation;
650  static const double m_threshold_displacement;
651  vpHomogeneousMatrix m_aMb;
652 
654  vpPlane m_bP;
655 
657  void build();
658 
663  void insert(const vpHomogeneousMatrix &aRb);
664 
669  void insert(const vpRotationMatrix &aRb);
670 
675  void insert(const vpThetaUVector &tu);
676 
681  void insert(const vpTranslationVector &atb);
682 
687  void insert(const vpPlane &bP);
688 
689  static void initRansac(unsigned int n, double *xb, double *yb, double *xa, double *ya, vpColVector &x);
690 };
691 
692 #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:484
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=nullptr)
Definition: vpArray2D.h:653
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:512
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
Definition: vpArray2D.h:855
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:84
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:403
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:189
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)