Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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  try {
145  // Read the command line options
146  if (getOptions(argc, argv) == false) {
147  exit(-1);
148  }
149 
150  vpPoint P[nbpt]; // Point to be tracked
151  std::vector<double> xa(nbpt), ya(nbpt);
152  std::vector<double> xb(nbpt), yb(nbpt);
153 
154  vpPoint aP[nbpt]; // Point to be tracked
155  vpPoint bP[nbpt]; // Point to be tracked
156 
157  P[0].setWorldCoordinates(-L, -L, 0);
158  P[1].setWorldCoordinates(2 * L, -L, 0);
159  P[2].setWorldCoordinates(L, L, 0);
160  P[3].setWorldCoordinates(-L, 3 * L, 0);
161  P[4].setWorldCoordinates(0, 0, 0);
162  /*
163  P[5].setWorldCoordinates(10,20, 0 ) ;
164  P[6].setWorldCoordinates(-10,12, 0 ) ;
165  */
166  vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
167  vpHomogeneousMatrix aMb(1, 0, 0.0, vpMath::rad(10), 0, vpMath::rad(40));
168  vpHomogeneousMatrix aMo = aMb * bMo;
169  for (unsigned int i = 0; i < nbpt; i++) {
170  P[i].project(aMo);
171  aP[i] = P[i];
172  xa[i] = P[i].get_x();
173  ya[i] = P[i].get_y();
174  }
175 
176  for (unsigned int i = 0; i < nbpt; i++) {
177  P[i].project(bMo);
178  bP[i] = P[i];
179  xb[i] = P[i].get_x();
180  yb[i] = P[i].get_y();
181  }
182  std::cout << "-------------------------------" << std::endl;
183  std::cout << "aMb " << std::endl << aMb << std::endl;
184  std::cout << "-------------------------------" << std::endl;
185  vpHomography aHb;
186 
187  vpHomography::HLM(xb, yb, xa, ya, true, aHb);
188 
189  aHb /= aHb[2][2];
190  std::cout << "aHb computed using the Malis paralax algorithm: \n" << aHb << std::endl;
191 
192  vpRotationMatrix aRb;
194  vpColVector n;
195 
196  std::cout << "-------------------------------" << std::endl;
197  std::cout << "extract R, T and n " << std::endl;
198  aHb.computeDisplacement(aRb, aTb, n);
199  std::cout << "Rotation: aRb" << std::endl;
200  std::cout << aRb << std::endl;
201  std::cout << "Translation: aTb" << std::endl;
202  std::cout << (aTb).t() << std::endl;
203  std::cout << "Normal to the plane: n" << std::endl;
204  std::cout << (n).t() << std::endl;
205 
206  std::cout << "-------------------------------" << std::endl;
207  std::cout << "Compare with built homography H = R + t/d " << std::endl;
208  vpPlane bp(0, 0, 1, 1);
209  vpHomography aHb_built(aMb, bp);
210  std::cout << "aHb built from the displacement " << std::endl;
211  std::cout << std::endl << aHb_built / aHb_built[2][2] << std::endl;
212 
213  aHb_built.computeDisplacement(aRb, aTb, n);
214  std::cout << "Rotation: aRb" << std::endl;
215  std::cout << aRb << std::endl;
216  std::cout << "Translation: aTb" << std::endl;
217  std::cout << (aTb).t() << std::endl;
218  std::cout << "Normal to the plane: n" << std::endl;
219  std::cout << (n).t() << std::endl;
220 
221  std::cout << "-------------------------------" << std::endl;
222  std::cout << "test if ap = aHb bp" << std::endl;
223 
224  for (unsigned int i = 0; i < nbpt; i++) {
225  std::cout << "Point " << i << std::endl;
226  vpPoint p;
227  std::cout << "(";
228  std::cout << aP[i].get_x() / aP[i].get_w() << ", " << aP[i].get_y() / aP[i].get_w();
229  std::cout << ") = (";
230  p = aHb * bP[i];
231  std::cout << p.get_x() / p.get_w() << ", " << p.get_y() / p.get_w() << ")" << std::endl;
232  }
233 
234  std::cout << "-------------------------------" << std::endl;
235  std::cout << "test displacement" << std::endl;
236 
237  std::list<vpRotationMatrix> laRb;
238  std::list<vpTranslationVector> laTb;
239  std::list<vpColVector> lnb;
240 
241  vpHomography::computeDisplacement(aHb, bP[0].get_x(), bP[0].get_y(), laRb, laTb, lnb);
242 
243  std::list<vpRotationMatrix>::const_iterator it_laRb = laRb.begin();
244  std::list<vpTranslationVector>::const_iterator it_laTb = laTb.begin();
245  std::list<vpColVector>::const_iterator it_lnb = lnb.begin();
246 
247  int k = 1;
248  while (it_lnb != lnb.end()) {
249  std::cout << "Solution " << k++ << std::endl;
250 
251  aRb = *it_laRb;
252  aTb = *it_laTb;
253  n = *it_lnb;
254  std::cout << "Rotation: aRb" << std::endl;
255  std::cout << aRb << std::endl;
256  std::cout << "Translation: aTb" << std::endl;
257  std::cout << (aTb).t() << std::endl;
258  std::cout << "Normal to the plane: n" << std::endl;
259  std::cout << (n).t() << std::endl;
260 
261  ++it_laRb;
262  ++it_laTb;
263  ++it_lnb;
264  }
265  return EXIT_SUCCESS;
266  } catch (const vpException &e) {
267  std::cout << "Catch an exception: " << e << std::endl;
268  return EXIT_FAILURE;
269  }
270 }
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:71
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:431
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.cpp:433
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 what is a point.
Definition: vpPoint.h:58
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.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:429
static double rad(double deg)
Definition: vpMath.h:102
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:113
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:58
Class that consider the case of a translation vector.