ViSP  3.0.0
homographyHartleyDLT2DObject.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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  * Example of the HartleyDLT homography estimation algorithm.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
54 #include <visp3/core/vpMath.h>
55 #include <visp3/core/vpRotationMatrix.h>
56 #include <visp3/vision/vpHomography.h>
57 #include <visp3/core/vpDebug.h>
58 #include <visp3/core/vpThetaUVector.h>
59 
60 #include <visp3/core/vpPoint.h>
61 #include <visp3/core/vpMath.h>
62 #include <visp3/core/vpHomogeneousMatrix.h>
63 #include <visp3/core/vpDebug.h>
64 #include <visp3/io/vpParseArgv.h>
65 #include <stdlib.h>
66 // List of allowed command line options
67 #define GETOPTARGS "h"
68 
69 #define L 0.1
70 #define nbpt 5
71 
72 void usage(const char *name, const char *badparam);
73 bool getOptions(int argc, const char **argv);
74 
84 void usage(const char *name, const char *badparam)
85 {
86  fprintf(stdout, "\n\
87 Test the HartleyDLT homography estimation algorithm.\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 }
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  try {
144  // Read the command line options
145  if (getOptions(argc, argv) == false) {
146  exit (-1);
147  }
148 
149  vpPoint P[nbpt] ; // Point to be tracked
150  std::vector<double> xa(nbpt), ya(nbpt), xb(nbpt), yb(nbpt);
151 
152  vpPoint aP[nbpt] ; // Point to be tracked
153  vpPoint bP[nbpt] ; // Point to be tracked
154 
155  P[0].setWorldCoordinates(-L,-L, 0 ) ;
156  P[1].setWorldCoordinates(2*L,-L, 0 ) ;
157  P[2].setWorldCoordinates(L,L, 0 ) ;
158  P[3].setWorldCoordinates(-L,3*L, 0 ) ;
159  P[4].setWorldCoordinates(0,0, 0 ) ;
160  /*
161  P[5].setWorldCoordinates(10,20, 0 ) ;
162  P[6].setWorldCoordinates(-10,12, 0 ) ;
163  */
164  vpHomogeneousMatrix bMo(0,0,1, 0,0,0) ;
165  vpHomogeneousMatrix aMb(1,0,0.0,vpMath::rad(10),0,vpMath::rad(40)) ;
166  vpHomogeneousMatrix aMo =aMb*bMo ;
167  for(unsigned int i=0 ; i < nbpt ; i++)
168  {
169  P[i].project(aMo) ;
170  aP[i] = P[i] ;
171  xa[i] = P[i].get_x() ;
172  ya[i] = P[i].get_y() ;
173  }
174 
175  for(unsigned int i=0 ; i < nbpt ; i++)
176  {
177  P[i].project(bMo) ;
178  bP[i] = P[i] ;
179  xb[i] = P[i].get_x() ;
180  yb[i] = P[i].get_y() ;
181  }
182  std::cout << "-------------------------------" <<std::endl ;
183  std::cout << "aMb "<<std::endl <<aMb << std::endl ;
184  std::cout << "-------------------------------" <<std::endl ;
185  vpHomography aHb ;
186 
187  vpHomography::DLT(xb, yb, xa, ya, aHb, true) ;
188 
189  vpTRACE("aHb computed using the DLT algorithm") ;
190  aHb /= aHb[2][2] ;
191  std::cout << std::endl << aHb<< std::endl ;
192 
193  vpRotationMatrix aRb ;
194  vpTranslationVector aTb ;
195  vpColVector n ;
196 
197  std::cout << "-------------------------------" <<std::endl ;
198  vpTRACE("extract R, T and n ") ;
199  aHb.computeDisplacement(aRb, aTb, n) ;
200  std::cout << "Rotation: aRb" <<std::endl ;
201  std::cout << aRb << std::endl ;
202  std::cout << "Translation: aTb" <<std::endl;
203  std::cout << (aTb).t() <<std::endl ;
204  std::cout << "Normal to the plane: n" <<std::endl;
205  std::cout << (n).t() <<std::endl ;
206 
207  std::cout << "-------------------------------" <<std::endl ;
208  vpTRACE("Compare with built homoraphy H = R + t/d ") ;
209  vpPlane bp(0,0,1,1) ;
210  vpHomography aHb_built(aMb,bp) ;
211  vpTRACE( "aHb built from the displacement ") ;
212  std::cout << std::endl <<aHb_built/aHb_built[2][2] << std::endl ;
213 
214  aHb_built.computeDisplacement(aRb, aTb, n) ;
215  std::cout << "Rotation: aRb" <<std::endl ;
216  std::cout << aRb << std::endl ;
217  std::cout << "Translation: aTb" <<std::endl;
218  std::cout << (aTb).t() <<std::endl ;
219  std::cout << "Normal to the plane: n" <<std::endl;
220  std::cout << (n).t() <<std::endl ;
221 
222  std::cout << "-------------------------------" <<std::endl ;
223  vpTRACE("test if ap = aHb bp") ;
224 
225  for(unsigned int i=0 ; i < nbpt ; i++)
226  {
227  std::cout << "Point "<< i<< std::endl ;
228  vpPoint p ;
229  std::cout << "(" ;
230  std::cout << aP[i].get_x()/aP[i].get_w()<<", "<< aP[i].get_y()/aP[i].get_w() ;
231  std::cout <<") = (" ;
232  p = aHb*bP[i] ;
233  std::cout << p.get_x() /p.get_w()<<", "<< p.get_y()/ p.get_w() <<")"<<std::endl ;
234  }
235  return 0;
236  }
237  catch(vpException e) {
238  std::cout << "Catch an exception: " << e << std::endl;
239  return 1;
240  }
241 }
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:73
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:458
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.cpp:460
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
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:179
#define vpTRACE
Definition: vpDebug.h:414
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:456
static double rad(double deg)
Definition: vpMath.h:104
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)
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
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:58
Class that consider the case of a translation vector.