Visual Servoing Platform  version 3.6.1 under development (2024-05-09)
homographyHartleyDLT2DObject.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  * Example of the HartleyDLT homography estimation algorithm
33  *
34 *****************************************************************************/
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/core/vpMath.h>
52 #include <visp3/core/vpRotationMatrix.h>
53 #include <visp3/core/vpThetaUVector.h>
54 #include <visp3/vision/vpHomography.h>
55 
56 #include <stdlib.h>
57 #include <visp3/core/vpDebug.h>
58 #include <visp3/core/vpHomogeneousMatrix.h>
59 #include <visp3/core/vpMath.h>
60 #include <visp3/core/vpPoint.h>
61 #include <visp3/io/vpParseArgv.h>
62 // List of allowed command line options
63 #define GETOPTARGS "h"
64 
65 #define L 0.1
66 #define nbpt 5
67 
68 void usage(const char *name, const char *badparam);
69 bool getOptions(int argc, const char **argv);
70 
80 void usage(const char *name, const char *badparam)
81 {
82  fprintf(stdout, "\n\
83 Test the HartleyDLT homography estimation algorithm.\n\
84 \n\
85 SYNOPSIS\n\
86  %s [-h]\n",
87  name);
88 
89  fprintf(stdout, "\n\
90 OPTIONS: Default\n\
91  -h\n\
92  Print the help.\n");
93 
94  if (badparam) {
95  fprintf(stderr, "ERROR: \n");
96  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
97  }
98 }
110 bool getOptions(int argc, const char **argv)
111 {
112  const char *optarg_;
113  int c;
114  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
115 
116  switch (c) {
117  case 'h':
118  usage(argv[0], nullptr);
119  return false;
120  break;
121 
122  default:
123  usage(argv[0], optarg_);
124  return false;
125  break;
126  }
127  }
128 
129  if ((c == 1) || (c == -1)) {
130  // standalone param or error
131  usage(argv[0], nullptr);
132  std::cerr << "ERROR: " << std::endl;
133  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
134  return false;
135  }
136 
137  return true;
138 }
139 
140 int main(int argc, const char **argv)
141 {
142 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
143  try {
144  // Read the command line options
145  if (getOptions(argc, argv) == false) {
146  return EXIT_FAILURE;
147  }
148 
149  vpPoint P[nbpt]; // Point to be tracked
150  std::vector<double> xa(nbpt), ya(nbpt), xb(nbpt), yb(nbpt);
151 
152  vpPoint aP[nbpt]; // Point to be tracked
153  vpPoint bP[nbpt]; // Point to be tracked
154 
155  P[0].setWorldCoordinates(-L, -L, 0);
156  P[1].setWorldCoordinates(2 * L, -L, 0);
157  P[2].setWorldCoordinates(L, L, 0);
158  P[3].setWorldCoordinates(-L, 3 * L, 0);
159  P[4].setWorldCoordinates(0, 0, 0);
160  /*
161  P[5].setWorldCoordinates(10,20, 0 ) ;
162  P[6].setWorldCoordinates(-10,12, 0 ) ;
163  */
164  vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
165  vpHomogeneousMatrix aMb(1, 0, 0.0, vpMath::rad(10), 0, vpMath::rad(40));
166  vpHomogeneousMatrix aMo = aMb * bMo;
167  for (unsigned int i = 0; i < nbpt; i++) {
168  P[i].project(aMo);
169  aP[i] = P[i];
170  xa[i] = P[i].get_x();
171  ya[i] = P[i].get_y();
172  }
173 
174  for (unsigned int i = 0; i < nbpt; i++) {
175  P[i].project(bMo);
176  bP[i] = P[i];
177  xb[i] = P[i].get_x();
178  yb[i] = P[i].get_y();
179  }
180  std::cout << "-------------------------------" << std::endl;
181  std::cout << "aMb " << std::endl << aMb << std::endl;
182  std::cout << "-------------------------------" << std::endl;
183  vpHomography aHb;
184 
185  vpHomography::DLT(xb, yb, xa, ya, aHb, true);
186 
187  vpTRACE("aHb computed using the DLT algorithm");
188  aHb /= aHb[2][2];
189  std::cout << std::endl << aHb << std::endl;
190 
191  vpRotationMatrix aRb;
193  vpColVector n;
194 
195  std::cout << "-------------------------------" << std::endl;
196  vpTRACE("extract R, T and n ");
197  aHb.computeDisplacement(aRb, aTb, n);
198  std::cout << "Rotation: aRb" << std::endl;
199  std::cout << aRb << std::endl;
200  std::cout << "Translation: aTb" << std::endl;
201  std::cout << (aTb).t() << std::endl;
202  std::cout << "Normal to the plane: n" << std::endl;
203  std::cout << (n).t() << std::endl;
204 
205  std::cout << "-------------------------------" << std::endl;
206  vpTRACE("Compare with built homoraphy H = R + t/d ");
207  vpPlane bp(0, 0, 1, 1);
208  vpHomography aHb_built(aMb, bp);
209  vpTRACE("aHb built from the displacement ");
210  std::cout << std::endl << aHb_built / aHb_built[2][2] << std::endl;
211 
212  aHb_built.computeDisplacement(aRb, aTb, n);
213  std::cout << "Rotation: aRb" << std::endl;
214  std::cout << aRb << std::endl;
215  std::cout << "Translation: aTb" << std::endl;
216  std::cout << (aTb).t() << std::endl;
217  std::cout << "Normal to the plane: n" << std::endl;
218  std::cout << (n).t() << std::endl;
219 
220  std::cout << "-------------------------------" << std::endl;
221  vpTRACE("test if ap = aHb bp");
222 
223  for (unsigned int i = 0; i < nbpt; i++) {
224  std::cout << "Point " << i << std::endl;
225  vpPoint p;
226  std::cout << "(";
227  std::cout << aP[i].get_x() / aP[i].get_w() << ", " << aP[i].get_y() / aP[i].get_w();
228  std::cout << ") = (";
229  p = aHb * bP[i];
230  std::cout << p.get_x() / p.get_w() << ", " << p.get_y() / p.get_w() << ")" << std::endl;
231  }
232  return EXIT_SUCCESS;
233  }
234  catch (const vpException &e) {
235  std::cout << "Catch an exception: " << e << std::endl;
236  return EXIT_FAILURE;
237  }
238 #else
239  (void)argc;
240  (void)argv;
241  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
242  return EXIT_SUCCESS;
243 #endif
244 }
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 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 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.
#define vpTRACE
Definition: vpDebug.h:405