Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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  vpMatrix convert() const;
224 
225  void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
226 
227  void computeDisplacement(const vpColVector &nd, vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n);
228 
229  void eye();
230 
232  vpHomography inverse() const;
234  void inverse(vpHomography &Hi) const;
235 
237  void load(std::ifstream &f);
238 
239  // Multiplication by an homography
240  vpHomography operator*(const vpHomography &H) const;
241  // Multiplication by a scalar
242  vpHomography operator*(const double &v) const;
243  vpColVector operator*(const vpColVector &b) const;
244  // Multiplication by a point
245  vpPoint operator*(const vpPoint &H) const;
246 
247  // Division by a scalar
248  vpHomography operator/(const double &v) const;
249  vpHomography &operator/=(double v);
251  vpHomography &operator=(const vpMatrix &H);
252 
253  vpImagePoint projection(const vpImagePoint &p);
254 
260  void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify = true)
261  {
262  (void)nrows;
263  (void)ncols;
264  (void)flagNullify;
265  throw(vpException(vpException::fatalError, "Cannot resize an homography matrix"));
266  };
267 
268  void save(std::ofstream &f) const;
269 
270  static void DLT(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
271  const std::vector<double> &ya, vpHomography &aHb, bool normalization = true);
272 
273  static void HLM(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
274  const std::vector<double> &ya, bool isplanar, vpHomography &aHb);
275 
276  static bool ransac(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
277  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inliers, double &residual,
278  unsigned int nbInliersConsensus, double threshold, bool normalization = true);
279 
280  static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa);
281  static vpPoint project(const vpHomography &bHa, const vpPoint &Pa);
282 
283  static void robust(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
284  const std::vector<double> &ya, vpHomography &aHb, std::vector<bool> &inlier, double &residual,
285  double weights_threshold = 0.4, unsigned int niter = 4, bool normalization = true);
286 
287 #ifndef DOXYGEN_SHOULD_SKIP_THIS
288  static void build(vpHomography &aHb, const vpHomogeneousMatrix &aMb, const vpPlane &bP);
290 
291  static void computeDisplacement(const vpHomography &aHb, const vpColVector &nd, vpRotationMatrix &aRb,
293 
294  static void computeDisplacement(const vpHomography &aHb, vpRotationMatrix &aRb, vpTranslationVector &atb,
295  vpColVector &n);
296 
297  static void computeDisplacement(const vpHomography &H, const double x, const double y,
298  std::list<vpRotationMatrix> &vR, std::list<vpTranslationVector> &vT,
299  std::list<vpColVector> &vN);
300  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane &oN,
301  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
302  static double computeDisplacement(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpPlane *oN,
303  vpHomogeneousMatrix &c2Mc1, vpHomogeneousMatrix &c1Mo, int userobust);
304  static double computeResidual(vpColVector &x, vpColVector &M, vpColVector &d);
305  // VVS
306  static double computeRotation(unsigned int nbpoint, vpPoint *c1P, vpPoint *c2P, vpHomogeneousMatrix &c2Mc1,
307  int userobust);
308  static void computeTransformation(vpColVector &x, unsigned int *ind, vpColVector &M);
309  static bool degenerateConfiguration(vpColVector &x, unsigned int *ind);
310  static bool degenerateConfiguration(vpColVector &x, unsigned int *ind, double threshold_area);
311  static bool degenerateConfiguration(const std::vector<double> &xb, const std::vector<double> &yb,
312  const std::vector<double> &xa, const std::vector<double> &ya);
313  static void HartleyNormalization(unsigned int n, const double *x, const double *y, double *xn, double *yn, double &xg,
314  double &yg, double &coef);
315  static void HartleyNormalization(const std::vector<double> &x, const std::vector<double> &y, std::vector<double> &xn,
316  std::vector<double> &yn, double &xg, double &yg, double &coef);
317  static void HartleyDenormalization(vpHomography &aHbn, vpHomography &aHb, double xg1, double yg1, double coef1,
318  double xg2, double yg2, double coef2);
319 
320 #endif // DOXYGEN_SHOULD_SKIP_THIS
321 
322 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
323 
327  void setIdentity();
329 #endif
330 };
331 
332 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
static bool save(const std::string &filename, const vpArray2D< Type > &A, const bool binary=false, const char *header="")
Definition: vpArray2D.h:519
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:247
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:71
Implementation of a generic 2D array used as vase class of matrices and vectors.
Definition: vpArray2D.h:70
Class that defines what is a point.
Definition: vpPoint.h:58
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
static bool load(const std::string &filename, vpArray2D< Type > &A, const bool binary=false, char *header=NULL)
Definition: vpArray2D.h:322
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpHomography.h:260
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.