Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
homographyHartleyDLT2DObject.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 HartleyDLT homography estimation algorithm
32 */
47
48#include <visp3/core/vpConfig.h>
49#include <visp3/core/vpDebug.h>
50#include <visp3/core/vpMath.h>
51#include <visp3/core/vpRotationMatrix.h>
52#include <visp3/core/vpThetaUVector.h>
53#include <visp3/vision/vpHomography.h>
54
55#include <stdlib.h>
56#include <visp3/core/vpDebug.h>
57#include <visp3/core/vpHomogeneousMatrix.h>
58#include <visp3/core/vpMath.h>
59#include <visp3/core/vpPoint.h>
60#include <visp3/io/vpParseArgv.h>
61// List of allowed command line options
62#define GETOPTARGS "h"
63
64#define L 0.1
65#define nbpt 5
66
67#ifdef ENABLE_VISP_NAMESPACE
68using namespace VISP_NAMESPACE_NAME;
69#endif
70
71void usage(const char *name, const char *badparam);
72bool getOptions(int argc, const char **argv);
73
83void usage(const char *name, const char *badparam)
84{
85 fprintf(stdout, "\n\
86Test the HartleyDLT homography estimation algorithm.\n\
87\n\
88SYNOPSIS\n\
89 %s [-h]\n",
90 name);
91
92 fprintf(stdout, "\n\
93OPTIONS: Default\n\
94 -h\n\
95 Print the help.\n");
96
97 if (badparam) {
98 fprintf(stderr, "ERROR: \n");
99 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
100 }
101}
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), 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::DLT(xb, yb, xa, ya, aHb, true);
187
188 vpTRACE("aHb computed using the DLT algorithm");
189 aHb /= aHb[2][2];
190 std::cout << std::endl << aHb << std::endl;
191
194 vpColVector n;
195
196 std::cout << "-------------------------------" << std::endl;
197 vpTRACE("extract R, T and n ");
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 vpTRACE("Compare with built homoraphy H = R + t/d ");
208 vpPlane bp(0, 0, 1, 1);
209 vpHomography aHb_built(aMb, bp);
210 vpTRACE("aHb built from the displacement ");
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 vpTRACE("test if ap = aHb bp");
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 return EXIT_SUCCESS;
234 }
235 catch (const vpException &e) {
236 std::cout << "Catch an exception: " << e << std::endl;
237 return EXIT_FAILURE;
238 }
239#else
240 (void)argc;
241 (void)argv;
242 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
243 return EXIT_SUCCESS;
244#endif
245}
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.
static void DLT(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, vpHomography &aHb, bool normalization=true)
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
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.
#define vpTRACE
Definition vpDebug.h:450