Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
photometricVisualServoingWithoutVpServo.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 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
36
37#include <visp3/core/vpConfig.h>
38#include <visp3/core/vpDebug.h>
39
40#include <visp3/core/vpImage.h>
41#include <visp3/core/vpImageTools.h>
42#include <visp3/io/vpImageIo.h>
43
44#include <visp3/core/vpCameraParameters.h>
45#include <visp3/core/vpTime.h>
46#include <visp3/robot/vpSimulatorCamera.h>
47
48#include <visp3/core/vpHomogeneousMatrix.h>
49#include <visp3/core/vpMath.h>
50#include <visp3/gui/vpDisplayFactory.h>
51
52#include <visp3/io/vpParseArgv.h>
53#include <visp3/visual_features/vpFeatureLuminance.h>
54
55#include <stdlib.h>
56#include <visp3/robot/vpImageSimulator.h>
57#define Z 1
58
59#include <visp3/core/vpIoTools.h>
60#include <visp3/io/vpParseArgv.h>
61
62// List of allowed command line options
63#define GETOPTARGS "cdi:n:h"
64
65#ifdef ENABLE_VISP_NAMESPACE
66using namespace VISP_NAMESPACE_NAME;
67#endif
68
69void usage(const char *name, const char *badparam, const std::string &ipath, int niter);
70bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display, int &niter);
71
82void usage(const char *name, const char *badparam, const std::string &ipath, int niter)
83{
84 fprintf(stdout, "\n\
85Tracking of Surf key-points.\n\
86\n\
87SYNOPSIS\n\
88 %s [-i <input image path>] [-c] [-d] [-n <number of iterations>] [-h]\n",
89 name);
90
91 fprintf(stdout, "\n\
92OPTIONS: Default\n\
93 -i <input image path> %s\n\
94 Set image input path.\n\
95 From this path read \"doisneau/doisneau.jpg\"\n\
96 images. \n\
97 Setting the VISP_INPUT_IMAGE_PATH environment\n\
98 variable produces the same behaviour than using\n\
99 this option.\n\
100\n\
101 -c\n\
102 Disable the mouse click. Useful to automate the \n\
103 execution of this program without human intervention.\n\
104\n\
105 -d \n\
106 Turn off the display.\n\
107\n\
108 -n %%d %d\n\
109 Number of iterations.\n\
110\n\
111 -h\n\
112 Print the help.\n",
113 ipath.c_str(), niter);
114
115 if (badparam)
116 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
117}
132bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display, int &niter)
133{
134 const char *optarg_;
135 int c;
136 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
137
138 switch (c) {
139 case 'c':
140 click_allowed = false;
141 break;
142 case 'd':
143 display = false;
144 break;
145 case 'i':
146 ipath = optarg_;
147 break;
148 case 'n':
149 niter = atoi(optarg_);
150 break;
151 case 'h':
152 usage(argv[0], nullptr, ipath, niter);
153 return false;
154
155 default:
156 usage(argv[0], optarg_, ipath, niter);
157 return false;
158 }
159 }
160
161 if ((c == 1) || (c == -1)) {
162 // standalone param or error
163 usage(argv[0], nullptr, ipath, niter);
164 std::cerr << "ERROR: " << std::endl;
165 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
166 return false;
167 }
168
169 return true;
170}
171
172int main(int argc, const char **argv)
173{
174#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
175#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
176 std::shared_ptr<vpDisplay> d, d1;
177#else
178 vpDisplay *d = nullptr;
179 vpDisplay *d1 = nullptr;
180#endif
181 try {
182 std::string env_ipath;
183 std::string opt_ipath;
184 std::string ipath;
185 std::string filename;
186 bool opt_click_allowed = true;
187 bool opt_display = true;
188 int opt_niter = 400;
189
190 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
191 // environment variable value
193
194 // Set the default input path
195 if (!env_ipath.empty())
196 ipath = env_ipath;
197
198 // Read the command line options
199 if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display, opt_niter) == false) {
200 return EXIT_FAILURE;
201 }
202
203 // Get the option values
204 if (!opt_ipath.empty())
205 ipath = opt_ipath;
206
207 // Compare ipath and env_ipath. If they differ, we take into account
208 // the input path coming from the command line option
209 if (!opt_ipath.empty() && !env_ipath.empty()) {
210 if (ipath != env_ipath) {
211 std::cout << std::endl << "WARNING: " << std::endl;
212 std::cout << " Since -i <visp image path=" << ipath << "> "
213 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
214 << " we skip the environment variable." << std::endl;
215 }
216 }
217
218 // Test if an input path is set
219 if (opt_ipath.empty() && env_ipath.empty()) {
220 usage(argv[0], nullptr, ipath, opt_niter);
221 std::cerr << std::endl << "ERROR:" << std::endl;
222 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
223 << " environment variable to specify the location of the " << std::endl
224 << " image path where test images are located." << std::endl
225 << std::endl;
226 return EXIT_FAILURE;
227 }
228
229 vpImage<unsigned char> Itexture;
230 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
231 vpImageIo::read(Itexture, filename);
232
233 vpColVector X[4];
234 for (int i = 0; i < 4; i++)
235 X[i].resize(3);
236 // Top left corner
237 X[0][0] = -0.3;
238 X[0][1] = -0.215;
239 X[0][2] = 0;
240
241 // Top right corner
242 X[1][0] = 0.3;
243 X[1][1] = -0.215;
244 X[1][2] = 0;
245
246 // Bottom right corner
247 X[2][0] = 0.3;
248 X[2][1] = 0.215;
249 X[2][2] = 0;
250
251 // Bottom left corner
252 X[3][0] = -0.3;
253 X[3][1] = 0.215;
254 X[3][2] = 0;
255
257
259 sim.init(Itexture, X);
260
261 vpCameraParameters cam(870, 870, 160, 120);
262
263 // ----------------------------------------------------------
264 // Create the framegraber (here a simulated image)
265 vpImage<unsigned char> I(240, 320, 0);
267
268 // camera desired position
270 cdMo[2][3] = 1;
271
272 // set the robot at the desired position
273 sim.setCameraPosition(cdMo);
274 sim.getImage(I, cam); // and aquire the image Id
275 Id = I;
276
277 // display the image
278#if defined(VISP_HAVE_DISPLAY)
279 if (opt_display) {
280#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
281 d = vpDisplayFactory::createDisplay(I, 20, 10, "Photometric visual servoing : s");
282#else
283 d = vpDisplayFactory::allocateDisplay(I, 20, 10, "Photometric visual servoing : s");
284#endif
287 }
288 if (opt_display && opt_click_allowed) {
289 std::cout << "Click in the image to continue..." << std::endl;
291 }
292#endif
293
294 // ----------------------------------------------------------
295 // position the robot at the initial position
296 // ----------------------------------------------------------
297
298 // camera desired position
300 cMo.buildFrom(0, 0, 1.2, vpMath::rad(15), vpMath::rad(-5), vpMath::rad(20));
301 vpHomogeneousMatrix wMo; // Set to identity
302 vpHomogeneousMatrix wMc; // Camera position in the world frame
303
304 // set the robot at the desired position
305 sim.setCameraPosition(cMo);
306 I = 0u;
307 sim.getImage(I, cam); // and aquire the image Id
308
309#if defined(VISP_HAVE_DISPLAY)
310 if (opt_display) {
313 }
314 if (opt_display && opt_click_allowed) {
315 std::cout << "Click in the image to continue..." << std::endl;
317 }
318#endif
319
321 Idiff = I;
322
323 vpImageTools::imageDifference(I, Id, Idiff);
324
325 // Affiche de l'image de difference
326#if defined(VISP_HAVE_DISPLAY)
327 if (opt_display) {
328#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
329 d1 = vpDisplayFactory::createDisplay(Idiff, 40 + static_cast<int>(I.getWidth()), 10, "photometric visual servoing : s-s* ");
330#else
331 d1 = vpDisplayFactory::allocateDisplay(Idiff, 40 + static_cast<int>(I.getWidth()), 10, "photometric visual servoing : s-s* ");
332#endif
333 vpDisplay::display(Idiff);
334 vpDisplay::flush(Idiff);
335 }
336#endif
337 // create the robot (here a simulated free flying camera)
338 vpSimulatorCamera robot;
339 robot.setSamplingTime(0.04);
340 wMc = wMo * cMo.inverse();
341 robot.setPosition(wMc);
342
343 // ------------------------------------------------------
344 // Visual feature, interaction matrix, error
345 // s, Ls, Lsd, Lt, Lp, etc
346 // ------------------------------------------------------
347
348 // current visual feature built from the image
349 // (actually, this is the image...)
351 sI.init(I.getHeight(), I.getWidth(), Z);
352 sI.setCameraParameters(cam);
353 sI.buildFrom(I);
354
355 // desired visual feature built from the image
357 sId.init(I.getHeight(), I.getWidth(), Z);
358 sId.setCameraParameters(cam);
359 sId.buildFrom(Id);
360
361 // Matrice d'interaction, Hessien, erreur,...
362 vpMatrix Lsd; // matrice d'interaction a la position desiree
363 vpMatrix Hsd; // hessien a la position desiree
364 vpMatrix H; // Hessien utilise pour le levenberg-Marquartd
365 vpColVector error; // Erreur I-I*
366
367 // Compute the interaction matrix
368 // link the variation of image intensity to camera motion
369
370 // here it is computed at the desired position
371 sId.interaction(Lsd);
372
373 // Compute the Hessian H = L^TL
374 Hsd = Lsd.AtA();
375
376 // Compute the Hessian diagonal for the Levenberg-Marquartd
377 // optimization process
378 unsigned int n = 6;
379 vpMatrix diagHsd(n, n);
380 diagHsd.eye(n);
381 for (unsigned int i = 0; i < n; i++)
382 diagHsd[i][i] = Hsd[i][i];
383
384 // ------------------------------------------------------
385 // Control law
386 double lambda; // gain
388 vpColVector v; // camera velocity send to the robot
389
390 // ----------------------------------------------------------
391 // minimization
392
393 double mu; // mu = 0 : Gauss Newton ; mu != 0 : LM
394 double lambdaGN;
395
396 mu = 0.01;
397 lambda = 30;
398 lambdaGN = 30;
399
400 // set a velocity control mode
401 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
402
403 // ----------------------------------------------------------
404 int iter = 1;
405 int iterGN = 90; // swicth to Gauss Newton after iterGN iterations
406
407 double normeError = 0;
408
409 vpChrono chrono;
410 chrono.start();
411 do {
412 std::cout << "--------------------------------------------" << iter++ << std::endl;
413
414 // Acquire the new image
415 sim.setCameraPosition(cMo);
416 sim.getImage(I, cam);
417#if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
418 if (opt_display) {
421 }
422#endif
423 vpImageTools::imageDifference(I, Id, Idiff);
424#if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
425 if (opt_display) {
426 vpDisplay::display(Idiff);
427 vpDisplay::flush(Idiff);
428 }
429#endif
430 // Compute current visual feature
431 sI.buildFrom(I);
432
433 // compute current error
434 sI.error(sId, error);
435
436 normeError = (error.sumSquare());
437 std::cout << "|e| " << normeError << std::endl;
438
439 // double t = vpTime::measureTimeMs() ;
440
441 // ---------- Levenberg Marquardt method --------------
442 {
443 if (iter > iterGN) {
444 mu = 0.0001;
445 lambda = lambdaGN;
446 }
447
448 // Compute the levenberg Marquartd term
449 {
450 H = ((mu * diagHsd) + Hsd).inverseByLU();
451 }
452 // Compute the control law
453 e = H * Lsd.t() * error;
454
455 v = -lambda * e;
456 }
457
458 std::cout << "lambda = " << lambda << " mu = " << mu;
459 std::cout << " |Tc| = " << sqrt(v.sumSquare()) << std::endl;
460
461 // send the robot velocity
462 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
463 wMc = robot.getPosition();
464 cMo = wMc.inverse() * wMo;
465 } while (normeError > 10000 && iter < opt_niter);
466
467 chrono.stop();
468 std::cout << "Time to convergence: " << chrono.getDurationMs() << " ms" << std::endl;
469
470 v = 0;
471 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
472
473#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
474 if (d != nullptr) {
475 delete d;
476 }
477 if (d1 != nullptr) {
478 delete d1;
479 }
480#endif
481 return EXIT_SUCCESS;
482 }
483 catch (const vpException &e) {
484 std::cout << "Catch an exception: " << e << std::endl;
485#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
486 if (d != nullptr) {
487 delete d;
488 }
489 if (d1 != nullptr) {
490 delete d1;
491 }
492#endif
493 return EXIT_FAILURE;
494 }
495#else
496 (void)argc;
497 (void)argv;
498 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
499 return EXIT_SUCCESS;
500#endif
501}
Generic class defining intrinsic camera parameters.
void start(bool reset=true)
Definition vpTime.cpp:411
void stop()
Definition vpTime.cpp:426
double getDurationMs()
Definition vpTime.cpp:400
Implementation of column vector and the associated operations.
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition vpException.h:60
Class that defines the image luminance visual feature.
vpFeatureLuminance & buildFrom(vpImage< unsigned char > &I)
vpColVector error(const vpBasicFeature &s_star, unsigned int select=FEATURE_ALL) VP_OVERRIDE
void init(unsigned int _nbr, unsigned int _nbc, double _Z)
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
void setCameraParameters(const vpCameraParameters &_cam)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class which enables to project an image in the 3D space and get the view of a virtual camera.
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
void init(const vpImage< unsigned char > &I, vpColVector *X)
void setInterpolationType(const vpInterpolationType interplt)
void setCameraPosition(const vpHomogeneousMatrix &cMt)
static void imageDifference(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static double rad(double deg)
Definition vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
vpMatrix AtA() const
vpMatrix t() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
virtual void setSamplingTime(const double &delta_t)
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
Class that defines the simplest robot: a free flying camera.
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.