Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpHomography.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 VP_HOMOGRAPHY_H
42 #define VP_HOMOGRAPHY_H
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 
55 
173 class VISP_EXPORT vpHomography : public vpArray2D<double>
174 {
175 public:
179  vpHomography();
183  vpHomography(const vpHomography &H);
185  vpHomography(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
187  vpHomography(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
189  vpHomography(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
191  vpHomography(const vpPoseVector &arb, const vpPlane &bP);
192 
193 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
195  void buildFrom(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
197  void buildFrom(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
199  void buildFrom(const vpPoseVector &arb, const vpPlane &bP);
201  void buildFrom(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
202 #endif
204  vpHomography &build(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
206  vpHomography &build(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
208  vpHomography &build(const vpPoseVector &arb, const vpPlane &bP);
210  vpHomography &build(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
211 
230  vpHomography collineation2homography(const vpCameraParameters &cam) const;
231 
236  vpMatrix convert() const;
237 
246  void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
247 
263  void computeDisplacement(const vpColVector &nd, vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
264 
268  double det() const;
269 
274  void eye();
275 
294  vpHomography homography2collineation(const vpCameraParameters &cam) const;
295 
307  vpHomography inverse(double sv_threshold = 1e-16, unsigned int *rank = nullptr) const;
308 
314  void inverse(vpHomography &bHa) const;
315 
324  void load(std::ifstream &f);
325 
337  vpHomography operator*(const vpHomography &H) const;
338 
351  vpHomography operator*(const double &v) const;
352 
358  vpColVector operator*(const vpColVector &b) const;
359 
369  vpPoint operator*(const vpPoint &b_P) const;
370 
382  vpHomography operator/(const double &v) const;
383 
387  vpHomography &operator/=(double v);
388 
396 
403  vpHomography &operator=(const vpMatrix &H);
404 
412  vpImagePoint projection(const vpImagePoint &ipb);
413 
419  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
420  {
421  (void)nrows;
422  (void)ncols;
423  (void)flagNullify;
424  throw(vpException(vpException::fatalError, "Cannot resize an homography matrix"));
425  };
426 
434  void save(std::ofstream &f) const;
435 
503  static void DLT(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
504  const std::vector<double> &ya, vpHomography &aHb, bool normalization = true);
505 
530  static void HLM(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
531  const std::vector<double> &ya, bool isplanar, vpHomography &aHb);
532 
560  static bool ransac(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
561  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
562  unsigned int nbInliersConsensus, double threshold, bool normalization = true);
563 
576  static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa);
577 
590  static vpPoint project(const vpHomography &bHa, const vpPoint &Pa);
591 
625  static void robust(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
626  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
627  double weights_threshold = 0.4, unsigned int niter = 4, bool normalization = true);
628 
629 #ifndef DOXYGEN_SHOULD_SKIP_THIS
630  static void build(vpHomography &aHb, const vpHomogeneousMatrix &aMb, const vpPlane &bP);
631 
632  static void computeDisplacement(const vpHomography &aHb, const vpColVector &nd, vpRotationMatrix &aRb,
634 
635  static void computeDisplacement(const vpHomography &aHb, vpRotationMatrix &aRb, vpTranslationVector &atb,
636  vpColVector &n);
637 
638  static void computeDisplacement(const vpHomography &H, double x, double y, std::list<vpRotationMatrix> &vR,
639  std::list<vpTranslationVector> &vT, std::list<vpColVector> &vN);
640  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane &oN,
641  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
642  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane *oN,
643  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
644  static double computeResidual(vpColVector &x, vpColVector &M, vpColVector &d);
645  // VVS
646  static double computeRotation(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpHomogeneousMatrix &c2Mc1,
647  int userobust);
648  static void computeTransformation(vpColVector &x, unsigned int *ind, vpColVector &M);
649  static bool degenerateConfiguration(const vpColVector &x, unsigned int *ind);
650  static bool degenerateConfiguration(const vpColVector &x, unsigned int *ind, double threshold_area);
651  static bool degenerateConfiguration(const std::vector<double> &xb, const std::vector<double> &yb,
652  const std::vector<double> &xa, const std::vector<double> &ya);
653  static void hartleyNormalization(unsigned int n, const double *x, const double *y, double *xn, double *yn, double &xg,
654  double &yg, double &coef);
655  static void hartleyNormalization(const std::vector<double> &x, const std::vector<double> &y, std::vector<double> &xn,
656  std::vector<double> &yn, double &xg, double &yg, double &coef);
657  static void hartleyDenormalization(vpHomography &aHbn, vpHomography &aHb, double xg1, double yg1, double coef1,
658  double xg2, double yg2, double coef2);
659 
660 #endif // DOXYGEN_SHOULD_SKIP_THIS
661 
662 private:
663  static const double m_sing_threshold; /* equals 0.0001 */
664  static const double m_threshold_rotation;
665  static const double m_threshold_displacement;
666  vpHomogeneousMatrix m_aMb;
667 
669  vpPlane m_bP;
670 
672  void build();
673 
678  void insert(const vpHomogeneousMatrix &aRb);
679 
684  void insert(const vpRotationMatrix &aRb);
685 
690  void insert(const vpThetaUVector &tu);
691 
696  void insert(const vpTranslationVector &atb);
697 
702  void insert(const vpPlane &bP);
703 
704  static void initRansac(unsigned int n, double *xb, double *yb, double *xa, double *ya, vpColVector &x);
705 };
706 
707 END_VISP_NAMESPACE
708 
709 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:145
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:494
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=nullptr)
Definition: vpArray2D.h:666
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:523
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
Definition: vpArray2D.h:871
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ fatalError
Fatal error.
Definition: vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true)
Definition: vpHomography.h:419
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:169
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:57
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:203
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)