ViSP  3.0.0
homographyRansac2DObject.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 Ransac homography estimation algorithm.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
56 #include <visp3/core/vpMath.h>
57 #include <visp3/core/vpRotationMatrix.h>
58 #include <visp3/vision/vpHomography.h>
59 #include <visp3/core/vpDebug.h>
60 #include <visp3/core/vpThetaUVector.h>
61 
62 #include <visp3/core/vpPoint.h>
63 #include <visp3/core/vpMath.h>
64 #include <visp3/core/vpHomogeneousMatrix.h>
65 #include <visp3/core/vpDebug.h>
66 
67 #include <visp3/core/vpRansac.h>
68 #include <visp3/io/vpParseArgv.h>
69 #include <stdlib.h>
70 // List of allowed command line options
71 #define GETOPTARGS "h"
72 
73 void usage(const char *name, const char *badparam);
74 bool getOptions(int argc, const char **argv);
75 
84 void usage(const char *name, const char *badparam)
85 {
86  fprintf(stdout, "\n\
87 Test the Ransac 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 }
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  try {
143  // Read the command line options
144  if (getOptions(argc, argv) == false) {
145  exit (-1);
146  }
147 
148  double L=0.1;
149  unsigned int nbpt = 11;
150 
151  std::vector<vpPoint> P(nbpt); // Point to be tracked
152  std::vector<double> xa(nbpt), ya(nbpt), xb(nbpt), yb(nbpt);
153 
154  std::vector<vpPoint> aP(nbpt); // Point to be tracked
155  std::vector<vpPoint> bP(nbpt); // Point to be tracked
156 
157  P[0].setWorldCoordinates(-L,-L, 0 ) ; // inlier
158  P[1].setWorldCoordinates(2*L,-L, 0 ) ; // inlier
159  P[2].setWorldCoordinates(L,L, 0 ) ; // inlier
160  P[3].setWorldCoordinates(-L,3*L, 0 ) ; // inlier
161  P[4].setWorldCoordinates(0,0, L ) ;
162  P[5].setWorldCoordinates(L,-2*L, L ) ;
163  P[6].setWorldCoordinates(L,-4*L, 2*L ) ;
164  P[7].setWorldCoordinates(-2*L,-L, -3*L ) ;
165  P[8].setWorldCoordinates(-5*L,-5*L, 0 ) ; // inlier
166  P[9].setWorldCoordinates(-2*L,+3*L, 4*L ) ;
167  P[10].setWorldCoordinates(-2*L,-0.5*L, 0 ) ; // inlier
168 
169  std::vector<bool> inliers_ground_truth(nbpt, false);
170  inliers_ground_truth[0] = true;
171  inliers_ground_truth[1] = true;
172  inliers_ground_truth[2] = true;
173  inliers_ground_truth[3] = true;
174  inliers_ground_truth[8] = true;
175  inliers_ground_truth[10] = true;
176 
177  vpHomogeneousMatrix bMo(0,0,1, 0,0,0) ;
178  vpHomogeneousMatrix aMb(0.1,0.1,0.1,vpMath::rad(10),0,vpMath::rad(40)) ;
179  vpHomogeneousMatrix aMo =aMb*bMo ;
180  for(unsigned int i=0 ; i < nbpt ; i++)
181  {
182  P[i].project(aMo) ;
183  aP[i] = P[i] ;
184  xa[i] = P[i].get_x() ;
185  ya[i] = P[i].get_y() ;
186  }
187 
188  for(unsigned int i=0 ; i < nbpt ; i++)
189  {
190  P[i].project(bMo) ;
191  bP[i] = P[i] ;
192  xb[i] = P[i].get_x() ;
193  yb[i] = P[i].get_y() ;
194  }
195  std::cout << "-------------------------------" <<std::endl ;
196 
197  vpRotationMatrix aRb ;
198  vpTranslationVector aTb ;
199  vpColVector n ;
200  std::cout << "Compare with built homography H = R + t/d n " << std::endl;
201  vpPlane bp(0,0,1,1) ;
202  vpHomography aHb_built(aMb,bp) ;
203  std::cout << "aHb built from the displacement: \n" << aHb_built/aHb_built[2][2] << std::endl ;
204 
205  aHb_built.computeDisplacement(aRb, aTb, n) ;
206  std::cout << "Rotation aRb: " <<std::endl ;
207  std::cout << aRb << std::endl ;
208  std::cout << "Translation: aTb" <<std::endl;
209  std::cout << (aTb).t() <<std::endl ;
210  std::cout << "Normal to the plane: n" <<std::endl;
211  std::cout << (n).t() <<std::endl ;
212 
213  std::cout << "-------------------------------" <<std::endl ;
214  vpHomography aHb;
215  std::vector<bool> inliers;
216  double residual;
217  // Suppose px=1000. Set the threshold to 2 pixels => 2/1000
218  // In the data we have 6 inliers. We request that at least 6 are retrieved
219  vpHomography::ransac(xb, yb, xa, ya, aHb, inliers, residual, 6, 2./1000) ;
220 
221  std::cout << "aHb estimated using ransac:\n" << aHb << std::endl ;
222  std::cout << "Inliers indexes (should be 0,1,2,3,8,10): ";
223  for (unsigned int i=0; i< inliers.size(); i++)
224  if (inliers[i]) std::cout << i << ",";
225  std::cout << std::endl;
226 
227  if (inliers == inliers_ground_truth) {
228  std::cout << "Ransac estimation succeed" << std::endl;
229  return 0;
230  }
231  else {
232  std::cout << "Ransac estimation fails" << std::endl;
233  return 1;
234  }
235  }
236  catch(vpException e) {
237  std::cout << "Catch an exception: " << e << std::endl;
238  return 1;
239  }
240 }
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:179
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:104
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.