Visual Servoing Platform  version 3.4.0
vpHomography.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Homography transformation.
33  *
34  * Authors:
35  * Muriel Pressigout
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
47 #ifndef vpHomography_hh
48 #define vpHomography_hh
49 
50 #include <list>
51 #include <vector>
52 
53 #include <visp3/core/vpCameraParameters.h>
54 #include <visp3/core/vpHomogeneousMatrix.h>
55 #include <visp3/core/vpImagePoint.h>
56 #include <visp3/core/vpMatrix.h>
57 #include <visp3/core/vpPlane.h>
58 #include <visp3/core/vpPoint.h>
59 
174 class VISP_EXPORT vpHomography : public vpArray2D<double>
175 {
176 private:
177  static const double sing_threshold; // = 0.0001;
178  static const double threshold_rotation;
179  static const double threshold_displacement;
181  // bool isplanar;
183  vpPlane bP;
184 
185 private:
187  void build();
188 
190  void insert(const vpHomogeneousMatrix &aRb);
192  void insert(const vpRotationMatrix &aRb);
194  void insert(const vpThetaUVector &tu);
196  void insert(const vpTranslationVector &atb);
198  void insert(const vpPlane &bP);
199 
200  static void initRansac(unsigned int n, double *xb, double *yb, double *xa, double *ya, vpColVector &x);
201 
202 public:
203  vpHomography();
204  vpHomography(const vpHomography &H);
206  vpHomography(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
208  vpHomography(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
210  vpHomography(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
212  vpHomography(const vpPoseVector &arb, const vpPlane &bP);
213  virtual ~vpHomography(){};
214 
216  void buildFrom(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP);
218  void buildFrom(const vpThetaUVector &tu, const vpTranslationVector &atb, const vpPlane &bP);
220  void buildFrom(const vpPoseVector &arb, const vpPlane &bP);
222  void buildFrom(const vpHomogeneousMatrix &aMb, const vpPlane &bP);
223 
224  vpHomography collineation2homography(const vpCameraParameters &cam) const;
225 
226  vpMatrix convert() const;
227 
228  void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
229 
230  void computeDisplacement(const vpColVector &nd, vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
231 
232  double det() const;
233  void eye();
234 
235  vpHomography homography2collineation(const vpCameraParameters &cam) const;
236 
238  vpHomography inverse(double sv_threshold = 1e-16, unsigned int *rank=NULL) const;
240  void inverse(vpHomography &Hi) const;
241 
243  void load(std::ifstream &f);
244 
245  // Multiplication by an homography
246  vpHomography operator*(const vpHomography &H) const;
247  // Multiplication by a scalar
248  vpHomography operator*(const double &v) const;
249  vpColVector operator*(const vpColVector &b) const;
250  // Multiplication by a point
251  vpPoint operator*(const vpPoint &H) const;
252 
253  // Division by a scalar
254  vpHomography operator/(const double &v) const;
255  vpHomography &operator/=(double v);
257  vpHomography &operator=(const vpMatrix &H);
258 
259  vpImagePoint projection(const vpImagePoint &p);
260 
266  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
267  {
268  (void)nrows;
269  (void)ncols;
270  (void)flagNullify;
271  throw(vpException(vpException::fatalError, "Cannot resize an homography matrix"));
272  };
273 
274  void save(std::ofstream &f) const;
275 
276  static void DLT(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
277  const std::vector<double> &ya, vpHomography &aHb, bool normalization = true);
278 
279  static void HLM(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
280  const std::vector<double> &ya, bool isplanar, vpHomography &aHb);
281 
282  static bool ransac(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
283  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
284  unsigned int nbInliersConsensus, double threshold, bool normalization = true);
285 
286  static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa);
287  static vpPoint project(const vpHomography &bHa, const vpPoint &Pa);
288 
289  static void robust(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
290  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inlier, double &residual,
291  double weights_threshold = 0.4, unsigned int niter = 4, bool normalization = true);
292 
293 #ifndef DOXYGEN_SHOULD_SKIP_THIS
294  static void build(vpHomography &aHb, const vpHomogeneousMatrix &aMb, const vpPlane &bP);
296 
297  static void computeDisplacement(const vpHomography &aHb, const vpColVector &nd, vpRotationMatrix &aRb,
299 
300  static void computeDisplacement(const vpHomography &aHb, vpRotationMatrix &aRb, vpTranslationVector &atb,
301  vpColVector &n);
302 
303  static void computeDisplacement(const vpHomography &H, double x, double y,
304  std::list<vpRotationMatrix> &vR, std::list<vpTranslationVector> &vT,
305  std::list<vpColVector> &vN);
306  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane &oN,
307  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
308  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane *oN,
309  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
310  static double computeResidual(vpColVector &x, vpColVector &M, vpColVector &d);
311  // VVS
312  static double computeRotation(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpHomogeneousMatrix &c2Mc1,
313  int userobust);
314  static void computeTransformation(vpColVector &x, unsigned int *ind, vpColVector &M);
315  static bool degenerateConfiguration(vpColVector &x, unsigned int *ind);
316  static bool degenerateConfiguration(vpColVector &x, unsigned int *ind, double threshold_area);
317  static bool degenerateConfiguration(const std::vector<double> &xb, const std::vector<double> &yb,
318  const std::vector<double> &xa, const std::vector<double> &ya);
319  static void HartleyNormalization(unsigned int n, const double *x, const double *y, double *xn, double *yn, double &xg,
320  double &yg, double &coef);
321  static void HartleyNormalization(const std::vector<double> &x, const std::vector<double> &y, std::vector<double> &xn,
322  std::vector<double> &yn, double &xg, double &yg, double &coef);
323  static void HartleyDenormalization(vpHomography &aHbn, vpHomography &aHb, double xg1, double yg1, double coef1,
324  double xg2, double yg2, double coef2);
325 
326 #endif // DOXYGEN_SHOULD_SKIP_THIS
327 
328 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
329 
333  void setIdentity();
335 #endif
336 };
337 
338 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:413
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
Definition: vpArray2D.h:737
error that can be emited by ViSP classes.
Definition: vpException.h:71
Implementation of a generic 2D array used as base class for matrices and vectors. ...
Definition: vpArray2D.h:131
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
Implementation of a rotation matrix and operations on such kind of matrices.
vpColVector operator*(const double &x, const vpColVector &v)
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
virtual ~vpHomography()
Definition: vpHomography.h:213
Generic class defining intrinsic camera parameters.
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true)
Definition: vpHomography.h:266
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:151
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:58
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=NULL)
Definition: vpArray2D.h:540