Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
homographyHLM2DObject.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 HLM (Malis) 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 <stdlib.h>
59#include <visp3/core/vpDebug.h>
60#include <visp3/core/vpHomogeneousMatrix.h>
61#include <visp3/core/vpMath.h>
62#include <visp3/core/vpPoint.h>
63#include <visp3/io/vpParseArgv.h>
64// List of allowed command line options
65#define GETOPTARGS "h"
66#define L 0.1
67#define nbpt 5
68
69void usage(const char *name, const char *badparam);
70bool getOptions(int argc, const char **argv);
71
81void usage(const char *name, const char *badparam)
82{
83 fprintf(stdout, "\n\
84Test the HLM (Malis) homography estimation algorithm with a planar object.\n\
85\n\
86SYNOPSIS\n\
87 %s [-h]\n",
88 name);
89
90 fprintf(stdout, "\n\
91OPTIONS: Default\n\
92 -h\n\
93 Print the help.\n");
94
95 if (badparam) {
96 fprintf(stderr, "ERROR: \n");
97 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
98 }
99}
110bool getOptions(int argc, const char **argv)
111{
112 const char *optarg_;
113 int c;
114 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
115
116 switch (c) {
117 case 'h':
118 usage(argv[0], NULL);
119 return false;
120 break;
121
122 default:
123 usage(argv[0], optarg_);
124 return false;
125 break;
126 }
127 }
128
129 if ((c == 1) || (c == -1)) {
130 // standalone param or error
131 usage(argv[0], NULL);
132 std::cerr << "ERROR: " << std::endl;
133 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
134 return false;
135 }
136
137 return true;
138}
139
140int main(int argc, const char **argv)
141{
142#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
143 try {
144 // Read the command line options
145 if (getOptions(argc, argv) == false) {
146 return EXIT_FAILURE;
147 }
148
149 vpPoint P[nbpt]; // Point to be tracked
150 std::vector<double> xa(nbpt), ya(nbpt);
151 std::vector<double> xb(nbpt), yb(nbpt);
152
153 vpPoint aP[nbpt]; // Point to be tracked
154 vpPoint bP[nbpt]; // Point to be tracked
155
156 P[0].setWorldCoordinates(-L, -L, 0);
157 P[1].setWorldCoordinates(2 * L, -L, 0);
158 P[2].setWorldCoordinates(L, L, 0);
159 P[3].setWorldCoordinates(-L, 3 * L, 0);
160 P[4].setWorldCoordinates(0, 0, 0);
161 /*
162 P[5].setWorldCoordinates(10,20, 0 ) ;
163 P[6].setWorldCoordinates(-10,12, 0 ) ;
164 */
165 vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
166 vpHomogeneousMatrix aMb(1, 0, 0.0, vpMath::rad(10), 0, vpMath::rad(40));
167 vpHomogeneousMatrix aMo = aMb * bMo;
168 for (unsigned int i = 0; i < nbpt; i++) {
169 P[i].project(aMo);
170 aP[i] = P[i];
171 xa[i] = P[i].get_x();
172 ya[i] = P[i].get_y();
173 }
174
175 for (unsigned int i = 0; i < nbpt; i++) {
176 P[i].project(bMo);
177 bP[i] = P[i];
178 xb[i] = P[i].get_x();
179 yb[i] = P[i].get_y();
180 }
181 std::cout << "-------------------------------" << std::endl;
182 std::cout << "aMb " << std::endl << aMb << std::endl;
183 std::cout << "-------------------------------" << std::endl;
184 vpHomography aHb;
185
186 vpHomography::HLM(xb, yb, xa, ya, true, aHb);
187
188 aHb /= aHb[2][2];
189 std::cout << "aHb computed using the Malis paralax algorithm: \n" << aHb << std::endl;
190
193 vpColVector n;
194
195 std::cout << "-------------------------------" << std::endl;
196 std::cout << "extract R, T and n " << std::endl;
197 aHb.computeDisplacement(aRb, aTb, n);
198 std::cout << "Rotation: aRb" << std::endl;
199 std::cout << aRb << std::endl;
200 std::cout << "Translation: aTb" << std::endl;
201 std::cout << (aTb).t() << std::endl;
202 std::cout << "Normal to the plane: n" << std::endl;
203 std::cout << (n).t() << std::endl;
204
205 std::cout << "-------------------------------" << std::endl;
206 std::cout << "Compare with built homography H = R + t/d " << std::endl;
207 vpPlane bp(0, 0, 1, 1);
208 vpHomography aHb_built(aMb, bp);
209 std::cout << "aHb built from the displacement " << std::endl;
210 std::cout << std::endl << aHb_built / aHb_built[2][2] << std::endl;
211
212 aHb_built.computeDisplacement(aRb, aTb, n);
213 std::cout << "Rotation: aRb" << std::endl;
214 std::cout << aRb << std::endl;
215 std::cout << "Translation: aTb" << std::endl;
216 std::cout << (aTb).t() << std::endl;
217 std::cout << "Normal to the plane: n" << std::endl;
218 std::cout << (n).t() << std::endl;
219
220 std::cout << "-------------------------------" << std::endl;
221 std::cout << "test if ap = aHb bp" << std::endl;
222
223 for (unsigned int i = 0; i < nbpt; i++) {
224 std::cout << "Point " << i << std::endl;
225 vpPoint p;
226 std::cout << "(";
227 std::cout << aP[i].get_x() / aP[i].get_w() << ", " << aP[i].get_y() / aP[i].get_w();
228 std::cout << ") = (";
229 p = aHb * bP[i];
230 std::cout << p.get_x() / p.get_w() << ", " << p.get_y() / p.get_w() << ")" << std::endl;
231 }
232
233 std::cout << "-------------------------------" << std::endl;
234 std::cout << "test displacement" << std::endl;
235
236 std::list<vpRotationMatrix> laRb;
237 std::list<vpTranslationVector> laTb;
238 std::list<vpColVector> lnb;
239
240 vpHomography::computeDisplacement(aHb, bP[0].get_x(), bP[0].get_y(), laRb, laTb, lnb);
241
242 std::list<vpRotationMatrix>::const_iterator it_laRb = laRb.begin();
243 std::list<vpTranslationVector>::const_iterator it_laTb = laTb.begin();
244 std::list<vpColVector>::const_iterator it_lnb = lnb.begin();
245
246 int k = 1;
247 while (it_lnb != lnb.end()) {
248 std::cout << "Solution " << k++ << std::endl;
249
250 aRb = *it_laRb;
251 aTb = *it_laTb;
252 n = *it_lnb;
253 std::cout << "Rotation: aRb" << std::endl;
254 std::cout << aRb << std::endl;
255 std::cout << "Translation: aTb" << std::endl;
256 std::cout << (aTb).t() << std::endl;
257 std::cout << "Normal to the plane: n" << std::endl;
258 std::cout << (n).t() << std::endl;
259
260 ++it_laRb;
261 ++it_laTb;
262 ++it_lnb;
263 }
264 return EXIT_SUCCESS;
265 }
266 catch (const vpException &e) {
267 std::cout << "Catch an exception: " << e << std::endl;
268 return EXIT_FAILURE;
269 }
270#else
271 (void)argc;
272 (void)argv;
273 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
274 return EXIT_SUCCESS;
275#endif
276}
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 void HLM(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, bool isplanar, vpHomography &aHb)
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
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
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:77
double get_w() const
Get the point w coordinate in the image plane.
Definition vpPoint.cpp:471
double get_y() const
Get the point y coordinate in the image plane.
Definition vpPoint.cpp:469
double get_x() const
Get the point x coordinate in the image plane.
Definition vpPoint.cpp:467
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:110
Implementation of a rotation matrix and operations on such kind of matrices.
Class that consider the case of a translation vector.