ViSP  2.9.0
homographyRansac2DObject.cpp
1 /****************************************************************************
2  *
3  * $Id: homographyRansac2DObject.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 Ransac homography estimation algorithm.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
60 #include <visp/vpMath.h>
61 #include <visp/vpRotationMatrix.h>
62 #include <visp/vpHomography.h>
63 #include <visp/vpDebug.h>
64 #include <visp/vpThetaUVector.h>
65 
66 #include <visp/vpPoint.h>
67 #include <visp/vpMath.h>
68 #include <visp/vpHomogeneousMatrix.h>
69 #include <visp/vpDebug.h>
70 
71 #include <visp/vpRansac.h>
72 #include <visp/vpParseArgv.h>
73 #include <stdlib.h>
74 // List of allowed command line options
75 #define GETOPTARGS "h"
76 
77 void usage(const char *name, const char *badparam);
78 bool getOptions(int argc, const char **argv);
79 
88 void usage(const char *name, const char *badparam)
89 {
90  fprintf(stdout, "\n\
91 Test the Ransac 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 }
116 bool getOptions(int argc, const char **argv)
117 {
118  const char *optarg_;
119  int c;
120  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
121 
122  switch (c) {
123  case 'h': usage(argv[0], NULL); return false; break;
124 
125  default:
126  usage(argv[0], optarg_);
127  return false; break;
128  }
129  }
130 
131  if ((c == 1) || (c == -1)) {
132  // standalone param or error
133  usage(argv[0], NULL);
134  std::cerr << "ERROR: " << std::endl;
135  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136  return false;
137  }
138 
139  return true;
140 }
141 
142 
143 int
144 main(int argc, const char ** argv)
145 {
146  try {
147  // Read the command line options
148  if (getOptions(argc, argv) == false) {
149  exit (-1);
150  }
151 
152  double L=0.1;
153  unsigned int nbpt = 11;
154 
155  std::vector<vpPoint> P(nbpt); // Point to be tracked
156  std::vector<double> xa(nbpt), ya(nbpt), xb(nbpt), yb(nbpt);
157 
158  std::vector<vpPoint> aP(nbpt); // Point to be tracked
159  std::vector<vpPoint> bP(nbpt); // Point to be tracked
160 
161  P[0].setWorldCoordinates(-L,-L, 0 ) ; // inlier
162  P[1].setWorldCoordinates(2*L,-L, 0 ) ; // inlier
163  P[2].setWorldCoordinates(L,L, 0 ) ; // inlier
164  P[3].setWorldCoordinates(-L,3*L, 0 ) ; // inlier
165  P[4].setWorldCoordinates(0,0, L ) ;
166  P[5].setWorldCoordinates(L,-2*L, L ) ;
167  P[6].setWorldCoordinates(L,-4*L, 2*L ) ;
168  P[7].setWorldCoordinates(-2*L,-L, -3*L ) ;
169  P[8].setWorldCoordinates(-5*L,-5*L, 0 ) ; // inlier
170  P[9].setWorldCoordinates(-2*L,+3*L, 4*L ) ;
171  P[10].setWorldCoordinates(-2*L,-0.5*L, 0 ) ; // inlier
172 
173  std::vector<bool> inliers_ground_truth(nbpt, false);
174  inliers_ground_truth[0] = true;
175  inliers_ground_truth[1] = true;
176  inliers_ground_truth[2] = true;
177  inliers_ground_truth[3] = true;
178  inliers_ground_truth[8] = true;
179  inliers_ground_truth[10] = true;
180 
181  vpHomogeneousMatrix bMo(0,0,1, 0,0,0) ;
182  vpHomogeneousMatrix aMb(0.1,0.1,0.1,vpMath::rad(10),0,vpMath::rad(40)) ;
183  vpHomogeneousMatrix aMo =aMb*bMo ;
184  for(unsigned int i=0 ; i < nbpt ; i++)
185  {
186  P[i].project(aMo) ;
187  aP[i] = P[i] ;
188  xa[i] = P[i].get_x() ;
189  ya[i] = P[i].get_y() ;
190  }
191 
192  for(unsigned int i=0 ; i < nbpt ; i++)
193  {
194  P[i].project(bMo) ;
195  bP[i] = P[i] ;
196  xb[i] = P[i].get_x() ;
197  yb[i] = P[i].get_y() ;
198  }
199  std::cout << "-------------------------------" <<std::endl ;
200 
201  vpRotationMatrix aRb ;
202  vpTranslationVector aTb ;
203  vpColVector n ;
204  std::cout << "Compare with built homography H = R + t/d n " << std::endl;
205  vpPlane bp(0,0,1,1) ;
206  vpHomography aHb_built(aMb,bp) ;
207  std::cout << "aHb built from the displacement: \n" << aHb_built/aHb_built[2][2] << std::endl ;
208 
209  aHb_built.computeDisplacement(aRb, aTb, n) ;
210  std::cout << "Rotation aRb: " <<std::endl ;
211  std::cout << aRb << std::endl ;
212  std::cout << "Translation: aTb" <<std::endl;
213  std::cout << (aTb).t() <<std::endl ;
214  std::cout << "Normal to the plane: n" <<std::endl;
215  std::cout << (n).t() <<std::endl ;
216 
217  std::cout << "-------------------------------" <<std::endl ;
218  vpHomography aHb;
219  std::vector<bool> inliers;
220  double residual;
221  // Suppose px=1000. Set the threshold to 2 pixels => 2/1000
222  // In the data we have 6 inliers. We request that at least 6 are retrieved
223  vpHomography::ransac(xb, yb, xa, ya, aHb, inliers, residual, 6, 2./1000) ;
224 
225  std::cout << "aHb estimated using ransac:\n" << aHb << std::endl ;
226  std::cout << "Inliers indexes (should be 0,1,2,3,8,10): ";
227  for (unsigned int i=0; i< inliers.size(); i++)
228  if (inliers[i]) std::cout << i << ",";
229  std::cout << std::endl;
230 
231  if (inliers == inliers_ground_truth) {
232  std::cout << "Ransac estimation succeed" << std::endl;
233  return 0;
234  }
235  else {
236  std::cout << "Ransac estimation fails" << std::endl;
237  return 1;
238  }
239  }
240  catch(vpException e) {
241  std::cout << "Catch an exception: " << e << std::endl;
242  return 1;
243  }
244 }
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
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
The vpRotationMatrix considers the particular case of a rotation matrix.
This class aims to compute the homography wrt.two images.
Definition: vpHomography.h:178
static bool ransac(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, vpHomography &aHb, std::vector< bool > &inliers, double &residual, unsigned int nbInliersConsensus, double threshold, bool normalization=true)
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.