Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testPoseRansac.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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  * Compute the pose of a 3D object using the Dementhon method. Assuming that
32  * the correspondance between 2D points and 3D points is not done, we use
33  * the RANSAC algorithm to achieve this task
34  *
35  * Authors:
36  * Aurelien Yol
37  *
38  *****************************************************************************/
39 
40 #include <visp3/vision/vpPose.h>
41 #include <visp3/core/vpPoint.h>
42 #include <visp3/core/vpMath.h>
43 #include <visp3/core/vpHomogeneousMatrix.h>
44 
45 #include <stdlib.h>
46 #include <stdio.h>
47 
48 #define L 0.1
49 
50 
58 int
59 main()
60 {
61  try {
62  std::cout << "Pose computation with matched points" << std::endl;
63  std::vector<vpPoint> P; // Point to be tracked
64 
65  P.push_back( vpPoint(-L,-L, 0 ) );
66  P.push_back( vpPoint(L,-L, 0 ) );
67  P.push_back( vpPoint(L,L, 0 ) );
68  P.push_back( vpPoint(-L,L, 0 ) );
69 
70  double L2 = L*3.0;
71  P.push_back( vpPoint(0,-L2, 0 ) );
72  P.push_back( vpPoint(L2,0, 0 ) );
73  P.push_back( vpPoint(0,L2, 0 ) );
74  P.push_back( vpPoint(-L2,0, 0 ) );
75 
76  vpHomogeneousMatrix cMo_ref(0, 0.2, 1, 0, 0, 0) ;
77  for(size_t i=0 ; i < P.size(); i++)
78  {
79  P[i].project(cMo_ref) ;
80  P[i].print() ;
81  std::cout << std::endl;
82  }
83 
84  //Introduce an error
85  double error = 0.01;
86  P[3].set_y(P[3].get_y() + 2*error);
87  P[6].set_x(P[6].get_x() + error);
88 
89  vpPose pose;
90  for(size_t i=0 ; i < P.size() ; i++)
91  pose.addPoint(P[i]);
92 
93  unsigned int nbInlierToReachConsensus = (unsigned int)(75.0 * (double)(P.size()) / 100.0);
94  double threshold = 0.001;
95 
96  pose.setRansacNbInliersToReachConsensus(nbInlierToReachConsensus);
97  pose.setRansacThreshold(threshold);
98 
100  //vpPose::ransac(lp,lP, 5, 1e-6, ninliers, lPi, cMo) ;
101  pose.computePose(vpPose::RANSAC, cMo);
102 
103  std::vector<vpPoint> inliers = pose.getRansacInliers();
104 
105  std::cout << "Inliers: " << std::endl;
106  for (unsigned int i = 0; i < inliers.size() ; i++)
107  {
108  inliers[i].print() ;
109  std::cout << std::endl;
110  }
111 
112  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
113  vpPoseVector pose_est = vpPoseVector(cMo);
114 
115  std::cout << std::endl;
116  std::cout << "reference cMo :\n" << pose_ref.t() << std::endl << std::endl;
117  std::cout << "estimated cMo :\n" << pose_est.t() << std::endl << std::endl;
118 
119  int test_fail = 0;
120  for(unsigned int i=0; i<6; i++) {
121  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
122  test_fail = 1;
123  }
124 
125  std::cout << "Pose is " << (test_fail ? "badly" : "well") << " estimated" << std::endl;
126  return test_fail;
127  }
128  catch(vpException &e) {
129  std::cout << "Catch an exception: " << e << std::endl;
130  return 1;
131  }
132 }
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:73
void setRansacThreshold(const double &t)
Definition: vpPose.h:227
Class that defines what is a point.
Definition: vpPoint.h:59
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:76
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
Definition: vpPose.cpp:372
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition: vpPose.h:226
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:145
std::vector< vpPoint > getRansacInliers() const
Definition: vpPose.h:238
vpRowVector t() const