Visual Servoing Platform  version 3.0.1
homographyHLM3DObject.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test the HLM (Malis) homography estimation algorithm with a 3D object.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
53 #include <visp3/core/vpMath.h>
54 #include <visp3/core/vpRotationMatrix.h>
55 #include <visp3/vision/vpHomography.h>
56 #include <visp3/core/vpDebug.h>
57 #include <visp3/core/vpThetaUVector.h>
58 
59 #include <visp3/core/vpPoint.h>
60 #include <visp3/core/vpMath.h>
61 #include <visp3/core/vpHomogeneousMatrix.h>
62 #include <visp3/core/vpDebug.h>
63 #include <visp3/io/vpParseArgv.h>
64 #include <stdlib.h>
65 // List of allowed command line options
66 #define GETOPTARGS "h"
67 
68 #define L 0.1
69 #define nbpt 11
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 HLM (Malis) homography estimation algorithm with a 3D object.\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 }
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': usage(argv[0], NULL); return false; break;
119 
120  default:
121  usage(argv[0], optarg_);
122  return false; break;
123  }
124  }
125 
126  if ((c == 1) || (c == -1)) {
127  // standalone param or error
128  usage(argv[0], NULL);
129  std::cerr << "ERROR: " << std::endl;
130  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
131  return false;
132  }
133 
134  return true;
135 }
136 
137 
138 int
139 main(int argc, const char ** argv)
140 {
141  try {
142  // Read the command line options
143  if (getOptions(argc, argv) == false) {
144  exit (-1);
145  }
146 
147  vpPoint P[nbpt] ; // Point to be tracked
148  std::vector<double> xa(nbpt), ya(nbpt) ;
149  std::vector<double> xb(nbpt), yb(nbpt) ;
150 
151  vpPoint aP[nbpt] ; // Point to be tracked
152  vpPoint bP[nbpt] ; // Point to be tracked
153 
154  P[0].setWorldCoordinates(-L,-L, 0 ) ;
155  P[1].setWorldCoordinates(2*L,-L, 0 ) ;
156  P[2].setWorldCoordinates(L,L, 0 ) ;
157  P[3].setWorldCoordinates(-L,3*L, 0 ) ;
158  P[4].setWorldCoordinates(0,0, L ) ;
159  P[5].setWorldCoordinates(L,-2*L, L ) ;
160  P[6].setWorldCoordinates(L,-4*L, 2*L ) ;
161  P[7].setWorldCoordinates(-2*L,-L, -L ) ;
162  P[8].setWorldCoordinates(-5*L,-5*L, L ) ;
163  P[9].setWorldCoordinates(-2*L,+3*L, 2*L ) ;
164  P[10].setWorldCoordinates(-2*L,-0.5*L, 2*L ) ;
165 
166  vpHomogeneousMatrix bMo(0,0,1, 0,0,0) ;
167  vpHomogeneousMatrix aMb(0.1,0.1,0.1,vpMath::rad(10),0,vpMath::rad(40)) ;
168  vpHomogeneousMatrix aMo =aMb*bMo ;
169  for(unsigned int i=0 ; i < nbpt ; i++)
170  {
171  P[i].project(aMo) ;
172  aP[i] = P[i] ;
173  xa[i] = P[i].get_x() ;
174  ya[i] = P[i].get_y() ;
175  }
176 
177  for(unsigned int i=0 ; i < nbpt ; i++)
178  {
179  P[i].project(bMo) ;
180  bP[i] = P[i] ;
181  xb[i] = P[i].get_x() ;
182  yb[i] = P[i].get_y() ;
183  }
184 
185  vpRotationMatrix aRb ;
186  vpTranslationVector aTb ;
187  vpColVector n ;
188  std::cout << "-------------------------------" << std::endl ;
189  std::cout << "Compare with built homography H = R + t/d n " << std::endl ;
190  vpPlane bp(0,0,1,1) ;
191  vpHomography aHb_built(aMb,bp) ;
192  std::cout << "aHb built from the displacement: \n" << aHb_built/aHb_built[2][2] << std::endl ;
193 
194  aHb_built.computeDisplacement(aRb, aTb, n) ;
195  std::cout << "Rotation: aRb" <<std::endl ;
196  std::cout << aRb << std::endl ;
197  std::cout << "Translation: aTb" <<std::endl;
198  std::cout << (aTb).t() <<std::endl ;
199  std::cout << "Normal to the plane: n" <<std::endl;
200  std::cout << (n).t() <<std::endl ;
201 
202  std::cout << "-------------------------------" <<std::endl ;
203  std::cout << "aMb "<<std::endl <<aMb << std::endl ;
204  std::cout << "-------------------------------" <<std::endl ;
205  vpHomography aHb ;
206 
207  vpHomography::HLM(xb, yb, xa, ya, false, aHb) ;
208 
209  std::cout << "aHb computed using the Malis paralax algorithm" << std::endl ;
210  aHb /= aHb[2][2] ;
211  std::cout << std::endl << aHb<< std::endl ;
212 
213  std::cout << "-------------------------------" <<std::endl ;
214  std::cout << "extract R, T and n " << std::endl ;
215  aHb.computeDisplacement(aRb, aTb, n) ;
216  std::cout << "Rotation: aRb" <<std::endl ;
217  std::cout << aRb << std::endl ;
218  std::cout << "Translation: aTb" <<std::endl;
219  std::cout << (aTb).t() <<std::endl ;
220  std::cout << "Normal to the plane: n" <<std::endl;
221  std::cout << (n).t() <<std::endl ;
222 
223  std::cout << "-------------------------------" <<std::endl ;
224  std::cout << "test if ap = aHb bp" << std::endl ;
225 
226  for(unsigned int i=0 ; i < nbpt ; i++)
227  {
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 0;
237  }
238  catch(vpException &e) {
239  std::cout << "Catch an exception: " << e << std::endl;
240  return 1;
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:73
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
Class that defines what is a point.
Definition: vpPoint.h:59
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.
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.cpp:460
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:179
static double rad(double deg)
Definition: vpMath.h:104
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:456
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:458
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:58
Class that consider the case of a translation vector.