Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testFindMatch.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 << "Find Matches using Ransac" << std::endl;
63  std::vector<vpPoint> P;
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  P.push_back( vpPoint(-0,L/2., L ) );
70 
71  vpHomogeneousMatrix cMo_ref(0, 0.2, 1, vpMath::rad(3), vpMath::rad(-2), vpMath::rad(10)) ;
72 
73  std::vector<vpPoint> p( P.size() );
74  for(unsigned int i=0 ; i < P.size() ; i++)
75  {
76  vpPoint pt = P[i];
77  pt.project(cMo_ref);
78  p[i].set_x(pt.get_x());
79  p[i].set_y(pt.get_y());
80  }
81 
82  unsigned int ninliers ;
83  std::vector<vpPoint> inliers;
84  double threshold = 1e-6;
85  unsigned int nbInlierToReachConsensus = (unsigned int)(P.size());
86 
88 
89  vpPose::findMatch(p,P,nbInlierToReachConsensus,threshold,ninliers,inliers,cMo);
90 
91  std::cout << "Inliers: " << std::endl;
92  for (unsigned int i = 0; i < inliers.size() ; i++)
93  {
94  inliers[i].print() ;
95  std::cout << std::endl;
96  }
97 
98  std::cout << "cMo :\n" << vpPoseVector(cMo).t() << std::endl << std::endl;
99 
100  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
101  vpPoseVector pose_est = vpPoseVector(cMo);
102 
103  std::cout << std::endl;
104  std::cout << "reference cMo :\n" << pose_ref.t() << std::endl << std::endl;
105  std::cout << "estimated cMo :\n" << pose_est.t() << std::endl << std::endl;
106 
107  int test_fail = 0;
108  for(unsigned int i=0; i<6; i++) {
109  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
110  test_fail = 1;
111  }
112 
113  std::cout << "Matching is " << (test_fail ? "badly" : "well") << " performed" << std::endl;
114 
115  return test_fail;
116  }
117  catch(vpException &e) {
118  std::cout << "Catch an exception: " << e << std::endl;
119  return 1;
120  }
121 }
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:73
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:458
Class that defines what is a point.
Definition: vpPoint.h:59
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)
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:456
static double rad(double deg)
Definition: vpMath.h:104
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
vpRowVector t() const