ViSP  2.9.0
photometricVisualServoing.cpp
1 /****************************************************************************
2  *
3  * $Id: photometricVisualServoing.cpp 4664 2014-02-16 16:17:54Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  *
35  * Authors:
36  * Eric Marchand
37  * Christophe Collewet
38  *
39  *****************************************************************************/
40 
48 #include <visp/vpDebug.h>
49 
50 #include <visp/vpImage.h>
51 #include <visp/vpImageIo.h>
52 #include <visp/vpImageTools.h>
53 
54 #include <visp/vpCameraParameters.h>
55 #include <visp/vpTime.h>
56 #include <visp/vpRobotCamera.h>
57 
58 #include <visp/vpMath.h>
59 #include <visp/vpHomogeneousMatrix.h>
60 #include <visp/vpDisplayGTK.h>
61 #include <visp/vpDisplayGDI.h>
62 #include <visp/vpDisplayOpenCV.h>
63 #include <visp/vpDisplayD3D.h>
64 #include <visp/vpDisplayX.h>
65 
66 #include <visp/vpFeatureLuminance.h>
67 #include <visp/vpParseArgv.h>
68 
69 #include <visp/vpImageSimulator.h>
70 #include <stdlib.h>
71 #define Z 1
72 
73 #include <visp/vpParseArgv.h>
74 #include <visp/vpIoTools.h>
75 
76 // List of allowed command line options
77 #define GETOPTARGS "cdi:n:h"
78 
79 void usage(const char *name, const char *badparam, std::string ipath, int niter);
80 bool getOptions(int argc, const char **argv, std::string &ipath,
81  bool &click_allowed, bool &display, int &niter);
82 
93 void usage(const char *name, const char *badparam, std::string ipath, int niter)
94 {
95  fprintf(stdout, "\n\
96 Tracking of Surf key-points.\n\
97 \n\
98 SYNOPSIS\n\
99  %s [-i <input image path>] [-c] [-d] [-n <number of iterations>] [-h]\n", name);
100 
101  fprintf(stdout, "\n\
102 OPTIONS: Default\n\
103  -i <input image path> %s\n\
104  Set image input path.\n\
105  From this path read \"ViSP-images/doisneau/doisneau.jpg\"\n\
106  images. \n\
107  Setting the VISP_INPUT_IMAGE_PATH environment\n\
108  variable produces the same behaviour than using\n\
109  this option.\n\
110 \n\
111  -c\n\
112  Disable the mouse click. Useful to automaze the \n\
113  execution of this program without humain intervention.\n\
114 \n\
115  -d \n\
116  Turn off the display.\n\
117 \n\
118  -n %%d %d\n\
119  Number of iterations.\n\
120 \n\
121  -h\n\
122  Print the help.\n",
123  ipath.c_str(), niter);
124 
125  if (badparam)
126  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
127 }
142 bool getOptions(int argc, const char **argv, std::string &ipath,
143  bool &click_allowed, bool &display, int &niter)
144 {
145  const char *optarg_;
146  int c;
147  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
148 
149  switch (c) {
150  case 'c': click_allowed = false; break;
151  case 'd': display = false; break;
152  case 'i': ipath = optarg_; break;
153  case 'n': niter = atoi(optarg_); break;
154  case 'h': usage(argv[0], NULL, ipath, niter); return false; break;
155 
156  default:
157  usage(argv[0], optarg_, ipath, niter);
158  return false; break;
159  }
160  }
161 
162  if ((c == 1) || (c == -1)) {
163  // standalone param or error
164  usage(argv[0], NULL, ipath, niter);
165  std::cerr << "ERROR: " << std::endl;
166  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
167  return false;
168  }
169 
170  return true;
171 }
172 
173 
174 
175 int
176 main(int argc, const char ** argv)
177 {
178  try {
179  std::string env_ipath;
180  std::string opt_ipath;
181  std::string ipath;
182  std::string filename;
183  bool opt_click_allowed = true;
184  bool opt_display = true;
185  int opt_niter = 400;
186 
187  // Get the VISP_IMAGE_PATH environment variable value
188  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
189  if (ptenv != NULL)
190  env_ipath = ptenv;
191 
192  // Set the default input path
193  if (! env_ipath.empty())
194  ipath = env_ipath;
195 
196 
197  // Read the command line options
198  if (getOptions(argc, argv, opt_ipath, opt_click_allowed,
199  opt_display, opt_niter) == false) {
200  return (-1);
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 comming from the command line option
209  if (!opt_ipath.empty() && !env_ipath.empty()) {
210  if (ipath != env_ipath) {
211  std::cout << std::endl
212  << "WARNING: " << std::endl;
213  std::cout << " Since -i <visp image path=" << ipath << "> "
214  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
215  << " we skip the environment variable." << std::endl;
216  }
217  }
218 
219  // Test if an input path is set
220  if (opt_ipath.empty() && env_ipath.empty()){
221  usage(argv[0], NULL, ipath, opt_niter);
222  std::cerr << std::endl
223  << "ERROR:" << std::endl;
224  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
225  << std::endl
226  << " environment variable to specify the location of the " << std::endl
227  << " image path where test images are located." << std::endl << std::endl;
228  exit(-1);
229  }
230 
231  vpImage<unsigned char> Itexture ;
232  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
233  vpImageIo::read(Itexture,filename) ;
234 
235  vpColVector X[4];
236  for (int i = 0; i < 4; i++) X[i].resize(3);
237  // Top left corner
238  X[0][0] = -0.3;
239  X[0][1] = -0.215;
240  X[0][2] = 0;
241 
242  // Top right corner
243  X[1][0] = 0.3;
244  X[1][1] = -0.215;
245  X[1][2] = 0;
246 
247  // Bottom right corner
248  X[2][0] = 0.3;
249  X[2][1] = 0.215;
250  X[2][2] = 0;
251 
252  //Bottom left corner
253  X[3][0] = -0.3;
254  X[3][1] = 0.215;
255  X[3][2] = 0;
256 
257  vpImageSimulator sim;
258 
260  sim.init(Itexture, X);
261 
262 
263 
264 
265  vpCameraParameters cam(870, 870, 160, 120);
266 
267  // ----------------------------------------------------------
268  // Create the framegraber (here a simulated image)
269  vpImage<unsigned char> I(240,320,0) ;
271 
272  //camera desired position
273  vpHomogeneousMatrix cdMo ;
274  cdMo[2][3] = 1 ;
275 
276  //set the robot at the desired position
277  sim.setCameraPosition(cdMo) ;
278  sim.getImage(I,cam); // and aquire the image Id
279  Id = I ;
280 
281 
282  // display the image
283 #if defined VISP_HAVE_X11
284  vpDisplayX d;
285 #elif defined VISP_HAVE_GDI
286  vpDisplayGDI d;
287 #elif defined VISP_HAVE_GTK
288  vpDisplayGTK d;
289 #endif
290 
291 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
292  if (opt_display) {
293  d.init(I, 20, 10, "Photometric visual servoing : s") ;
295  vpDisplay::flush(I);
296  }
297  if (opt_display && opt_click_allowed) {
298  std::cout << "Click in the image to continue..." << std::endl;
300  }
301 #endif
302 
303 
304  // ----------------------------------------------------------
305  // position the robot at the initial position
306  // ----------------------------------------------------------
307 
308  //camera desired position
309  vpHomogeneousMatrix cMo ;
310  cMo.buildFrom(0,0,1.2,vpMath::rad(15),vpMath::rad(-5),vpMath::rad(20));
311 
312  //set the robot at the desired position
313  sim.setCameraPosition(cMo) ;
314  I =0 ;
315  sim.getImage(I,cam); // and aquire the image Id
316 
317 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
318  if (opt_display) {
319  vpDisplay::display(I) ;
320  vpDisplay::flush(I) ;
321  }
322  if (opt_display && opt_click_allowed) {
323  std::cout << "Click in the image to continue..." << std::endl;
325  }
326 #endif
327 
328  vpImage<unsigned char> Idiff ;
329  Idiff = I ;
330 
331 
332  vpImageTools::imageDifference(I,Id,Idiff) ;
333 
334 
335  // Affiche de l'image de difference
336 #if defined VISP_HAVE_X11
337  vpDisplayX d1;
338 #elif defined VISP_HAVE_GDI
339  vpDisplayGDI d1;
340 #elif defined VISP_HAVE_GTK
341  vpDisplayGTK d1;
342 #endif
343 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
344  if (opt_display) {
345  d1.init(Idiff, 40+(int)I.getWidth(), 10, "photometric visual servoing : s-s* ") ;
346  vpDisplay::display(Idiff) ;
347  vpDisplay::flush(Idiff) ;
348  }
349 #endif
350  // create the robot (here a simulated free flying camera)
351  vpRobotCamera robot ;
352  robot.setSamplingTime(0.04);
353  robot.setPosition(cMo) ;
354 
355  // ------------------------------------------------------
356  // Visual feature, interaction matrix, error
357  // s, Ls, Lsd, Lt, Lp, etc
358  // ------------------------------------------------------
359 
360  // current visual feature built from the image
361  // (actually, this is the image...)
362  vpFeatureLuminance sI ;
363  sI.init( I.getHeight(), I.getWidth(), Z) ;
364  sI.setCameraParameters(cam) ;
365  sI.buildFrom(I) ;
366 
367 
368  // desired visual feature built from the image
369  vpFeatureLuminance sId ;
370  sId.init(I.getHeight(), I.getWidth(), Z) ;
371  sId.setCameraParameters(cam) ;
372  sId.buildFrom(Id) ;
373 
374 
375 
376  // Matrice d'interaction, Hessien, erreur,...
377  vpMatrix Lsd; // matrice d'interaction a la position desiree
378  vpMatrix Hsd; // hessien a la position desiree
379  vpMatrix H ; // Hessien utilise pour le levenberg-Marquartd
380  vpColVector error ; // Erreur I-I*
381 
382  // Compute the interaction matrix
383  // link the variation of image intensity to camera motion
384 
385  // here it is computed at the desired position
386  sId.interaction(Lsd) ;
387 
388 
389  // Compute the Hessian H = L^TL
390  Hsd = Lsd.AtA() ;
391 
392  // Compute the Hessian diagonal for the Levenberg-Marquartd
393  // optimization process
394  unsigned int n = 6 ;
395  vpMatrix diagHsd(n,n) ;
396  diagHsd.eye(n);
397  for(unsigned int i = 0 ; i < n ; i++) diagHsd[i][i] = Hsd[i][i];
398 
399 
400 
401  // ------------------------------------------------------
402  // Control law
403  double lambda ; //gain
404  vpColVector e ;
405  vpColVector v ; // camera velocity send to the robot
406 
407 
408  // ----------------------------------------------------------
409  // Minimisation
410 
411  double mu ; // mu = 0 : Gauss Newton ; mu != 0 : LM
412  double lambdaGN;
413 
414 
415  mu = 0.01;
416  lambda = 30 ;
417  lambdaGN = 30;
418 
419 
420 
421 
422 
423 
424  // set a velocity control mode
426 
427  // ----------------------------------------------------------
428  int iter = 1;
429  int iterGN = 90 ; // swicth to Gauss Newton after iterGN iterations
430 
431  double normeError = 0;
432  do
433  {
434 
435  std::cout << "--------------------------------------------" << iter++ << std::endl ;
436 
437 
438  // Acquire the new image
439  sim.setCameraPosition(cMo) ;
440  sim.getImage(I,cam) ;
441 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
442  if (opt_display) {
443  vpDisplay::display(I) ;
444  vpDisplay::flush(I) ;
445  }
446 #endif
447  vpImageTools::imageDifference(I,Id,Idiff) ;
448 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
449  if (opt_display) {
450  vpDisplay::display(Idiff) ;
451  vpDisplay::flush(Idiff) ;
452  }
453 #endif
454  // Compute current visual feature
455  sI.buildFrom(I) ;
456 
457  // compute current error
458  sI.error(sId,error) ;
459 
460  normeError = (error.sumSquare());
461  std::cout << "|e| "<<normeError <<std::endl ;
462 
463  // double t = vpTime::measureTimeMs() ;
464 
465  // ---------- Levenberg Marquardt method --------------
466  {
467  if (iter > iterGN)
468  {
469  mu = 0.0001 ;
470  lambda = lambdaGN;
471  }
472 
473  // Compute the levenberg Marquartd term
474  {
475  H = ((mu * diagHsd) + Hsd).inverseByLU();
476  }
477  // compute the control law
478  e = H * Lsd.t() *error ;
479 
480  v = - lambda*e;
481  }
482 
483  std::cout << "lambda = " << lambda << " mu = " << mu ;
484  std::cout << " |Tc| = " << sqrt(v.sumSquare()) << std::endl;
485 
486  // send the robot velocity
488  robot.getPosition(cMo) ;
489 
490  }
491  while(normeError > 10000 && iter < opt_niter);
492 
493  v = 0 ;
495 
496  return 0;
497  }
498  catch(vpException e) {
499  std::cout << "Catch an exception: " << e << std::endl;
500  return 1;
501  }
502 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void init(const vpImage< unsigned char > &I, vpColVector *X)
unsigned int getWidth() const
Definition: vpImage.h:159
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void buildFrom(vpImage< unsigned char > &I)
void setPosition(const vpHomogeneousMatrix &cMw)
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void setCameraParameters(vpCameraParameters &_cam)
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static void imageDifference(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:809
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:190
virtual void setSamplingTime(const double &delta_t)
void setCameraPosition(const vpHomogeneousMatrix &cMt)
Initialize the velocity controller.
Definition: vpRobot.h:70
void setInterpolationType(const vpInterpolationType interplt)
vpMatrix AtA() const
Definition: vpMatrix.cpp:1408
Class that defines the image luminance visual feature.
Class that defines the simplest robot: a free flying camera.
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
Generic class defining intrinsic camera parameters.
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
Class which enables to project an image in the 3D space and get the view of a virtual camera...
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
static double rad(double deg)
Definition: vpMath.h:100
void getPosition(vpHomogeneousMatrix &cMw) const
vpMatrix t() const
Definition: vpMatrix.cpp:1225
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpMatrix inverseByLU() const
unsigned int getHeight() const
Definition: vpImage.h:150
virtual bool getClick(bool blocking=true)=0
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)