Visual Servoing Platform  version 3.4.0
homographyHartleyDLT2DObject.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 HartleyDLT homography estimation algorithm.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
53 #include <visp3/core/vpDebug.h>
54 #include <visp3/core/vpMath.h>
55 #include <visp3/core/vpRotationMatrix.h>
56 #include <visp3/core/vpThetaUVector.h>
57 #include <visp3/vision/vpHomography.h>
58 
59 #include <stdlib.h>
60 #include <visp3/core/vpDebug.h>
61 #include <visp3/core/vpHomogeneousMatrix.h>
62 #include <visp3/core/vpMath.h>
63 #include <visp3/core/vpPoint.h>
64 #include <visp3/io/vpParseArgv.h>
65 // List of allowed command line options
66 #define GETOPTARGS "h"
67 
68 #define L 0.1
69 #define nbpt 5
70 
71 void usage(const char *name, const char *badparam);
72 bool getOptions(int argc, const char **argv);
73 
83 void usage(const char *name, const char *badparam)
84 {
85  fprintf(stdout, "\n\
86 Test the HartleyDLT homography estimation algorithm.\n\
87 \n\
88 SYNOPSIS\n\
89  %s [-h]\n", 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 }
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), 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::DLT(xb, yb, xa, ya, aHb, true);
188 
189  vpTRACE("aHb computed using the DLT algorithm");
190  aHb /= aHb[2][2];
191  std::cout << std::endl << aHb << std::endl;
192 
193  vpRotationMatrix aRb;
195  vpColVector n;
196 
197  std::cout << "-------------------------------" << std::endl;
198  vpTRACE("extract R, T and n ");
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  vpTRACE("Compare with built homoraphy H = R + t/d ");
209  vpPlane bp(0, 0, 1, 1);
210  vpHomography aHb_built(aMb, bp);
211  vpTRACE("aHb built from the displacement ");
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  vpTRACE("test if ap = aHb bp");
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  return EXIT_SUCCESS;
235  } catch (const vpException &e) {
236  std::cout << "Catch an exception: " << e << std::endl;
237  return EXIT_FAILURE;
238  }
239 #else
240  (void)argc;
241  (void)argv;
242  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
243  return EXIT_SUCCESS;
244 #endif
245 }
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
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:458
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.cpp:460
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
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
#define vpTRACE
Definition: vpDebug.h:416
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:456
static double rad(double deg)
Definition: vpMath.h:110
static void DLT(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, vpHomography &aHb, bool normalization=true)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:58
Class that consider the case of a translation vector.