ViSP  2.8.0
homographyHLM3DObject.cpp
1 /****************************************************************************
2  *
3  * $Id: homographyHLM3DObject.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  * 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 
84 void usage(const char *name, const char *badparam)
85 {
86  fprintf(stdout, "\n\
87 Test the HLM (Malis) homography estimation algorithm with a 3D object.\n\
88 \n\
89 SYNOPSIS\n\
90  %s [-h]\n", name);
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  -h\n\
95  Print the help.\n");
96 
97  if (badparam) {
98  fprintf(stderr, "ERROR: \n" );
99  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
100  }
101 }
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': usage(argv[0], NULL); return false; break;
120 
121  default:
122  usage(argv[0], optarg);
123  return false; break;
124  }
125  }
126 
127  if ((c == 1) || (c == -1)) {
128  // standalone param or error
129  usage(argv[0], NULL);
130  std::cerr << "ERROR: " << std::endl;
131  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
132  return false;
133  }
134 
135  return true;
136 }
137 
138 
139 int
140 main(int argc, const char ** argv)
141 {
142  // Read the command line options
143  if (getOptions(argc, argv) == false) {
144  exit (-1);
145  }
146 
147  int i ;
148 
149  vpPoint P[nbpt] ; // Point to be tracked
150  double xa[nbpt], ya[nbpt] ;
151  double 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, L ) ;
161  P[5].setWorldCoordinates(L,-2*L, L ) ;
162  P[6].setWorldCoordinates(L,-4*L, 2*L ) ;
163  P[7].setWorldCoordinates(-2*L,-L, -L ) ;
164  P[8].setWorldCoordinates(-5*L,-5*L, L ) ;
165  P[9].setWorldCoordinates(-2*L,+3*L, 2*L ) ;
166  P[10].setWorldCoordinates(-2*L,-0.5*L, 2*L ) ;
167  /*
168  P[5].setWorldCoordinates(10,20, 0 ) ;
169  P[6].setWorldCoordinates(-10,12, 0 ) ;
170  */
171  vpHomogeneousMatrix bMo(0,0,1, 0,0,0) ;
172  vpHomogeneousMatrix aMb(0.1,0.1,0.1,vpMath::rad(10),0,vpMath::rad(40)) ;
173  vpHomogeneousMatrix aMo =aMb*bMo ;
174  for(i=0 ; i < nbpt ; i++)
175  {
176  P[i].project(aMo) ;
177  aP[i] = P[i] ;
178  xa[i] = P[i].get_x() ;
179  ya[i] = P[i].get_y() ;
180  }
181 
182  for(i=0 ; i < nbpt ; i++)
183  {
184  P[i].project(bMo) ;
185  bP[i] = P[i] ;
186  xb[i] = P[i].get_x() ;
187  yb[i] = P[i].get_y() ;
188  }
189 
190  vpRotationMatrix aRb ;
191  vpTranslationVector aTb ;
192  vpColVector n ;
193  std::cout << "-------------------------------" <<std::endl ;
194  vpTRACE("Compare with built homography H = R + t/d n ") ;
195  vpPlane bp(0,0,1,1) ;
196  vpHomography aHb_built(aMb,bp) ;
197  vpTRACE( "aHb built from the displacement ") ;
198  std::cout << std::endl <<aHb_built/aHb_built[2][2] << std::endl ;
199 
200  aHb_built.computeDisplacement(aRb, aTb, n) ;
201  std::cout << "Rotation: aRb" <<std::endl ;
202  std::cout << aRb << std::endl ;
203  std::cout << "Translation: aTb" <<std::endl;
204  std::cout << (aTb).t() <<std::endl ;
205  std::cout << "Normal to the plane: n" <<std::endl;
206  std::cout << (n).t() <<std::endl ;
207 
208  std::cout << "-------------------------------" <<std::endl ;
209  std::cout << "aMb "<<std::endl <<aMb << std::endl ;
210  std::cout << "-------------------------------" <<std::endl ;
211  vpHomography aHb ;
212 
213  vpHomography::HLM(nbpt,xb,yb,xa,ya,false, aHb) ;
214 
215  vpTRACE("aHb computed using the Malis paralax algorithm") ;
216  aHb /= aHb[2][2] ;
217  std::cout << std::endl << aHb<< std::endl ;
218 
219 
220  std::cout << "-------------------------------" <<std::endl ;
221  vpTRACE("extract R, T and n ") ;
222  aHb.computeDisplacement(aRb, aTb, n) ;
223  std::cout << "Rotation: aRb" <<std::endl ;
224  std::cout << aRb << std::endl ;
225  std::cout << "Translation: aTb" <<std::endl;
226  std::cout << (aTb).t() <<std::endl ;
227  std::cout << "Normal to the plane: n" <<std::endl;
228  std::cout << (n).t() <<std::endl ;
229 
230 
231  std::cout << "-------------------------------" <<std::endl ;
232  vpTRACE("test if ap = aHb bp") ;
233 
234  for(i=0 ; i < nbpt ; i++)
235  {
236  std::cout << "Point "<< i<< std::endl ;
237  vpPoint p ;
238  std::cout << "(" ;
239  std::cout << aP[i].get_x()/aP[i].get_w()<<", "<< aP[i].get_y()/aP[i].get_w() ;
240  std::cout <<") = (" ;
241  p = aHb*bP[i] ;
242  std::cout << p.get_x() /p.get_w()<<", "<< p.get_y()/ p.get_w() <<")"<<std::endl ;
243  }
244 }
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