Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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  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), xb(nbpt), yb(nbpt);
152 
153  vpPoint aP[nbpt]; // Point to be tracked
154  vpPoint bP[nbpt]; // Point to be tracked
155 
156  P[0].setWorldCoordinates(-L, -L, 0);
157  P[1].setWorldCoordinates(2 * L, -L, 0);
158  P[2].setWorldCoordinates(L, L, 0);
159  P[3].setWorldCoordinates(-L, 3 * L, 0);
160  P[4].setWorldCoordinates(0, 0, 0);
161  /*
162  P[5].setWorldCoordinates(10,20, 0 ) ;
163  P[6].setWorldCoordinates(-10,12, 0 ) ;
164  */
165  vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
166  vpHomogeneousMatrix aMb(1, 0, 0.0, vpMath::rad(10), 0, vpMath::rad(40));
167  vpHomogeneousMatrix aMo = aMb * bMo;
168  for (unsigned int i = 0; i < nbpt; i++) {
169  P[i].project(aMo);
170  aP[i] = P[i];
171  xa[i] = P[i].get_x();
172  ya[i] = P[i].get_y();
173  }
174 
175  for (unsigned int i = 0; i < nbpt; i++) {
176  P[i].project(bMo);
177  bP[i] = P[i];
178  xb[i] = P[i].get_x();
179  yb[i] = P[i].get_y();
180  }
181  std::cout << "-------------------------------" << std::endl;
182  std::cout << "aMb " << std::endl << aMb << std::endl;
183  std::cout << "-------------------------------" << std::endl;
184  vpHomography aHb;
185 
186  vpHomography::DLT(xb, yb, xa, ya, aHb, true);
187 
188  vpTRACE("aHb computed using the DLT algorithm");
189  aHb /= aHb[2][2];
190  std::cout << std::endl << aHb << std::endl;
191 
192  vpRotationMatrix aRb;
194  vpColVector n;
195 
196  std::cout << "-------------------------------" << std::endl;
197  vpTRACE("extract R, T and n ");
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  vpTRACE("Compare with built homoraphy H = R + t/d ");
208  vpPlane bp(0, 0, 1, 1);
209  vpHomography aHb_built(aMb, bp);
210  vpTRACE("aHb built from the displacement ");
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  vpTRACE("test if ap = aHb bp");
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  return EXIT_SUCCESS;
234  } catch (const vpException &e) {
235  std::cout << "Catch an exception: " << e << std::endl;
236  return EXIT_FAILURE;
237  }
238 }
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
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:429
static double rad(double deg)
Definition: vpMath.h:102
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)
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.