Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
testPoseRansac.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Compute the pose of a 3D object using the Dementhon method. Assuming that
33  * the correspondance between 2D points and 3D points is not done, we use
34  * the RANSAC algorithm to achieve this task
35  *
36  * Authors:
37  * Aurelien Yol
38  *
39  *****************************************************************************/
40 
41 #include <visp3/core/vpHomogeneousMatrix.h>
42 #include <visp3/core/vpMath.h>
43 #include <visp3/core/vpPoint.h>
44 #include <visp3/vision/vpPose.h>
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #define L 0.1
50 
58 int main()
59 {
60  try {
61  std::cout << "Pose computation with matched points" << std::endl;
62  std::vector<vpPoint> P; // Point to be tracked
63 
64  P.push_back(vpPoint(-L, -L, 0));
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 
69  double L2 = L * 3.0;
70  P.push_back(vpPoint(0, -L2, 0));
71  P.push_back(vpPoint(L2, 0, 0));
72  P.push_back(vpPoint(0, L2, 0));
73  P.push_back(vpPoint(-L2, 0, 0));
74 
75  vpHomogeneousMatrix cMo_ref(0, 0.2, 1, 0, 0, 0);
76  for (size_t i = 0; i < P.size(); i++) {
77  P[i].project(cMo_ref);
78  P[i].print();
79  std::cout << std::endl;
80  }
81 
82  // Introduce an error
83  double error = 0.01;
84  P[3].set_y(P[3].get_y() + 2 * error);
85  P[6].set_x(P[6].get_x() + error);
86 
87  vpPose pose;
88  for (size_t i = 0; i < P.size(); i++)
89  pose.addPoint(P[i]);
90 
91  unsigned int nbInlierToReachConsensus = (unsigned int)(75.0 * (double)(P.size()) / 100.0);
92  double threshold = 0.001;
93 
94  pose.setRansacNbInliersToReachConsensus(nbInlierToReachConsensus);
95  pose.setRansacThreshold(threshold);
96 
98  // vpPose::ransac(lp,lP, 5, 1e-6, ninliers, lPi, cMo) ;
99  pose.computePose(vpPose::RANSAC, cMo);
100 
101  std::vector<vpPoint> inliers = pose.getRansacInliers();
102 
103  std::cout << "Inliers: " << std::endl;
104  for (unsigned int i = 0; i < inliers.size(); i++) {
105  inliers[i].print();
106  std::cout << std::endl;
107  }
108 
109  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
110  vpPoseVector pose_est = vpPoseVector(cMo);
111 
112  std::cout << std::endl;
113  std::cout << "reference cMo :\n" << pose_ref.t() << std::endl << std::endl;
114  std::cout << "estimated cMo :\n" << pose_est.t() << std::endl << std::endl;
115 
116  int test_fail = 0;
117  for (unsigned int i = 0; i < 6; i++) {
118  if (std::fabs(pose_ref[i] - pose_est[i]) > 0.001)
119  test_fail = 1;
120  }
121 
122  std::cout << "Pose is " << (test_fail ? "badly" : "well") << " estimated" << std::endl;
123  return test_fail;
124  } catch (const vpException &e) {
125  std::cout << "Catch an exception: " << e << std::endl;
126  return 1;
127  }
128 }
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition: vpPose.cpp:374
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:71
void setRansacThreshold(const double &t)
Definition: vpPose.h:248
std::vector< vpPoint > getRansacInliers() const
Definition: vpPose.h:260
Class that defines what is a point.
Definition: vpPoint.h:58
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:80
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition: vpPose.h:247
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:151
vpRowVector t() const
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:149