ViSP  2.9.0
testPoseRansac.cpp
1 /****************************************************************************
2  *
3  * $Id: testPoseRansac.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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  try {
66  std::cout << "Pose computation with matched points" << std::endl;
67  int size = 8;
68  vpPoint *P = new vpPoint [size] ; // Point to be tracked
69 
70  P[0].setWorldCoordinates(-L,-L, 0 ) ;
71  P[1].setWorldCoordinates(L,-L, 0 ) ;
72  P[2].setWorldCoordinates(L,L, 0 ) ;
73  P[3].setWorldCoordinates(-L,L, 0 ) ;
74 
75  double L2 = L*3.0;
76  P[4].setWorldCoordinates(0,-L2, 0 ) ;
77  P[5].setWorldCoordinates(L2,0, 0 ) ;
78  P[6].setWorldCoordinates(0,L2, 0 ) ;
79  P[7].setWorldCoordinates(-L2,0, 0 ) ;
80 
81  // P[4].setWorldCoordinates(-0,0, L ) ;
82 
83  vpHomogeneousMatrix cMo_ref(0, 0.2, 1, 0, 0, 0) ;
84  for(int i=0 ; i < size ; i++)
85  {
86  P[i].project(cMo_ref) ;
87  P[i].print() ;
88  std::cout << std::endl;
89  }
90 
91  //Introduce an error
92  double error = 0.01;
93  P[3].set_y(P[3].get_y() + 2*error);
94  P[6].set_x(P[6].get_x() + error);
95 
96  vpPose pose;
97  for(int i=0 ; i < size ; i++)
98  pose.addPoint(P[i]);
99 
100  unsigned int nbInlierToReachConsensus = (unsigned int)(75.0 * (double)size / 100.0);
101  double threshold = 0.001;
102 
103  pose.setRansacNbInliersToReachConsensus(nbInlierToReachConsensus);
104  pose.setRansacThreshold(threshold);
105 
106  vpHomogeneousMatrix cMo ;
107  //vpPose::ransac(lp,lP, 5, 1e-6, ninliers, lPi, cMo) ;
108  pose.computePose(vpPose::RANSAC, cMo);
109 
110  std::vector<vpPoint> inliers = pose.getRansacInliers();
111 
112  std::cout << "Inliers: " << std::endl;
113  for (unsigned int i = 0; i < inliers.size() ; i++)
114  {
115  inliers[i].print() ;
116  std::cout << std::endl;
117  }
118 
119  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
120  vpPoseVector pose_est = vpPoseVector(cMo);
121 
122  std::cout << std::endl;
123  std::cout << "reference cMo :\n" << pose_ref.t() << std::endl << std::endl;
124  std::cout << "estimated cMo :\n" << pose_est.t() << std::endl << std::endl;
125 
126  int test_fail = 0;
127  for(unsigned int i=0; i<6; i++) {
128  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
129  test_fail = 1;
130  }
131 
132  std::cout << "Pose is " << (test_fail ? "badly" : "well") << " estimated" << std::endl;
133  delete [] P;
134  return test_fail;
135  }
136  catch(vpException e) {
137  std::cout << "Catch an exception: " << e << std::endl;
138  return 1;
139  }
140 }
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
error that can be emited by ViSP classes.
Definition: vpException.h:76
void setRansacThreshold(const double &t)
Definition: vpPose.h:170
void set_x(const double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.h:194
Class that defines what is a point.
Definition: vpPoint.h:65
virtual void print() const
vpRowVector t() const
transpose of Vector
Class used for pose computation from N points (pose from point only).
Definition: vpPose.h:78
void set_y(const double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.h:196
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition: vpPose.h:169
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
void computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo)
compute the pose for a given method
Definition: vpPose.cpp:386
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:155
std::vector< vpPoint > getRansacInliers() const
Definition: vpPose.h:173
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