Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
homographyHLM3DObject.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 * Test the HLM (Malis) homography estimation algorithm with a 3D object.
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
66#define L 0.1
67#define nbpt 11
68
69#ifdef ENABLE_VISP_NAMESPACE
70using namespace VISP_NAMESPACE_NAME;
71#endif
72
73void usage(const char *name, const char *badparam);
74bool getOptions(int argc, const char **argv);
75
85void usage(const char *name, const char *badparam)
86{
87 fprintf(stdout, "\n\
88Test the HLM (Malis) homography estimation algorithm with a 3D object.\n\
89\n\
90SYNOPSIS\n\
91 %s [-h]\n",
92 name);
93
94 fprintf(stdout, "\n\
95OPTIONS: Default\n\
96 -h\n\
97 Print the help.\n");
98
99 if (badparam) {
100 fprintf(stderr, "ERROR: \n");
101 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
102 }
103}
114bool getOptions(int argc, const char **argv)
115{
116 const char *optarg_;
117 int c;
118 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
119
120 switch (c) {
121 case 'h':
122 usage(argv[0], nullptr);
123 return false;
124
125 default:
126 usage(argv[0], optarg_);
127 return false;
128 }
129 }
130
131 if ((c == 1) || (c == -1)) {
132 // standalone param or error
133 usage(argv[0], nullptr);
134 std::cerr << "ERROR: " << std::endl;
135 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136 return false;
137 }
138
139 return true;
140}
141
142int main(int argc, const char **argv)
143{
144#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
145 try {
146 // Read the command line options
147 if (getOptions(argc, argv) == false) {
148 return EXIT_FAILURE;
149 }
150
151 vpPoint P[nbpt]; // Point to be tracked
152 std::vector<double> xa(nbpt), ya(nbpt);
153 std::vector<double> xb(nbpt), yb(nbpt);
154
155 vpPoint aP[nbpt]; // Point to be tracked
156 vpPoint bP[nbpt]; // Point to be tracked
157
158 P[0].setWorldCoordinates(-L, -L, 0);
159 P[1].setWorldCoordinates(2 * L, -L, 0);
160 P[2].setWorldCoordinates(L, L, 0);
161 P[3].setWorldCoordinates(-L, 3 * L, 0);
162 P[4].setWorldCoordinates(0, 0, L);
163 P[5].setWorldCoordinates(L, -2 * L, L);
164 P[6].setWorldCoordinates(L, -4 * L, 2 * L);
165 P[7].setWorldCoordinates(-2 * L, -L, -L);
166 P[8].setWorldCoordinates(-5 * L, -5 * L, L);
167 P[9].setWorldCoordinates(-2 * L, +3 * L, 2 * L);
168 P[10].setWorldCoordinates(-2 * L, -0.5 * L, 2 * L);
169
170 vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
171 vpHomogeneousMatrix aMb(0.1, 0.1, 0.1, vpMath::rad(10), 0, vpMath::rad(40));
172 vpHomogeneousMatrix aMo = aMb * bMo;
173 for (unsigned int i = 0; i < nbpt; i++) {
174 P[i].project(aMo);
175 aP[i] = P[i];
176 xa[i] = P[i].get_x();
177 ya[i] = P[i].get_y();
178 }
179
180 for (unsigned int i = 0; i < nbpt; i++) {
181 P[i].project(bMo);
182 bP[i] = P[i];
183 xb[i] = P[i].get_x();
184 yb[i] = P[i].get_y();
185 }
186
189 vpColVector n;
190 std::cout << "-------------------------------" << std::endl;
191 std::cout << "Compare with built homography H = R + t/d n " << std::endl;
192 vpPlane bp(0, 0, 1, 1);
193 vpHomography aHb_built(aMb, bp);
194 std::cout << "aHb built from the displacement: \n" << aHb_built / aHb_built[2][2] << std::endl;
195
196 aHb_built.computeDisplacement(aRb, aTb, n);
197 std::cout << "Rotation: aRb" << std::endl;
198 std::cout << aRb << std::endl;
199 std::cout << "Translation: aTb" << std::endl;
200 std::cout << (aTb).t() << std::endl;
201 std::cout << "Normal to the plane: n" << std::endl;
202 std::cout << (n).t() << std::endl;
203
204 std::cout << "-------------------------------" << std::endl;
205 std::cout << "aMb " << std::endl << aMb << std::endl;
206 std::cout << "-------------------------------" << std::endl;
207 vpHomography aHb;
208
209 vpHomography::HLM(xb, yb, xa, ya, false, aHb);
210
211 std::cout << "aHb computed using the Malis paralax algorithm" << std::endl;
212 aHb /= aHb[2][2];
213 std::cout << std::endl << aHb << std::endl;
214
215 std::cout << "-------------------------------" << std::endl;
216 std::cout << "extract R, T and n " << std::endl;
217 aHb.computeDisplacement(aRb, aTb, n);
218 std::cout << "Rotation: aRb" << std::endl;
219 std::cout << aRb << std::endl;
220 std::cout << "Translation: aTb" << std::endl;
221 std::cout << (aTb).t() << std::endl;
222 std::cout << "Normal to the plane: n" << std::endl;
223 std::cout << (n).t() << std::endl;
224
225 std::cout << "-------------------------------" << std::endl;
226 std::cout << "test if ap = aHb bp" << std::endl;
227
228 for (unsigned int i = 0; i < nbpt; i++) {
229 std::cout << "Point " << i << std::endl;
230 vpPoint p;
231 std::cout << "(";
232 std::cout << aP[i].get_x() / aP[i].get_w() << ", " << aP[i].get_y() / aP[i].get_w();
233 std::cout << ") = (";
234 p = aHb * bP[i];
235 std::cout << p.get_x() / p.get_w() << ", " << p.get_y() / p.get_w() << ")" << std::endl;
236 }
237 return EXIT_SUCCESS;
238 }
239 catch (const vpException &e) {
240 std::cout << "Catch an exception: " << e << std::endl;
241 return EXIT_FAILURE;
242 }
243#else
244 (void)argc;
245 (void)argv;
246 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
247 return EXIT_SUCCESS;
248#endif
249}
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.