ViSP  2.8.0
homographyRansac2DObject.cpp
1 /****************************************************************************
2  *
3  * $Id: homographyRansac2DObject.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 #define L 0.1
78 #define nbpt 11
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  // Read the command line options
147  if (getOptions(argc, argv) == false) {
148  exit (-1);
149  }
150 
151  int i ;
152 
153  vpPoint P[nbpt] ; // Point to be tracked
154  double xa[nbpt], ya[nbpt] ;
155  double xb[nbpt], yb[nbpt] ;
156 
157  vpPoint aP[nbpt] ; // Point to be tracked
158  vpPoint bP[nbpt] ; // Point to be tracked
159 
160  P[0].setWorldCoordinates(-L,-L, 0 ) ; // inlier
161  P[1].setWorldCoordinates(2*L,-L, 0 ) ; // inlier
162  P[2].setWorldCoordinates(L,L, 0 ) ; // inlier
163  P[3].setWorldCoordinates(-L,3*L, 0 ) ; // inlier
164  P[4].setWorldCoordinates(0,0, L ) ;
165  P[5].setWorldCoordinates(L,-2*L, L ) ;
166  P[6].setWorldCoordinates(L,-4*L, 2*L ) ;
167  P[7].setWorldCoordinates(-2*L,-L, -3*L ) ;
168  P[8].setWorldCoordinates(-5*L,-5*L, 0 ) ; // inlier
169  P[9].setWorldCoordinates(-2*L,+3*L, 4*L ) ;
170  P[10].setWorldCoordinates(-2*L,-0.5*L, 0 ) ;
171  /*
172  P[5].setWorldCoordinates(10,20, 0 ) ;
173  P[6].setWorldCoordinates(-10,12, 0 ) ;
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(i=0 ; i < nbpt ; i++)
179  {
180  P[i].project(aMo) ;
181  aP[i] = P[i] ;
182  xa[i] = P[i].get_x() ;
183  ya[i] = P[i].get_y() ;
184  }
185 
186  for(i=0 ; i < nbpt ; i++)
187  {
188  P[i].project(bMo) ;
189  bP[i] = P[i] ;
190  xb[i] = P[i].get_x() ;
191  yb[i] = P[i].get_y() ;
192  }
193  std::cout << "-------------------------------" <<std::endl ;
194 
195  vpRotationMatrix aRb ;
196  vpTranslationVector aTb ;
197  vpColVector n ;
198  vpTRACE("Compare with built homography H = R + t/d n ") ;
199  vpPlane bp(0,0,1,1) ;
200  vpHomography aHb_built(aMb,bp) ;
201  vpTRACE( "aHb built from the displacement ") ;
202  std::cout << std::endl <<aHb_built/aHb_built[2][2] << std::endl ;
203 
204  aHb_built.computeDisplacement(aRb, aTb, n) ;
205  std::cout << "Rotation aRb: " <<std::endl ;
206  std::cout << aRb << std::endl ;
207  std::cout << "Translation: aTb" <<std::endl;
208  std::cout << (aTb).t() <<std::endl ;
209  std::cout << "Normal to the plane: n" <<std::endl;
210  std::cout << (n).t() <<std::endl ;
211 
212  std::cout << "-------------------------------" <<std::endl ;
213  vpTRACE(" ") ;
214  vpHomography aHb ;
215  vpHomography::ransac(nbpt,xb,yb,xa,ya, aHb) ;
216 
217  std::cout << aHb << std::endl ;
218 }
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpTRACE
Definition: vpDebug.h:401
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.h:138
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:173
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
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
static bool ransac(unsigned int n, double *xb, double *yb, double *xa, double *ya, vpHomography &aHb, int consensus=1000, double threshold=1e-6)
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