ViSP  2.6.2
testPoseRansac.cpp
1 /****************************************************************************
2  *
3  * $Id: testPoseRansac.cpp 3780 2012-06-07 08:56:03Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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*2.0;
75  P[4].setWorldCoordinates(-L2,-L2, 0 ) ;
76  P[5].setWorldCoordinates(L2,-L2, 0 ) ;
77  P[6].setWorldCoordinates(L2,L2, 0 ) ;
78  P[7].setWorldCoordinates(-L2,L2, 0 ) ;
79 
80  //P[4].setWorldCoordinates(-0,0, L ) ; //ERREUR DANS LAGRANGE ET DEMENTHON
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.01;
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  std::cout << "cMo :\n" << vpPoseVector(cMo).t() << std::endl << std::endl;
119 
120  delete [] P;
121 }
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void setRansacThreshold(const double &t)
Definition: vpPose.h:174
void set_x(const double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.h:183
Class that defines what is a point.
Definition: vpPoint.h:65
std::vector< vpPoint > getRansacInliers()
Definition: vpPose.h:177
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:80
void setRansacNbInliersToReachConsensus(const int &nbC)
Definition: vpPose.h:173
void set_y(const double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.h:185
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:298
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:148
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