ViSP  2.9.0
homographyHartleyDLT2DObject.cpp
1 /****************************************************************************
2  *
3  * $Id: homographyHartleyDLT2DObject.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  * Example of the HartleyDLT homography estimation algorithm.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
58 #include <visp/vpMath.h>
59 #include <visp/vpRotationMatrix.h>
60 #include <visp/vpHomography.h>
61 #include <visp/vpDebug.h>
62 #include <visp/vpThetaUVector.h>
63 
64 #include <visp/vpPoint.h>
65 #include <visp/vpMath.h>
66 #include <visp/vpHomogeneousMatrix.h>
67 #include <visp/vpDebug.h>
68 #include <visp/vpParseArgv.h>
69 #include <stdlib.h>
70 // List of allowed command line options
71 #define GETOPTARGS "h"
72 
73 #define L 0.1
74 #define nbpt 5
75 
76 void usage(const char *name, const char *badparam);
77 bool getOptions(int argc, const char **argv);
78 
88 void usage(const char *name, const char *badparam)
89 {
90  fprintf(stdout, "\n\
91 Test the HartleyDLT homography estimation algorithm.\n\
92 \n\
93 SYNOPSIS\n\
94  %s [-h]\n", name);
95 
96  fprintf(stdout, "\n\
97 OPTIONS: Default\n\
98  -h\n\
99  Print the help.\n");
100 
101  if (badparam) {
102  fprintf(stderr, "ERROR: \n" );
103  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
104  }
105 }
117 bool getOptions(int argc, const char **argv)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'h': usage(argv[0], NULL); return false; break;
125 
126  default:
127  usage(argv[0], optarg_);
128  return false; break;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], NULL);
135  std::cerr << "ERROR: " << std::endl;
136  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137  return false;
138  }
139 
140  return true;
141 }
142 
143 
144 int
145 main(int argc, const char ** argv)
146 {
147  try {
148  // Read the command line options
149  if (getOptions(argc, argv) == false) {
150  exit (-1);
151  }
152 
153  vpPoint P[nbpt] ; // Point to be tracked
154  std::vector<double> xa(nbpt), ya(nbpt), xb(nbpt), yb(nbpt);
155 
156  vpPoint aP[nbpt] ; // Point to be tracked
157  vpPoint bP[nbpt] ; // Point to be tracked
158 
159  P[0].setWorldCoordinates(-L,-L, 0 ) ;
160  P[1].setWorldCoordinates(2*L,-L, 0 ) ;
161  P[2].setWorldCoordinates(L,L, 0 ) ;
162  P[3].setWorldCoordinates(-L,3*L, 0 ) ;
163  P[4].setWorldCoordinates(0,0, 0 ) ;
164  /*
165  P[5].setWorldCoordinates(10,20, 0 ) ;
166  P[6].setWorldCoordinates(-10,12, 0 ) ;
167  */
168  vpHomogeneousMatrix bMo(0,0,1, 0,0,0) ;
169  vpHomogeneousMatrix aMb(1,0,0.0,vpMath::rad(10),0,vpMath::rad(40)) ;
170  vpHomogeneousMatrix aMo =aMb*bMo ;
171  for(unsigned int i=0 ; i < nbpt ; i++)
172  {
173  P[i].project(aMo) ;
174  aP[i] = P[i] ;
175  xa[i] = P[i].get_x() ;
176  ya[i] = P[i].get_y() ;
177  }
178 
179  for(unsigned int i=0 ; i < nbpt ; i++)
180  {
181  P[i].project(bMo) ;
182  bP[i] = P[i] ;
183  xb[i] = P[i].get_x() ;
184  yb[i] = P[i].get_y() ;
185  }
186  std::cout << "-------------------------------" <<std::endl ;
187  std::cout << "aMb "<<std::endl <<aMb << std::endl ;
188  std::cout << "-------------------------------" <<std::endl ;
189  vpHomography aHb ;
190 
191  vpHomography::DLT(xb, yb, xa, ya, aHb, true) ;
192 
193  vpTRACE("aHb computed using the DLT algorithm") ;
194  aHb /= aHb[2][2] ;
195  std::cout << std::endl << aHb<< std::endl ;
196 
197  vpRotationMatrix aRb ;
198  vpTranslationVector aTb ;
199  vpColVector n ;
200 
201  std::cout << "-------------------------------" <<std::endl ;
202  vpTRACE("extract R, T and n ") ;
203  aHb.computeDisplacement(aRb, aTb, n) ;
204  std::cout << "Rotation: aRb" <<std::endl ;
205  std::cout << aRb << std::endl ;
206  std::cout << "Translation: aTb" <<std::endl;
207  std::cout << (aTb).t() <<std::endl ;
208  std::cout << "Normal to the plane: n" <<std::endl;
209  std::cout << (n).t() <<std::endl ;
210 
211  std::cout << "-------------------------------" <<std::endl ;
212  vpTRACE("Compare with built homoraphy H = R + t/d ") ;
213  vpPlane bp(0,0,1,1) ;
214  vpHomography aHb_built(aMb,bp) ;
215  vpTRACE( "aHb built from the displacement ") ;
216  std::cout << std::endl <<aHb_built/aHb_built[2][2] << std::endl ;
217 
218  aHb_built.computeDisplacement(aRb, aTb, n) ;
219  std::cout << "Rotation: aRb" <<std::endl ;
220  std::cout << aRb << std::endl ;
221  std::cout << "Translation: aTb" <<std::endl;
222  std::cout << (aTb).t() <<std::endl ;
223  std::cout << "Normal to the plane: n" <<std::endl;
224  std::cout << (n).t() <<std::endl ;
225 
226  std::cout << "-------------------------------" <<std::endl ;
227  vpTRACE("test if ap = aHb bp") ;
228 
229  for(unsigned int i=0 ; i < nbpt ; i++)
230  {
231  std::cout << "Point "<< i<< std::endl ;
232  vpPoint p ;
233  std::cout << "(" ;
234  std::cout << aP[i].get_x()/aP[i].get_w()<<", "<< aP[i].get_y()/aP[i].get_w() ;
235  std::cout <<") = (" ;
236  p = aHb*bP[i] ;
237  std::cout << p.get_x() /p.get_w()<<", "<< p.get_y()/ p.get_w() <<")"<<std::endl ;
238  }
239  return 0;
240  }
241  catch(vpException e) {
242  std::cout << "Catch an exception: " << e << std::endl;
243  return 1;
244  }
245 }
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpTRACE
Definition: vpDebug.h:418
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
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
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)
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