ViSP  2.9.0
homographyHLM3DObject.cpp
1 /****************************************************************************
2  *
3  * $Id: homographyHLM3DObject.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Test the HLM (Malis) homography estimation algorithm with a 3D object.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
57 #include <visp/vpMath.h>
58 #include <visp/vpRotationMatrix.h>
59 #include <visp/vpHomography.h>
60 #include <visp/vpDebug.h>
61 #include <visp/vpThetaUVector.h>
62 
63 #include <visp/vpPoint.h>
64 #include <visp/vpMath.h>
65 #include <visp/vpHomogeneousMatrix.h>
66 #include <visp/vpDebug.h>
67 #include <visp/vpParseArgv.h>
68 #include <stdlib.h>
69 // List of allowed command line options
70 #define GETOPTARGS "h"
71 
72 #define L 0.1
73 #define nbpt 11
74 
75 void usage(const char *name, const char *badparam);
76 bool getOptions(int argc, const char **argv);
77 
87 void usage(const char *name, const char *badparam)
88 {
89  fprintf(stdout, "\n\
90 Test the HLM (Malis) homography estimation algorithm with a 3D object.\n\
91 \n\
92 SYNOPSIS\n\
93  %s [-h]\n", name);
94 
95  fprintf(stdout, "\n\
96 OPTIONS: Default\n\
97  -h\n\
98  Print the help.\n");
99 
100  if (badparam) {
101  fprintf(stderr, "ERROR: \n" );
102  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
103  }
104 }
115 bool getOptions(int argc, const char **argv)
116 {
117  const char *optarg_;
118  int c;
119  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
120 
121  switch (c) {
122  case 'h': usage(argv[0], NULL); return false; break;
123 
124  default:
125  usage(argv[0], optarg_);
126  return false; break;
127  }
128  }
129 
130  if ((c == 1) || (c == -1)) {
131  // standalone param or error
132  usage(argv[0], NULL);
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 
142 int
143 main(int argc, const char ** argv)
144 {
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) ;
153  std::vector<double> xb(nbpt), yb(nbpt) ;
154 
155  vpPoint aP[nbpt] ; // Point to be tracked
156  vpPoint bP[nbpt] ; // Point to be tracked
157 
158  P[0].setWorldCoordinates(-L,-L, 0 ) ;
159  P[1].setWorldCoordinates(2*L,-L, 0 ) ;
160  P[2].setWorldCoordinates(L,L, 0 ) ;
161  P[3].setWorldCoordinates(-L,3*L, 0 ) ;
162  P[4].setWorldCoordinates(0,0, L ) ;
163  P[5].setWorldCoordinates(L,-2*L, L ) ;
164  P[6].setWorldCoordinates(L,-4*L, 2*L ) ;
165  P[7].setWorldCoordinates(-2*L,-L, -L ) ;
166  P[8].setWorldCoordinates(-5*L,-5*L, L ) ;
167  P[9].setWorldCoordinates(-2*L,+3*L, 2*L ) ;
168  P[10].setWorldCoordinates(-2*L,-0.5*L, 2*L ) ;
169 
170  vpHomogeneousMatrix bMo(0,0,1, 0,0,0) ;
171  vpHomogeneousMatrix aMb(0.1,0.1,0.1,vpMath::rad(10),0,vpMath::rad(40)) ;
172  vpHomogeneousMatrix aMo =aMb*bMo ;
173  for(unsigned int i=0 ; i < nbpt ; i++)
174  {
175  P[i].project(aMo) ;
176  aP[i] = P[i] ;
177  xa[i] = P[i].get_x() ;
178  ya[i] = P[i].get_y() ;
179  }
180 
181  for(unsigned int i=0 ; i < nbpt ; i++)
182  {
183  P[i].project(bMo) ;
184  bP[i] = P[i] ;
185  xb[i] = P[i].get_x() ;
186  yb[i] = P[i].get_y() ;
187  }
188 
189  vpRotationMatrix aRb ;
190  vpTranslationVector aTb ;
191  vpColVector n ;
192  std::cout << "-------------------------------" << std::endl ;
193  std::cout << "Compare with built homography H = R + t/d n " << std::endl ;
194  vpPlane bp(0,0,1,1) ;
195  vpHomography aHb_built(aMb,bp) ;
196  std::cout << "aHb built from the displacement: \n" << aHb_built/aHb_built[2][2] << std::endl ;
197 
198  aHb_built.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  std::cout << "aMb "<<std::endl <<aMb << std::endl ;
208  std::cout << "-------------------------------" <<std::endl ;
209  vpHomography aHb ;
210 
211  vpHomography::HLM(xb, yb, xa, ya, false, aHb) ;
212 
213  std::cout << "aHb computed using the Malis paralax algorithm" << std::endl ;
214  aHb /= aHb[2][2] ;
215  std::cout << std::endl << aHb<< std::endl ;
216 
217  std::cout << "-------------------------------" <<std::endl ;
218  std::cout << "extract R, T and n " << std::endl ;
219  aHb.computeDisplacement(aRb, aTb, n) ;
220  std::cout << "Rotation: aRb" <<std::endl ;
221  std::cout << aRb << std::endl ;
222  std::cout << "Translation: aTb" <<std::endl;
223  std::cout << (aTb).t() <<std::endl ;
224  std::cout << "Normal to the plane: n" <<std::endl;
225  std::cout << (n).t() <<std::endl ;
226 
227  std::cout << "-------------------------------" <<std::endl ;
228  std::cout << "test if ap = aHb bp" << std::endl ;
229 
230  for(unsigned int i=0 ; i < nbpt ; i++)
231  {
232  std::cout << "Point "<< i<< std::endl ;
233  vpPoint p ;
234  std::cout << "(" ;
235  std::cout << aP[i].get_x()/aP[i].get_w()<<", "<< aP[i].get_y()/aP[i].get_w() ;
236  std::cout <<") = (" ;
237  p = aHb*bP[i] ;
238  std::cout << p.get_x() /p.get_w()<<", "<< p.get_y()/ p.get_w() <<")"<<std::endl ;
239  }
240  return 0;
241  }
242  catch(vpException e) {
243  std::cout << "Catch an exception: " << e << std::endl;
244  return 1;
245  }
246 }
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
error that can be emited by ViSP classes.
Definition: vpException.h:76
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.h:138
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.h:140
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
Class that defines what is a point.
Definition: vpPoint.h:65
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)
The vpRotationMatrix considers the particular case of a rotation matrix.
This class aims to compute the homography wrt.two images.
Definition: vpHomography.h:178
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.h:136
static double rad(double deg)
Definition: vpMath.h:100
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:67
Class that consider the case of a translation vector.
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74