ViSP  2.8.0
homographyHLM2DObject.cpp
1 /****************************************************************************
2  *
3  * $Id: homographyHLM2DObject.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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  * Example of the HLM (Malis) homography estimation algorithm.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
59 #include <visp/vpMath.h>
60 #include <visp/vpRotationMatrix.h>
61 #include <visp/vpHomography.h>
62 #include <visp/vpDebug.h>
63 #include <visp/vpThetaUVector.h>
64 
65 #include <visp/vpPoint.h>
66 #include <visp/vpMath.h>
67 #include <visp/vpHomogeneousMatrix.h>
68 #include <visp/vpDebug.h>
69 #include <visp/vpParseArgv.h>
70 #include <stdlib.h>
71 // List of allowed command line options
72 #define GETOPTARGS "h"
73 #define L 0.1
74 #define nbpt 5
75 
85 void usage(const char *name, const char *badparam)
86 {
87  fprintf(stdout, "\n\
88 Test the HLM (Malis) homography estimation algorithm with a planar object.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-h]\n", name);
92 
93  fprintf(stdout, "\n\
94 OPTIONS: Default\n\
95  -h\n\
96  Print the help.\n");
97 
98  if (badparam) {
99  fprintf(stderr, "ERROR: \n" );
100  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
101  }
102 }
113 bool getOptions(int argc, const char **argv)
114 {
115  const char *optarg;
116  int c;
117  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
118 
119  switch (c) {
120  case 'h': usage(argv[0], NULL); return false; break;
121 
122  default:
123  usage(argv[0], optarg);
124  return false; break;
125  }
126  }
127 
128  if ((c == 1) || (c == -1)) {
129  // standalone param or error
130  usage(argv[0], NULL);
131  std::cerr << "ERROR: " << std::endl;
132  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
133  return false;
134  }
135 
136  return true;
137 }
138 
139 
140 int
141 main(int argc, const char ** argv)
142 {
143  // Read the command line options
144  if (getOptions(argc, argv) == false) {
145  exit (-1);
146  }
147 
148  int i ;
149 
150  vpPoint P[nbpt] ; // Point to be tracked
151  double xa[nbpt], ya[nbpt] ;
152  double 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(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(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  std::cout << "-------------------------------" <<std::endl ;
185  std::cout << "aMb "<<std::endl <<aMb << std::endl ;
186  std::cout << "-------------------------------" <<std::endl ;
187  vpHomography aHb ;
188 
189  vpHomography::HLM(nbpt,xb,yb,xa,ya,true, aHb) ;
190 
191  vpTRACE("aHb computed using the Malis paralax algorithm") ;
192  aHb /= aHb[2][2] ;
193  std::cout << std::endl << aHb<< std::endl ;
194 
195  vpRotationMatrix aRb ;
196  vpTranslationVector aTb ;
197  vpColVector n ;
198 
199  std::cout << "-------------------------------" <<std::endl ;
200  vpTRACE("extract R, T and n ") ;
201  aHb.computeDisplacement(aRb, aTb, n) ;
202  std::cout << "Rotation: aRb" <<std::endl ;
203  std::cout << aRb << std::endl ;
204  std::cout << "Translation: aTb" <<std::endl;
205  std::cout << (aTb).t() <<std::endl ;
206  std::cout << "Normal to the plane: n" <<std::endl;
207  std::cout << (n).t() <<std::endl ;
208 
209 
210  std::cout << "-------------------------------" <<std::endl ;
211  vpTRACE("Compare with built homography H = R + t/d ") ;
212  vpPlane bp(0,0,1,1) ;
213  vpHomography aHb_built(aMb,bp) ;
214  vpTRACE( "aHb built from the displacement ") ;
215  std::cout << std::endl <<aHb_built/aHb_built[2][2] << std::endl ;
216 
217  aHb_built.computeDisplacement(aRb, aTb, n) ;
218  std::cout << "Rotation: aRb" <<std::endl ;
219  std::cout << aRb << std::endl ;
220  std::cout << "Translation: aTb" <<std::endl;
221  std::cout << (aTb).t() <<std::endl ;
222  std::cout << "Normal to the plane: n" <<std::endl;
223  std::cout << (n).t() <<std::endl ;
224 
225  std::cout << "-------------------------------" <<std::endl ;
226  vpTRACE("test if ap = aHb bp") ;
227 
228  for(i=0 ; i < nbpt ; i++)
229  {
230  std::cout << "Point "<< i<< std::endl ;
231  vpPoint p ;
232  std::cout << "(" ;
233  std::cout << aP[i].get_x()/aP[i].get_w()<<", "<< aP[i].get_y()/aP[i].get_w() ;
234  std::cout <<") = (" ;
235  p = aHb*bP[i] ;
236  std::cout << p.get_x() /p.get_w()<<", "<< p.get_y()/ p.get_w() <<")"<<std::endl ;
237  }
238 
239  std::cout << "-------------------------------" <<std::endl ;
240  vpTRACE("test displacement") ;
241 
242  std::list<vpRotationMatrix> laRb ;
243  std::list<vpTranslationVector> laTb ;
244  std::list<vpColVector> lnb ;
245 
246  vpHomography::computeDisplacement(aHb,bP[0].get_x(),bP[0].get_y(),
247  laRb, laTb, lnb) ;
248 
249  std::list<vpRotationMatrix>::const_iterator it_laRb = laRb.begin();
250  std::list<vpTranslationVector>::const_iterator it_laTb = laTb.begin();
251  std::list<vpColVector>::const_iterator it_lnb = lnb.begin();
252 
253  int k =1 ;
254  while (it_lnb != lnb.end())
255  {
256  std::cout << "Solution " << k++ << std::endl ;
257 
258  aRb = *it_laRb;
259  aTb = *it_laTb;
260  n = *it_lnb;
261  std::cout << "Rotation: aRb" <<std::endl ;
262  std::cout << aRb << std::endl ;
263  std::cout << "Translation: aTb" <<std::endl;
264  std::cout << (aTb).t() <<std::endl ;
265  std::cout << "Normal to the plane: n" <<std::endl;
266  std::cout << (n).t() <<std::endl ;
267 
268  ++ it_laRb;
269  ++ it_laTb;
270  ++ it_lnb;
271  }
272 
273 }
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpTRACE
Definition: vpDebug.h:401
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
The vpRotationMatrix considers the particular case of a rotation matrix.
This class aims to compute the homography wrt.two images.
Definition: vpHomography.h:173
static void HLM(unsigned int n, double *xb, double *yb, double *xa, double *ya, bool isplan, vpHomography &aHb)
Computes the homography matrix from planar or non planar points using Ezio Malis linear method (HLM)...
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