ViSP  2.9.0
testFindMatch.cpp
1 /****************************************************************************
2  *
3  * $Id: testFindMatch.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 << "Find Matches using Ransac" << std::endl;
67  unsigned int nb3D = 5;
68  unsigned int nb2D = 5;
69  std::vector<vpPoint> P(nb3D);
70  std::vector<vpPoint> p(nb2D);
71 
72  P[0].setWorldCoordinates(-L,-L, 0 ) ;
73  P[1].setWorldCoordinates(L,-L, 0 ) ;
74  P[2].setWorldCoordinates(L,L, 0 ) ;
75  P[3].setWorldCoordinates(-L,L, 0 ) ;
76  P[4].setWorldCoordinates(-0,L/2., L ) ;
77 
78  vpHomogeneousMatrix cMo_ref(0, 0.2, 1, vpMath::rad(3), vpMath::rad(-2), vpMath::rad(10)) ;
79 
80  for(unsigned int i=0 ; i < nb3D ; i++)
81  {
82  vpPoint pt = P[i];
83  pt.project(cMo_ref);
84  p[i].set_x(pt.get_x());
85  p[i].set_y(pt.get_y());
86  }
87 
88  unsigned int ninliers ;
89  std::vector<vpPoint> inliers;
90  double threshold = 1e-6;
91  unsigned int nbInlierToReachConsensus = nb3D;
92 
94 
95  vpPose::findMatch(p,P,nbInlierToReachConsensus,threshold,ninliers,inliers,cMo);
96 
97  std::cout << "Inliers: " << std::endl;
98  for (unsigned int i = 0; i < inliers.size() ; i++)
99  {
100  inliers[i].print() ;
101  std::cout << std::endl;
102  }
103 
104  std::cout << "cMo :\n" << vpPoseVector(cMo).t() << std::endl << std::endl;
105 
106  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
107  vpPoseVector pose_est = vpPoseVector(cMo);
108 
109  std::cout << std::endl;
110  std::cout << "reference cMo :\n" << pose_ref.t() << std::endl << std::endl;
111  std::cout << "estimated cMo :\n" << pose_est.t() << std::endl << std::endl;
112 
113  int test_fail = 0;
114  for(unsigned int i=0; i<6; i++) {
115  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
116  test_fail = 1;
117  }
118 
119  std::cout << "Matching is " << (test_fail ? "badly" : "well") << " performed" << std::endl;
120 
121  return test_fail;
122  }
123  catch(vpException e) {
124  std::cout << "Catch an exception: " << e << std::endl;
125  return 1;
126  }
127 }
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
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.h:138
Class that defines what is a point.
Definition: vpPoint.h:65
static void findMatch(std::vector< vpPoint > &p2D, std::vector< vpPoint > &p3D, const unsigned int &numberOfInlierToReachAConsensus, const double &threshold, unsigned int &ninliers, std::vector< vpPoint > &listInliers, vpHomogeneousMatrix &cMo, const int &maxNbTrials=10000)
vpRowVector t() const
transpose of Vector
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
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92