Visual Servoing Platform version 3.5.0
homographyHLM3DObject.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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 * Test the HLM (Malis) homography estimation algorithm with a 3D object.
33 *
34 * Authors:
35 * Eric Marchand
36 *
37 *****************************************************************************/
38
55#include <visp3/core/vpDebug.h>
56#include <visp3/core/vpMath.h>
57#include <visp3/core/vpRotationMatrix.h>
58#include <visp3/core/vpThetaUVector.h>
59#include <visp3/vision/vpHomography.h>
60
61#include <stdlib.h>
62#include <visp3/core/vpDebug.h>
63#include <visp3/core/vpHomogeneousMatrix.h>
64#include <visp3/core/vpMath.h>
65#include <visp3/core/vpPoint.h>
66#include <visp3/io/vpParseArgv.h>
67// List of allowed command line options
68#define GETOPTARGS "h"
69
70#define L 0.1
71#define nbpt 11
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", 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], NULL);
122 return false;
123 break;
124
125 default:
126 usage(argv[0], optarg_);
127 return false;
128 break;
129 }
130 }
131
132 if ((c == 1) || (c == -1)) {
133 // standalone param or error
134 usage(argv[0], NULL);
135 std::cerr << "ERROR: " << std::endl;
136 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137 return false;
138 }
139
140 return true;
141}
142
143int main(int argc, const char **argv)
144{
145#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
146 try {
147 // Read the command line options
148 if (getOptions(argc, argv) == false) {
149 exit(-1);
150 }
151
152 vpPoint P[nbpt]; // Point to be tracked
153 std::vector<double> xa(nbpt), ya(nbpt);
154 std::vector<double> xb(nbpt), yb(nbpt);
155
156 vpPoint aP[nbpt]; // Point to be tracked
157 vpPoint bP[nbpt]; // Point to be tracked
158
159 P[0].setWorldCoordinates(-L, -L, 0);
160 P[1].setWorldCoordinates(2 * L, -L, 0);
161 P[2].setWorldCoordinates(L, L, 0);
162 P[3].setWorldCoordinates(-L, 3 * L, 0);
163 P[4].setWorldCoordinates(0, 0, L);
164 P[5].setWorldCoordinates(L, -2 * L, L);
165 P[6].setWorldCoordinates(L, -4 * L, 2 * L);
166 P[7].setWorldCoordinates(-2 * L, -L, -L);
167 P[8].setWorldCoordinates(-5 * L, -5 * L, L);
168 P[9].setWorldCoordinates(-2 * L, +3 * L, 2 * L);
169 P[10].setWorldCoordinates(-2 * L, -0.5 * L, 2 * L);
170
171 vpHomogeneousMatrix bMo(0, 0, 1, 0, 0, 0);
172 vpHomogeneousMatrix aMb(0.1, 0.1, 0.1, vpMath::rad(10), 0, vpMath::rad(40));
173 vpHomogeneousMatrix aMo = aMb * bMo;
174 for (unsigned int i = 0; i < nbpt; i++) {
175 P[i].project(aMo);
176 aP[i] = P[i];
177 xa[i] = P[i].get_x();
178 ya[i] = P[i].get_y();
179 }
180
181 for (unsigned int i = 0; i < nbpt; i++) {
182 P[i].project(bMo);
183 bP[i] = P[i];
184 xb[i] = P[i].get_x();
185 yb[i] = P[i].get_y();
186 }
187
190 vpColVector n;
191 std::cout << "-------------------------------" << std::endl;
192 std::cout << "Compare with built homography H = R + t/d n " << std::endl;
193 vpPlane bp(0, 0, 1, 1);
194 vpHomography aHb_built(aMb, bp);
195 std::cout << "aHb built from the displacement: \n" << aHb_built / aHb_built[2][2] << std::endl;
196
197 aHb_built.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 << "aMb " << std::endl << aMb << std::endl;
207 std::cout << "-------------------------------" << std::endl;
208 vpHomography aHb;
209
210 vpHomography::HLM(xb, yb, xa, ya, false, aHb);
211
212 std::cout << "aHb computed using the Malis paralax algorithm" << std::endl;
213 aHb /= aHb[2][2];
214 std::cout << std::endl << aHb << std::endl;
215
216 std::cout << "-------------------------------" << std::endl;
217 std::cout << "extract R, T and n " << std::endl;
218 aHb.computeDisplacement(aRb, aTb, n);
219 std::cout << "Rotation: aRb" << std::endl;
220 std::cout << aRb << std::endl;
221 std::cout << "Translation: aTb" << std::endl;
222 std::cout << (aTb).t() << std::endl;
223 std::cout << "Normal to the plane: n" << std::endl;
224 std::cout << (n).t() << std::endl;
225
226 std::cout << "-------------------------------" << std::endl;
227 std::cout << "test if ap = aHb bp" << std::endl;
228
229 for (unsigned int i = 0; i < nbpt; i++) {
230 std::cout << "Point " << i << std::endl;
231 vpPoint p;
232 std::cout << "(";
233 std::cout << aP[i].get_x() / aP[i].get_w() << ", " << aP[i].get_y() / aP[i].get_w();
234 std::cout << ") = (";
235 p = aHb * bP[i];
236 std::cout << p.get_x() / p.get_w() << ", " << p.get_y() / p.get_w() << ")" << std::endl;
237 }
238 return EXIT_SUCCESS;
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.
Definition: vpColVector.h:131
error that can be emited by ViSP classes.
Definition: vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:175
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:110
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:59
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:82
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.cpp:474
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:472
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:470
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
Implementation of a rotation matrix and operations on such kind of matrices.
Class that consider the case of a translation vector.