Visual Servoing Platform  version 3.6.1 under development (2024-05-09)
homographyHLM3DObject.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
52 #include <visp3/core/vpDebug.h>
53 #include <visp3/core/vpMath.h>
54 #include <visp3/core/vpRotationMatrix.h>
55 #include <visp3/core/vpThetaUVector.h>
56 #include <visp3/vision/vpHomography.h>
57 
58 #include <stdlib.h>
59 #include <visp3/core/vpDebug.h>
60 #include <visp3/core/vpHomogeneousMatrix.h>
61 #include <visp3/core/vpMath.h>
62 #include <visp3/core/vpPoint.h>
63 #include <visp3/io/vpParseArgv.h>
64 // List of allowed command line options
65 #define GETOPTARGS "h"
66 
67 #define L 0.1
68 #define nbpt 11
69 
70 void usage(const char *name, const char *badparam);
71 bool getOptions(int argc, const char **argv);
72 
82 void usage(const char *name, const char *badparam)
83 {
84  fprintf(stdout, "\n\
85 Test the HLM (Malis) homography estimation algorithm with a 3D object.\n\
86 \n\
87 SYNOPSIS\n\
88  %s [-h]\n",
89  name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93  -h\n\
94  Print the help.\n");
95 
96  if (badparam) {
97  fprintf(stderr, "ERROR: \n");
98  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
99  }
100 }
111 bool getOptions(int argc, const char **argv)
112 {
113  const char *optarg_;
114  int c;
115  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 
117  switch (c) {
118  case 'h':
119  usage(argv[0], nullptr);
120  return false;
121  break;
122 
123  default:
124  usage(argv[0], optarg_);
125  return false;
126  break;
127  }
128  }
129 
130  if ((c == 1) || (c == -1)) {
131  // standalone param or error
132  usage(argv[0], nullptr);
133  std::cerr << "ERROR: " << std::endl;
134  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
135  return false;
136  }
137 
138  return true;
139 }
140 
141 int main(int argc, const char **argv)
142 {
143 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
144  try {
145  // Read the command line options
146  if (getOptions(argc, argv) == false) {
147  return EXIT_FAILURE;
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, L);
162  P[5].setWorldCoordinates(L, -2 * L, L);
163  P[6].setWorldCoordinates(L, -4 * L, 2 * L);
164  P[7].setWorldCoordinates(-2 * L, -L, -L);
165  P[8].setWorldCoordinates(-5 * L, -5 * L, L);
166  P[9].setWorldCoordinates(-2 * L, +3 * L, 2 * L);
167  P[10].setWorldCoordinates(-2 * L, -0.5 * L, 2 * L);
168 
169  vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
170  vpHomogeneousMatrix aMb(0.1, 0.1, 0.1, vpMath::rad(10), 0, vpMath::rad(40));
171  vpHomogeneousMatrix aMo = aMb * bMo;
172  for (unsigned int i = 0; i < nbpt; i++) {
173  P[i].project(aMo);
174  aP[i] = P[i];
175  xa[i] = P[i].get_x();
176  ya[i] = P[i].get_y();
177  }
178 
179  for (unsigned int i = 0; i < nbpt; i++) {
180  P[i].project(bMo);
181  bP[i] = P[i];
182  xb[i] = P[i].get_x();
183  yb[i] = P[i].get_y();
184  }
185 
186  vpRotationMatrix aRb;
188  vpColVector n;
189  std::cout << "-------------------------------" << std::endl;
190  std::cout << "Compare with built homography H = R + t/d n " << std::endl;
191  vpPlane bp(0, 0, 1, 1);
192  vpHomography aHb_built(aMb, bp);
193  std::cout << "aHb built from the displacement: \n" << aHb_built / aHb_built[2][2] << std::endl;
194 
195  aHb_built.computeDisplacement(aRb, aTb, n);
196  std::cout << "Rotation: aRb" << std::endl;
197  std::cout << aRb << std::endl;
198  std::cout << "Translation: aTb" << std::endl;
199  std::cout << (aTb).t() << std::endl;
200  std::cout << "Normal to the plane: n" << std::endl;
201  std::cout << (n).t() << std::endl;
202 
203  std::cout << "-------------------------------" << std::endl;
204  std::cout << "aMb " << std::endl << aMb << std::endl;
205  std::cout << "-------------------------------" << std::endl;
206  vpHomography aHb;
207 
208  vpHomography::HLM(xb, yb, xa, ya, false, aHb);
209 
210  std::cout << "aHb computed using the Malis paralax algorithm" << std::endl;
211  aHb /= aHb[2][2];
212  std::cout << std::endl << aHb << std::endl;
213 
214  std::cout << "-------------------------------" << std::endl;
215  std::cout << "extract R, T and n " << std::endl;
216  aHb.computeDisplacement(aRb, aTb, n);
217  std::cout << "Rotation: aRb" << std::endl;
218  std::cout << aRb << std::endl;
219  std::cout << "Translation: aTb" << std::endl;
220  std::cout << (aTb).t() << std::endl;
221  std::cout << "Normal to the plane: n" << std::endl;
222  std::cout << (n).t() << std::endl;
223 
224  std::cout << "-------------------------------" << std::endl;
225  std::cout << "test if ap = aHb bp" << std::endl;
226 
227  for (unsigned int i = 0; i < nbpt; i++) {
228  std::cout << "Point " << i << std::endl;
229  vpPoint p;
230  std::cout << "(";
231  std::cout << aP[i].get_x() / aP[i].get_w() << ", " << aP[i].get_y() / aP[i].get_w();
232  std::cout << ") = (";
233  p = aHb * bP[i];
234  std::cout << p.get_x() / p.get_w() << ", " << p.get_y() / p.get_w() << ")" << std::endl;
235  }
236  return EXIT_SUCCESS;
237  }
238  catch (const vpException &e) {
239  std::cout << "Catch an exception: " << e << std::endl;
240  return EXIT_FAILURE;
241  }
242 #else
243  (void)argc;
244  (void)argv;
245  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
246  return EXIT_SUCCESS;
247 #endif
248 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:168
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)
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
static double rad(double deg)
Definition: vpMath.h:127
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:54
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.cpp:467
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:465
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:463
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:110
Implementation of a rotation matrix and operations on such kind of matrices.
Class that consider the case of a translation vector.