Visual Servoing Platform version 3.5.0
testFindMatch.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 << "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 vpPoint pt = P[i];
76 pt.project(cMo_ref);
77 p[i].set_x(pt.get_x());
78 p[i].set_y(pt.get_y());
79 }
80
81 unsigned int ninliers;
82 std::vector<vpPoint> inliers;
83 double threshold = 1e-6;
84 unsigned int nbInlierToReachConsensus = (unsigned int)(P.size());
85
87
88 vpPose::findMatch(p, P, nbInlierToReachConsensus, threshold, ninliers, inliers, cMo);
89
90 std::cout << "Inliers: " << std::endl;
91 for (unsigned int i = 0; i < inliers.size(); i++) {
92 inliers[i].print();
93 std::cout << std::endl;
94 }
95
96 std::cout << "cMo :\n" << vpPoseVector(cMo).t() << std::endl << std::endl;
97
98 vpPoseVector pose_ref = vpPoseVector(cMo_ref);
99 vpPoseVector pose_est = vpPoseVector(cMo);
100
101 std::cout << std::endl;
102 std::cout << "reference cMo :\n" << pose_ref.t() << std::endl << std::endl;
103 std::cout << "estimated cMo :\n" << pose_est.t() << std::endl << std::endl;
104
105 int test_fail = 0;
106 for (unsigned int i = 0; i < 6; i++) {
107 if (std::fabs(pose_ref[i] - pose_est[i]) > 0.001)
108 test_fail = 1;
109 }
110
111 std::cout << "Matching is " << (test_fail ? "badly" : "well") << " performed" << std::endl;
112
113 return (test_fail ? EXIT_FAILURE : EXIT_SUCCESS);
114 } catch (const vpException &e) {
115 std::cout << "Catch an exception: " << e << std::endl;
116 return EXIT_FAILURE;
117 }
118#else
119 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
120 return EXIT_SUCCESS;
121#endif
122}
error that can be emited by ViSP classes.
Definition: vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
static double rad(double deg)
Definition: vpMath.h:110
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:82
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:472
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:470
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:152
vpRowVector t() const
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, bool useParallelRansac=true, unsigned int nthreads=0, bool(*func)(const vpHomogeneousMatrix &)=NULL)