Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
homographyHLM3DObject.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  * Test the HLM (Malis) homography estimation algorithm with a 3D object.
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 
70 #define L 0.1
71 #define nbpt 11
72 
73 void usage(const char *name, const char *badparam);
74 bool getOptions(int argc, const char **argv);
75 
85 void usage(const char *name, const char *badparam)
86 {
87  fprintf(stdout, "\n\
88 Test the HLM (Malis) homography estimation algorithm with a 3D object.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-h]\n", name);
92 
93  fprintf(stdout, "\n\
94 OPTIONS: Default\n\
95  -h\n\
96  Print the help.\n");
97 
98  if (badparam) {
99  fprintf(stderr, "ERROR: \n");
100  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
101  }
102 }
113 bool getOptions(int argc, const char **argv)
114 {
115  const char *optarg_;
116  int c;
117  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
118 
119  switch (c) {
120  case 'h':
121  usage(argv[0], NULL);
122  return false;
123  break;
124 
125  default:
126  usage(argv[0], optarg_);
127  return false;
128  break;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], NULL);
135  std::cerr << "ERROR: " << std::endl;
136  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137  return false;
138  }
139 
140  return true;
141 }
142 
143 int main(int argc, const char **argv)
144 {
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, L);
163  P[5].setWorldCoordinates(L, -2 * L, L);
164  P[6].setWorldCoordinates(L, -4 * L, 2 * L);
165  P[7].setWorldCoordinates(-2 * L, -L, -L);
166  P[8].setWorldCoordinates(-5 * L, -5 * L, L);
167  P[9].setWorldCoordinates(-2 * L, +3 * L, 2 * L);
168  P[10].setWorldCoordinates(-2 * L, -0.5 * L, 2 * L);
169 
170  vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
171  vpHomogeneousMatrix aMb(0.1, 0.1, 0.1, vpMath::rad(10), 0, vpMath::rad(40));
172  vpHomogeneousMatrix aMo = aMb * bMo;
173  for (unsigned int i = 0; i < nbpt; i++) {
174  P[i].project(aMo);
175  aP[i] = P[i];
176  xa[i] = P[i].get_x();
177  ya[i] = P[i].get_y();
178  }
179 
180  for (unsigned int i = 0; i < nbpt; i++) {
181  P[i].project(bMo);
182  bP[i] = P[i];
183  xb[i] = P[i].get_x();
184  yb[i] = P[i].get_y();
185  }
186 
187  vpRotationMatrix aRb;
189  vpColVector n;
190  std::cout << "-------------------------------" << std::endl;
191  std::cout << "Compare with built homography H = R + t/d n " << std::endl;
192  vpPlane bp(0, 0, 1, 1);
193  vpHomography aHb_built(aMb, bp);
194  std::cout << "aHb built from the displacement: \n" << aHb_built / aHb_built[2][2] << std::endl;
195 
196  aHb_built.computeDisplacement(aRb, aTb, n);
197  std::cout << "Rotation: aRb" << std::endl;
198  std::cout << aRb << std::endl;
199  std::cout << "Translation: aTb" << std::endl;
200  std::cout << (aTb).t() << std::endl;
201  std::cout << "Normal to the plane: n" << std::endl;
202  std::cout << (n).t() << std::endl;
203 
204  std::cout << "-------------------------------" << std::endl;
205  std::cout << "aMb " << std::endl << aMb << std::endl;
206  std::cout << "-------------------------------" << std::endl;
207  vpHomography aHb;
208 
209  vpHomography::HLM(xb, yb, xa, ya, false, aHb);
210 
211  std::cout << "aHb computed using the Malis paralax algorithm" << std::endl;
212  aHb /= aHb[2][2];
213  std::cout << std::endl << aHb << std::endl;
214 
215  std::cout << "-------------------------------" << std::endl;
216  std::cout << "extract R, T and n " << std::endl;
217  aHb.computeDisplacement(aRb, aTb, n);
218  std::cout << "Rotation: aRb" << std::endl;
219  std::cout << aRb << std::endl;
220  std::cout << "Translation: aTb" << std::endl;
221  std::cout << (aTb).t() << std::endl;
222  std::cout << "Normal to the plane: n" << std::endl;
223  std::cout << (n).t() << std::endl;
224 
225  std::cout << "-------------------------------" << std::endl;
226  std::cout << "test if ap = aHb bp" << std::endl;
227 
228  for (unsigned int i = 0; i < nbpt; i++) {
229  std::cout << "Point " << i << std::endl;
230  vpPoint p;
231  std::cout << "(";
232  std::cout << aP[i].get_x() / aP[i].get_w() << ", " << aP[i].get_y() / aP[i].get_w();
233  std::cout << ") = (";
234  p = aHb * bP[i];
235  std::cout << p.get_x() / p.get_w() << ", " << p.get_y() / p.get_w() << ")" << std::endl;
236  }
237  return EXIT_SUCCESS;
238  } catch (const vpException &e) {
239  std::cout << "Catch an exception: " << e << std::endl;
240  return EXIT_FAILURE;
241  }
242 }
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.