Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
homographyRansac2DObject.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Example of the Ransac homography estimation algorithm.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
55 #include <visp3/core/vpDebug.h>
56 #include <visp3/core/vpMath.h>
57 #include <visp3/core/vpRotationMatrix.h>
58 #include <visp3/core/vpThetaUVector.h>
59 #include <visp3/vision/vpHomography.h>
60 
61 #include <visp3/core/vpDebug.h>
62 #include <visp3/core/vpHomogeneousMatrix.h>
63 #include <visp3/core/vpMath.h>
64 #include <visp3/core/vpPoint.h>
65 
66 #include <stdlib.h>
67 #include <visp3/core/vpRansac.h>
68 #include <visp3/io/vpParseArgv.h>
69 // List of allowed command line options
70 #define GETOPTARGS "h"
71 
72 void usage(const char *name, const char *badparam);
73 bool getOptions(int argc, const char **argv);
74 
83 void usage(const char *name, const char *badparam)
84 {
85  fprintf(stdout, "\n\
86 Test the Ransac homography estimation algorithm.\n\
87 \n\
88 SYNOPSIS\n\
89  %s [-h]\n", name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93  -h\n\
94  Print the help.\n");
95 
96  if (badparam) {
97  fprintf(stderr, "ERROR: \n");
98  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
99  }
100 }
111 bool getOptions(int argc, const char **argv)
112 {
113  const char *optarg_;
114  int c;
115  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 
117  switch (c) {
118  case 'h':
119  usage(argv[0], NULL);
120  return false;
121  break;
122 
123  default:
124  usage(argv[0], optarg_);
125  return false;
126  break;
127  }
128  }
129 
130  if ((c == 1) || (c == -1)) {
131  // standalone param or error
132  usage(argv[0], NULL);
133  std::cerr << "ERROR: " << std::endl;
134  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
135  return false;
136  }
137 
138  return true;
139 }
140 
141 int 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  double L = 0.1;
150  unsigned int nbpt = 11;
151 
152  std::vector<vpPoint> P(nbpt); // Point to be tracked
153  std::vector<double> xa(nbpt), ya(nbpt), xb(nbpt), yb(nbpt);
154 
155  P[0].setWorldCoordinates(-L, -L, 0); // inlier
156  P[1].setWorldCoordinates(2 * L, -L, 0); // inlier
157  P[2].setWorldCoordinates(L, L, 0); // inlier
158  P[3].setWorldCoordinates(-L, 3 * L, 0); // inlier
159  P[4].setWorldCoordinates(0, 0, L);
160  P[5].setWorldCoordinates(L, -2 * L, L);
161  P[6].setWorldCoordinates(L, -4 * L, 2 * L);
162  P[7].setWorldCoordinates(-2 * L, -L, -3 * L);
163  P[8].setWorldCoordinates(-5 * L, -5 * L, 0); // inlier
164  P[9].setWorldCoordinates(-2 * L, +3 * L, 4 * L);
165  P[10].setWorldCoordinates(-2 * L, -0.5 * L, 0); // inlier
166 
167  std::vector<bool> inliers_ground_truth(nbpt, false);
168  inliers_ground_truth[0] = true;
169  inliers_ground_truth[1] = true;
170  inliers_ground_truth[2] = true;
171  inliers_ground_truth[3] = true;
172  inliers_ground_truth[8] = true;
173  inliers_ground_truth[10] = true;
174 
175  vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
176  vpHomogeneousMatrix aMb(0.1, 0.1, 0.1, vpMath::rad(10), 0, vpMath::rad(40));
177  vpHomogeneousMatrix aMo = aMb * bMo;
178  for (unsigned int i = 0; i < nbpt; i++) {
179  P[i].project(aMo);
180  xa[i] = P[i].get_x();
181  ya[i] = P[i].get_y();
182  }
183 
184  for (unsigned int i = 0; i < nbpt; i++) {
185  P[i].project(bMo);
186  xb[i] = P[i].get_x();
187  yb[i] = P[i].get_y();
188  }
189  std::cout << "-------------------------------" << std::endl;
190 
191  vpRotationMatrix aRb;
193  vpColVector n;
194  std::cout << "Compare with built homography H = R + t/d n " << std::endl;
195  vpPlane bp(0, 0, 1, 1);
196  vpHomography aHb_built(aMb, bp);
197  std::cout << "aHb built from the displacement: \n" << aHb_built / aHb_built[2][2] << std::endl;
198 
199  aHb_built.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  vpHomography aHb;
209  std::vector<bool> inliers;
210  double residual;
211  // Suppose px=1000. Set the threshold to 2 pixels => 2/1000
212  // In the data we have 6 inliers. We request that at least 6 are retrieved
213  vpHomography::ransac(xb, yb, xa, ya, aHb, inliers, residual, 6, 2. / 1000);
214 
215  std::cout << "aHb estimated using ransac:\n" << aHb << std::endl;
216  std::cout << "Inliers indexes (should be 0,1,2,3,8,10): ";
217  for (unsigned int i = 0; i < inliers.size(); i++)
218  if (inliers[i])
219  std::cout << i << ",";
220  std::cout << std::endl;
221 
222  if (inliers == inliers_ground_truth) {
223  std::cout << "Ransac estimation succeed" << std::endl;
224  return EXIT_SUCCESS;
225  } else {
226  std::cout << "Ransac estimation fails" << std::endl;
227  return EXIT_FAILURE;
228  }
229  } catch (const vpException &e) {
230  std::cout << "Catch an exception: " << e << std::endl;
231  return EXIT_FAILURE;
232  }
233 }
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:71
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
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:102
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.