Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testPoseRansac.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
36#include <visp3/core/vpHomogeneousMatrix.h>
37#include <visp3/core/vpMath.h>
38#include <visp3/core/vpPoint.h>
39#include <visp3/vision/vpPose.h>
40
41#include <stdio.h>
42#include <stdlib.h>
43
44#define L 0.1
45
53int main()
54{
55#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
56 try {
57 std::cout << "Pose computation with matched points" << std::endl;
58 std::vector<vpPoint> P; // Point to be tracked
59
60 P.push_back(vpPoint(-L, -L, 0));
61 P.push_back(vpPoint(L, -L, 0));
62 P.push_back(vpPoint(L, L, 0));
63 P.push_back(vpPoint(-L, L, 0));
64
65 double L2 = L * 3.0;
66 P.push_back(vpPoint(0, -L2, 0));
67 P.push_back(vpPoint(L2, 0, 0));
68 P.push_back(vpPoint(0, L2, 0));
69 P.push_back(vpPoint(-L2, 0, 0));
70
71 vpHomogeneousMatrix cMo_ref(0, 0.2, 1, 0, 0, 0);
72 for (size_t i = 0; i < P.size(); i++) {
73 P[i].project(cMo_ref);
74 P[i].print();
75 std::cout << std::endl;
76 }
77
78 // Introduce an error
79 double error = 0.01;
80 P[3].set_y(P[3].get_y() + 2 * error);
81 P[6].set_x(P[6].get_x() + error);
82
83 vpPose pose;
84 for (size_t i = 0; i < P.size(); i++)
85 pose.addPoint(P[i]);
86
87 unsigned int nbInlierToReachConsensus = (unsigned int)(75.0 * (double)(P.size()) / 100.0);
88 double threshold = 0.001;
89
90 pose.setRansacNbInliersToReachConsensus(nbInlierToReachConsensus);
91 pose.setRansacThreshold(threshold);
92
94 // vpPose::ransac(lp,lP, 5, 1e-6, ninliers, lPi, cMo) ;
95 pose.computePose(vpPose::RANSAC, cMo);
96
97 std::vector<vpPoint> inliers = pose.getRansacInliers();
98
99 std::cout << "Inliers: " << std::endl;
100 for (unsigned int i = 0; i < inliers.size(); i++) {
101 inliers[i].print();
102 std::cout << std::endl;
103 }
104
105 vpPoseVector pose_ref = vpPoseVector(cMo_ref);
106 vpPoseVector pose_est = vpPoseVector(cMo);
107
108 std::cout << std::endl;
109 std::cout << "reference cMo :\n" << pose_ref.t() << std::endl << std::endl;
110 std::cout << "estimated cMo :\n" << pose_est.t() << std::endl << std::endl;
111
112 int test_fail = 0;
113 for (unsigned int i = 0; i < 6; i++) {
114 if (std::fabs(pose_ref[i] - pose_est[i]) > 0.001)
115 test_fail = 1;
116 }
117
118 std::cout << "Pose is " << (test_fail ? "badly" : "well") << " estimated" << std::endl;
119 return (test_fail ? EXIT_FAILURE : EXIT_SUCCESS);
120 } catch (const vpException &e) {
121 std::cout << "Catch an exception: " << e << std::endl;
122 return EXIT_FAILURE;
123 }
124#else
125 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
126 return EXIT_SUCCESS;
127#endif
128}
error that can be emitted by ViSP classes.
Definition vpException.h:59
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:77
Implementation of a pose vector and operations on poses.
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:140
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition vpPose.h:245
@ RANSAC
Definition vpPose.h:91
std::vector< vpPoint > getRansacInliers() const
Definition vpPose.h:259
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition vpPose.cpp:469
void setRansacThreshold(const double &t)
Definition vpPose.h:246