ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testPoseRansac.cpp
1 /****************************************************************************
2  *
3  * $Id: testPoseRansac.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  * Compute the pose of a 3D object using the Dementhon method. Assuming that
36  * the correspondance between 2D points and 3D points is not done, we use
37  * the RANSAC algorithm to achieve this task
38  *
39  * Authors:
40  * Aurelien Yol
41  *
42  *****************************************************************************/
43 
44 #include <visp/vpPose.h>
45 #include <visp/vpPoint.h>
46 #include <visp/vpMath.h>
47 #include <visp/vpHomogeneousMatrix.h>
48 
49 #include <stdlib.h>
50 #include <stdio.h>
51 
52 #define L 0.1
53 
54 
62 int
63 main()
64 {
65  std::cout << "Pose computation with matched points" << std::endl;
66  int size = 8;
67  vpPoint *P = new vpPoint [size] ; // Point to be tracked
68 
69  P[0].setWorldCoordinates(-L,-L, 0 ) ;
70  P[1].setWorldCoordinates(L,-L, 0 ) ;
71  P[2].setWorldCoordinates(L,L, 0 ) ;
72  P[3].setWorldCoordinates(-L,L, 0 ) ;
73 
74  double L2 = L*3.0;
75  P[4].setWorldCoordinates(0,-L2, 0 ) ;
76  P[5].setWorldCoordinates(L2,0, 0 ) ;
77  P[6].setWorldCoordinates(0,L2, 0 ) ;
78  P[7].setWorldCoordinates(-L2,0, 0 ) ;
79 
80  // P[4].setWorldCoordinates(-0,0, L ) ;
81 
82  vpHomogeneousMatrix cMo_ref(0, 0.2, 1, 0, 0, 0) ;
83  for(int i=0 ; i < size ; i++)
84  {
85  P[i].project(cMo_ref) ;
86  P[i].print() ;
87  std::cout << std::endl;
88  }
89 
90  //Introduce an error
91  double error = 0.01;
92  P[3].set_y(P[3].get_y() + 2*error);
93  P[6].set_x(P[6].get_x() + error);
94 
95  vpPose pose;
96  for(int i=0 ; i < size ; i++)
97  pose.addPoint(P[i]);
98 
99  unsigned int nbInlierToReachConsensus = (unsigned int)(75.0 * (double)size / 100.0);
100  double threshold = 0.001;
101 
102  pose.setRansacNbInliersToReachConsensus(nbInlierToReachConsensus);
103  pose.setRansacThreshold(threshold);
104 
105  vpHomogeneousMatrix cMo ;
106  //vpPose::ransac(lp,lP, 5, 1e-6, ninliers, lPi, cMo) ;
107  pose.computePose(vpPose::RANSAC, cMo);
108 
109  std::vector<vpPoint> inliers = pose.getRansacInliers();
110 
111  std::cout << "Inliers: " << std::endl;
112  for (unsigned int i = 0; i < inliers.size() ; i++)
113  {
114  inliers[i].print() ;
115  std::cout << std::endl;
116  }
117 
118  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
119  vpPoseVector pose_est = vpPoseVector(cMo);
120 
121  std::cout << std::endl;
122  std::cout << "reference cMo :\n" << pose_ref.t() << std::endl << std::endl;
123  std::cout << "estimated cMo :\n" << pose_est.t() << std::endl << std::endl;
124 
125  int test_fail = 0;
126  for(unsigned int i=0; i<6; i++) {
127  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
128  test_fail = 1;
129  }
130 
131  std::cout << "Pose is " << (test_fail ? "badly" : "well") << " estimated" << std::endl;
132  delete [] P;
133  return test_fail;
134 }