Visual Servoing Platform version 3.5.0
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
58int main()
59{
60#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
61 try {
62 std::cout << "Pose computation with matched points" << std::endl;
63 std::vector<vpPoint> P; // Point to be tracked
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
70 double L2 = L * 3.0;
71 P.push_back(vpPoint(0, -L2, 0));
72 P.push_back(vpPoint(L2, 0, 0));
73 P.push_back(vpPoint(0, L2, 0));
74 P.push_back(vpPoint(-L2, 0, 0));
75
76 vpHomogeneousMatrix cMo_ref(0, 0.2, 1, 0, 0, 0);
77 for (size_t i = 0; i < P.size(); i++) {
78 P[i].project(cMo_ref);
79 P[i].print();
80 std::cout << std::endl;
81 }
82
83 // Introduce an error
84 double error = 0.01;
85 P[3].set_y(P[3].get_y() + 2 * error);
86 P[6].set_x(P[6].get_x() + error);
87
88 vpPose pose;
89 for (size_t i = 0; i < P.size(); i++)
90 pose.addPoint(P[i]);
91
92 unsigned int nbInlierToReachConsensus = (unsigned int)(75.0 * (double)(P.size()) / 100.0);
93 double threshold = 0.001;
94
95 pose.setRansacNbInliersToReachConsensus(nbInlierToReachConsensus);
96 pose.setRansacThreshold(threshold);
97
99 // vpPose::ransac(lp,lP, 5, 1e-6, ninliers, lPi, cMo) ;
100 pose.computePose(vpPose::RANSAC, cMo);
101
102 std::vector<vpPoint> inliers = pose.getRansacInliers();
103
104 std::cout << "Inliers: " << std::endl;
105 for (unsigned int i = 0; i < inliers.size(); i++) {
106 inliers[i].print();
107 std::cout << std::endl;
108 }
109
110 vpPoseVector pose_ref = vpPoseVector(cMo_ref);
111 vpPoseVector pose_est = vpPoseVector(cMo);
112
113 std::cout << std::endl;
114 std::cout << "reference cMo :\n" << pose_ref.t() << std::endl << std::endl;
115 std::cout << "estimated cMo :\n" << pose_est.t() << std::endl << std::endl;
116
117 int test_fail = 0;
118 for (unsigned int i = 0; i < 6; i++) {
119 if (std::fabs(pose_ref[i] - pose_est[i]) > 0.001)
120 test_fail = 1;
121 }
122
123 std::cout << "Pose is " << (test_fail ? "badly" : "well") << " estimated" << std::endl;
124 return (test_fail ? EXIT_FAILURE : EXIT_SUCCESS);
125 } catch (const vpException &e) {
126 std::cout << "Catch an exception: " << e << std::endl;
127 return EXIT_FAILURE;
128 }
129#else
130 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
131 return EXIT_SUCCESS;
132#endif
133}
error that can be emited by ViSP classes.
Definition: vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:82
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:152
vpRowVector t() const
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:81
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:149
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition: vpPose.h:235
@ RANSAC
Definition: vpPose.h:90
std::vector< vpPoint > getRansacInliers() const
Definition: vpPose.h:248
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition: vpPose.cpp:374
void setRansacThreshold(const double &t)
Definition: vpPose.h:236