Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
homographyRansac2DObject.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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 * Example of the Ransac homography estimation algorithm.
33 *
34*****************************************************************************/
35
52#include <visp3/core/vpDebug.h>
53#include <visp3/core/vpMath.h>
54#include <visp3/core/vpRotationMatrix.h>
55#include <visp3/core/vpThetaUVector.h>
56#include <visp3/vision/vpHomography.h>
57
58#include <visp3/core/vpDebug.h>
59#include <visp3/core/vpHomogeneousMatrix.h>
60#include <visp3/core/vpMath.h>
61#include <visp3/core/vpPoint.h>
62
63#include <stdlib.h>
64#include <visp3/core/vpRansac.h>
65#include <visp3/io/vpParseArgv.h>
66// List of allowed command line options
67#define GETOPTARGS "h"
68
69void usage(const char *name, const char *badparam);
70bool getOptions(int argc, const char **argv);
71
80void usage(const char *name, const char *badparam)
81{
82 fprintf(stdout, "\n\
83Test the Ransac homography estimation algorithm.\n\
84\n\
85SYNOPSIS\n\
86 %s [-h]\n",
87 name);
88
89 fprintf(stdout, "\n\
90OPTIONS: Default\n\
91 -h\n\
92 Print the help.\n");
93
94 if (badparam) {
95 fprintf(stderr, "ERROR: \n");
96 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
97 }
98}
109bool getOptions(int argc, const char **argv)
110{
111 const char *optarg_;
112 int c;
113 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
114
115 switch (c) {
116 case 'h':
117 usage(argv[0], NULL);
118 return false;
119 break;
120
121 default:
122 usage(argv[0], optarg_);
123 return false;
124 break;
125 }
126 }
127
128 if ((c == 1) || (c == -1)) {
129 // standalone param or error
130 usage(argv[0], NULL);
131 std::cerr << "ERROR: " << std::endl;
132 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
133 return false;
134 }
135
136 return true;
137}
138
139int main(int argc, const char **argv)
140{
141#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
142 try {
143 // Read the command line options
144 if (getOptions(argc, argv) == false) {
145 return EXIT_FAILURE;
146 }
147
148 double L = 0.1;
149 unsigned int nbpt = 11;
150
151 std::vector<vpPoint> P(nbpt); // Point to be tracked
152 std::vector<double> xa(nbpt), ya(nbpt), xb(nbpt), yb(nbpt);
153
154 P[0].setWorldCoordinates(-L, -L, 0); // inlier
155 P[1].setWorldCoordinates(2 * L, -L, 0); // inlier
156 P[2].setWorldCoordinates(L, L, 0); // inlier
157 P[3].setWorldCoordinates(-L, 3 * L, 0); // inlier
158 P[4].setWorldCoordinates(0, 0, L);
159 P[5].setWorldCoordinates(L, -2 * L, L);
160 P[6].setWorldCoordinates(L, -4 * L, 2 * L);
161 P[7].setWorldCoordinates(-2 * L, -L, -3 * L);
162 P[8].setWorldCoordinates(-5 * L, -5 * L, 0); // inlier
163 P[9].setWorldCoordinates(-2 * L, +3 * L, 4 * L);
164 P[10].setWorldCoordinates(-2 * L, -0.5 * L, 0); // inlier
165
166 std::vector<bool> inliers_ground_truth(nbpt, false);
167 inliers_ground_truth[0] = true;
168 inliers_ground_truth[1] = true;
169 inliers_ground_truth[2] = true;
170 inliers_ground_truth[3] = true;
171 inliers_ground_truth[8] = true;
172 inliers_ground_truth[10] = true;
173
174 vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
175 vpHomogeneousMatrix aMb(0.1, 0.1, 0.1, vpMath::rad(10), 0, vpMath::rad(40));
176 vpHomogeneousMatrix aMo = aMb * bMo;
177 for (unsigned int i = 0; i < nbpt; i++) {
178 P[i].project(aMo);
179 xa[i] = P[i].get_x();
180 ya[i] = P[i].get_y();
181 }
182
183 for (unsigned int i = 0; i < nbpt; i++) {
184 P[i].project(bMo);
185 xb[i] = P[i].get_x();
186 yb[i] = P[i].get_y();
187 }
188 std::cout << "-------------------------------" << std::endl;
189
192 vpColVector n;
193 std::cout << "Compare with built homography H = R + t/d n " << std::endl;
194 vpPlane bp(0, 0, 1, 1);
195 vpHomography aHb_built(aMb, bp);
196 std::cout << "aHb built from the displacement: \n" << aHb_built / aHb_built[2][2] << std::endl;
197
198 aHb_built.computeDisplacement(aRb, aTb, n);
199 std::cout << "Rotation aRb: " << std::endl;
200 std::cout << aRb << std::endl;
201 std::cout << "Translation: aTb" << std::endl;
202 std::cout << (aTb).t() << std::endl;
203 std::cout << "Normal to the plane: n" << std::endl;
204 std::cout << (n).t() << std::endl;
205
206 std::cout << "-------------------------------" << std::endl;
207 vpHomography aHb;
208 std::vector<bool> inliers;
209 double residual;
210 // Suppose px=1000. Set the threshold to 2 pixels => 2/1000
211 // In the data we have 6 inliers. We request that at least 6 are retrieved
212 vpHomography::ransac(xb, yb, xa, ya, aHb, inliers, residual, 6, 2. / 1000);
213
214 std::cout << "aHb estimated using ransac:\n" << aHb << std::endl;
215 std::cout << "Inliers indexes (should be 0,1,2,3,8,10): ";
216 for (unsigned int i = 0; i < inliers.size(); i++)
217 if (inliers[i])
218 std::cout << i << ",";
219 std::cout << std::endl;
220
221 if (inliers == inliers_ground_truth) {
222 std::cout << "Ransac estimation succeed" << std::endl;
223 return EXIT_SUCCESS;
224 }
225 else {
226 std::cout << "Ransac estimation fails" << std::endl;
227 return EXIT_FAILURE;
228 }
229 }
230 catch (const vpException &e) {
231 std::cout << "Catch an exception: " << e << std::endl;
232 return EXIT_FAILURE;
233 }
234#else
235 (void)argc;
236 (void)argv;
237 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
238 return EXIT_SUCCESS;
239#endif
240}
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:59
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
static bool ransac(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, vpHomography &aHb, std::vector< bool > &inliers, double &residual, unsigned int nbInliersConsensus, double threshold, bool normalization=true)
static double rad(double deg)
Definition vpMath.h:116
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
This class defines the container for a plane geometrical structure.
Definition vpPlane.h:54
Implementation of a rotation matrix and operations on such kind of matrices.
Class that consider the case of a translation vector.