Visual Servoing Platform  version 3.6.1 under development (2024-10-15)
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 
54 BEGIN_VISP_NAMESPACE
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 
194  vpHomography &buildFrom(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
196  vpHomography &buildFrom(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
198  vpHomography &buildFrom(const vpPoseVector &arb, const vpPlane &bP);
200  vpHomography &buildFrom(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
201 
220  vpHomography collineation2homography(const vpCameraParameters &cam) const;
221 
226  vpMatrix convert() const;
227 
236  void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
237 
253  void computeDisplacement(const vpColVector &nd, vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
254 
258  double det() const;
259 
264  void eye();
265 
284  vpHomography homography2collineation(const vpCameraParameters &cam) const;
285 
297  vpHomography inverse(double sv_threshold = 1e-16, unsigned int *rank = nullptr) const;
298 
304  void inverse(vpHomography &bHa) const;
305 
314  void load(std::ifstream &f);
315 
327  vpHomography operator*(const vpHomography &H) const;
328 
341  vpHomography operator*(const double &v) const;
342 
348  vpColVector operator*(const vpColVector &b) const;
349 
359  vpPoint operator*(const vpPoint &b_P) const;
360 
372  vpHomography operator/(const double &v) const;
373 
377  vpHomography &operator/=(double v);
378 
386 
393  vpHomography &operator=(const vpMatrix &H);
394 
402  vpImagePoint projection(const vpImagePoint &ipb);
403 
409  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
410  {
411  (void)nrows;
412  (void)ncols;
413  (void)flagNullify;
414  throw(vpException(vpException::fatalError, "Cannot resize an homography matrix"));
415  };
416 
424  void save(std::ofstream &f) const;
425 
493  static void DLT(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
494  const std::vector<double> &ya, vpHomography &aHb, bool normalization = true);
495 
520  static void HLM(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
521  const std::vector<double> &ya, bool isplanar, vpHomography &aHb);
522 
550  static bool ransac(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
551  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
552  unsigned int nbInliersConsensus, double threshold, bool normalization = true);
553 
566  static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa);
567 
580  static vpPoint project(const vpHomography &bHa, const vpPoint &Pa);
581 
615  static void robust(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
616  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
617  double weights_threshold = 0.4, unsigned int niter = 4, bool normalization = true);
618 
619 #ifndef DOXYGEN_SHOULD_SKIP_THIS
620  static void build(vpHomography &aHb, const vpHomogeneousMatrix &aMb, const vpPlane &bP);
621 
622  static void computeDisplacement(const vpHomography &aHb, const vpColVector &nd, vpRotationMatrix &aRb,
624 
625  static void computeDisplacement(const vpHomography &aHb, vpRotationMatrix &aRb, vpTranslationVector &atb,
626  vpColVector &n);
627 
628  static void computeDisplacement(const vpHomography &H, double x, double y, std::list<vpRotationMatrix> &vR,
629  std::list<vpTranslationVector> &vT, std::list<vpColVector> &vN);
630  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane &oN,
631  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
632  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane *oN,
633  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
634  static double computeResidual(vpColVector &x, vpColVector &M, vpColVector &d);
635  // VVS
636  static double computeRotation(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpHomogeneousMatrix &c2Mc1,
637  int userobust);
638  static void computeTransformation(vpColVector &x, unsigned int *ind, vpColVector &M);
639  static bool degenerateConfiguration(const vpColVector &x, unsigned int *ind);
640  static bool degenerateConfiguration(const vpColVector &x, unsigned int *ind, double threshold_area);
641  static bool degenerateConfiguration(const std::vector<double> &xb, const std::vector<double> &yb,
642  const std::vector<double> &xa, const std::vector<double> &ya);
643  static void hartleyNormalization(unsigned int n, const double *x, const double *y, double *xn, double *yn, double &xg,
644  double &yg, double &coef);
645  static void hartleyNormalization(const std::vector<double> &x, const std::vector<double> &y, std::vector<double> &xn,
646  std::vector<double> &yn, double &xg, double &yg, double &coef);
647  static void hartleyDenormalization(vpHomography &aHbn, vpHomography &aHb, double xg1, double yg1, double coef1,
648  double xg2, double yg2, double coef2);
649 
650 #endif // DOXYGEN_SHOULD_SKIP_THIS
651 
652 private:
653  static const double m_sing_threshold; /* equals 0.0001 */
654  static const double m_threshold_rotation;
655  static const double m_threshold_displacement;
656  vpHomogeneousMatrix m_aMb;
657 
659  vpPlane m_bP;
660 
662  void build();
663 
668  void insert(const vpHomogeneousMatrix &aRb);
669 
674  void insert(const vpRotationMatrix &aRb);
675 
680  void insert(const vpThetaUVector &tu);
681 
686  void insert(const vpTranslationVector &atb);
687 
692  void insert(const vpPlane &bP);
693 
694  static void initRansac(unsigned int n, double *xb, double *yb, double *xa, double *ya, vpColVector &x);
695 };
696 
697 END_VISP_NAMESPACE
698 
699 #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:409
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)