Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
homographyHLM2DObject.cpp
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  * Example of the HLM (Malis) homography estimation algorithm.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
55 #include <visp3/core/vpDebug.h>
56 #include <visp3/core/vpMath.h>
57 #include <visp3/core/vpRotationMatrix.h>
58 #include <visp3/core/vpThetaUVector.h>
59 #include <visp3/vision/vpHomography.h>
60 
61 #include <stdlib.h>
62 #include <visp3/core/vpDebug.h>
63 #include <visp3/core/vpHomogeneousMatrix.h>
64 #include <visp3/core/vpMath.h>
65 #include <visp3/core/vpPoint.h>
66 #include <visp3/io/vpParseArgv.h>
67 // List of allowed command line options
68 #define GETOPTARGS "h"
69 #define L 0.1
70 #define nbpt 5
71 
72 void usage(const char *name, const char *badparam);
73 bool getOptions(int argc, const char **argv);
74 
84 void usage(const char *name, const char *badparam)
85 {
86  fprintf(stdout, "\n\
87 Test the HLM (Malis) homography estimation algorithm with a planar object.\n\
88 \n\
89 SYNOPSIS\n\
90  %s [-h]\n", name);
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  -h\n\
95  Print the help.\n");
96 
97  if (badparam) {
98  fprintf(stderr, "ERROR: \n");
99  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
100  }
101 }
112 bool getOptions(int argc, const char **argv)
113 {
114  const char *optarg_;
115  int c;
116  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
117 
118  switch (c) {
119  case 'h':
120  usage(argv[0], NULL);
121  return false;
122  break;
123 
124  default:
125  usage(argv[0], optarg_);
126  return false;
127  break;
128  }
129  }
130 
131  if ((c == 1) || (c == -1)) {
132  // standalone param or error
133  usage(argv[0], NULL);
134  std::cerr << "ERROR: " << std::endl;
135  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136  return false;
137  }
138 
139  return true;
140 }
141 
142 int main(int argc, const char **argv)
143 {
144 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
145  try {
146  // Read the command line options
147  if (getOptions(argc, argv) == false) {
148  exit(-1);
149  }
150 
151  vpPoint P[nbpt]; // Point to be tracked
152  std::vector<double> xa(nbpt), ya(nbpt);
153  std::vector<double> xb(nbpt), yb(nbpt);
154 
155  vpPoint aP[nbpt]; // Point to be tracked
156  vpPoint bP[nbpt]; // Point to be tracked
157 
158  P[0].setWorldCoordinates(-L, -L, 0);
159  P[1].setWorldCoordinates(2 * L, -L, 0);
160  P[2].setWorldCoordinates(L, L, 0);
161  P[3].setWorldCoordinates(-L, 3 * L, 0);
162  P[4].setWorldCoordinates(0, 0, 0);
163  /*
164  P[5].setWorldCoordinates(10,20, 0 ) ;
165  P[6].setWorldCoordinates(-10,12, 0 ) ;
166  */
167  vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
168  vpHomogeneousMatrix aMb(1, 0, 0.0, vpMath::rad(10), 0, vpMath::rad(40));
169  vpHomogeneousMatrix aMo = aMb * bMo;
170  for (unsigned int i = 0; i < nbpt; i++) {
171  P[i].project(aMo);
172  aP[i] = P[i];
173  xa[i] = P[i].get_x();
174  ya[i] = P[i].get_y();
175  }
176 
177  for (unsigned int i = 0; i < nbpt; i++) {
178  P[i].project(bMo);
179  bP[i] = P[i];
180  xb[i] = P[i].get_x();
181  yb[i] = P[i].get_y();
182  }
183  std::cout << "-------------------------------" << std::endl;
184  std::cout << "aMb " << std::endl << aMb << std::endl;
185  std::cout << "-------------------------------" << std::endl;
186  vpHomography aHb;
187 
188  vpHomography::HLM(xb, yb, xa, ya, true, aHb);
189 
190  aHb /= aHb[2][2];
191  std::cout << "aHb computed using the Malis paralax algorithm: \n" << aHb << std::endl;
192 
193  vpRotationMatrix aRb;
195  vpColVector n;
196 
197  std::cout << "-------------------------------" << std::endl;
198  std::cout << "extract R, T and n " << std::endl;
199  aHb.computeDisplacement(aRb, aTb, n);
200  std::cout << "Rotation: aRb" << std::endl;
201  std::cout << aRb << std::endl;
202  std::cout << "Translation: aTb" << std::endl;
203  std::cout << (aTb).t() << std::endl;
204  std::cout << "Normal to the plane: n" << std::endl;
205  std::cout << (n).t() << std::endl;
206 
207  std::cout << "-------------------------------" << std::endl;
208  std::cout << "Compare with built homography H = R + t/d " << std::endl;
209  vpPlane bp(0, 0, 1, 1);
210  vpHomography aHb_built(aMb, bp);
211  std::cout << "aHb built from the displacement " << std::endl;
212  std::cout << std::endl << aHb_built / aHb_built[2][2] << std::endl;
213 
214  aHb_built.computeDisplacement(aRb, aTb, n);
215  std::cout << "Rotation: aRb" << std::endl;
216  std::cout << aRb << std::endl;
217  std::cout << "Translation: aTb" << std::endl;
218  std::cout << (aTb).t() << std::endl;
219  std::cout << "Normal to the plane: n" << std::endl;
220  std::cout << (n).t() << std::endl;
221 
222  std::cout << "-------------------------------" << std::endl;
223  std::cout << "test if ap = aHb bp" << std::endl;
224 
225  for (unsigned int i = 0; i < nbpt; i++) {
226  std::cout << "Point " << i << std::endl;
227  vpPoint p;
228  std::cout << "(";
229  std::cout << aP[i].get_x() / aP[i].get_w() << ", " << aP[i].get_y() / aP[i].get_w();
230  std::cout << ") = (";
231  p = aHb * bP[i];
232  std::cout << p.get_x() / p.get_w() << ", " << p.get_y() / p.get_w() << ")" << std::endl;
233  }
234 
235  std::cout << "-------------------------------" << std::endl;
236  std::cout << "test displacement" << std::endl;
237 
238  std::list<vpRotationMatrix> laRb;
239  std::list<vpTranslationVector> laTb;
240  std::list<vpColVector> lnb;
241 
242  vpHomography::computeDisplacement(aHb, bP[0].get_x(), bP[0].get_y(), laRb, laTb, lnb);
243 
244  std::list<vpRotationMatrix>::const_iterator it_laRb = laRb.begin();
245  std::list<vpTranslationVector>::const_iterator it_laTb = laTb.begin();
246  std::list<vpColVector>::const_iterator it_lnb = lnb.begin();
247 
248  int k = 1;
249  while (it_lnb != lnb.end()) {
250  std::cout << "Solution " << k++ << std::endl;
251 
252  aRb = *it_laRb;
253  aTb = *it_laTb;
254  n = *it_lnb;
255  std::cout << "Rotation: aRb" << std::endl;
256  std::cout << aRb << std::endl;
257  std::cout << "Translation: aTb" << std::endl;
258  std::cout << (aTb).t() << std::endl;
259  std::cout << "Normal to the plane: n" << std::endl;
260  std::cout << (n).t() << std::endl;
261 
262  ++it_laRb;
263  ++it_laTb;
264  ++it_lnb;
265  }
266  return EXIT_SUCCESS;
267  } catch (const vpException &e) {
268  std::cout << "Catch an exception: " << e << std::endl;
269  return EXIT_FAILURE;
270  }
271 #else
272  (void)argc;
273  (void)argv;
274  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
275  return EXIT_SUCCESS;
276 #endif
277 }
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:71
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
static void HLM(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, bool isplanar, vpHomography &aHb)
Implementation of a rotation matrix and operations on such kind of matrices.
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.cpp:474
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
static double rad(double deg)
Definition: vpMath.h:110
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:470
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:472
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:58
Class that consider the case of a translation vector.