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