ViSP  2.8.0
photometricVisualServoing.cpp
1 /****************************************************************************
2  *
3  * $Id: photometricVisualServoing.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
51 #include <visp/vpDebug.h>
52 
53 #include <visp/vpImage.h>
54 #include <visp/vpImageIo.h>
55 #include <visp/vpImageTools.h>
56 
57 #include <visp/vpCameraParameters.h>
58 #include <visp/vpTime.h>
59 #include <visp/vpRobotCamera.h>
60 
61 #include <visp/vpMath.h>
62 #include <visp/vpHomogeneousMatrix.h>
63 #include <visp/vpDisplayGTK.h>
64 #include <visp/vpDisplayGDI.h>
65 #include <visp/vpDisplayOpenCV.h>
66 #include <visp/vpDisplayD3D.h>
67 #include <visp/vpDisplayX.h>
68 
69 #include <visp/vpFeatureLuminance.h>
70 #include <visp/vpParseArgv.h>
71 
72 #include <visp/vpImageSimulator.h>
73 #include <stdlib.h>
74 #define Z 1
75 
76 #include <visp/vpParseArgv.h>
77 #include <visp/vpIoTools.h>
78 
79 // List of allowed command line options
80 #define GETOPTARGS "cdi:n:h"
81 
92 void usage(const char *name, const char *badparam, std::string ipath, int niter)
93 {
94  fprintf(stdout, "\n\
95 Tracking of Surf key-points.\n\
96 \n\
97 SYNOPSIS\n\
98  %s [-i <input image path>] [-c] [-d] [-n <number of iterations>] [-h]\n", name);
99 
100  fprintf(stdout, "\n\
101 OPTIONS: Default\n\
102  -i <input image path> %s\n\
103  Set image input path.\n\
104  From this path read \"ViSP-images/doisneau/doisneau.jpg\"\n\
105  images. \n\
106  Setting the VISP_INPUT_IMAGE_PATH environment\n\
107  variable produces the same behaviour than using\n\
108  this option.\n\
109 \n\
110  -c\n\
111  Disable the mouse click. Useful to automaze the \n\
112  execution of this program without humain intervention.\n\
113 \n\
114  -d \n\
115  Turn off the display.\n\
116 \n\
117  -n %%d %d\n\
118  Number of iterations.\n\
119 \n\
120  -h\n\
121  Print the help.\n",
122  ipath.c_str(), niter);
123 
124  if (badparam)
125  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
126 }
141 bool getOptions(int argc, const char **argv, std::string &ipath,
142  bool &click_allowed, bool &display, int &niter)
143 {
144  const char *optarg;
145  int c;
146  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
147 
148  switch (c) {
149  case 'c': click_allowed = false; break;
150  case 'd': display = false; break;
151  case 'i': ipath = optarg; break;
152  case 'n': niter = atoi(optarg); break;
153  case 'h': usage(argv[0], NULL, ipath, niter); return false; break;
154 
155  default:
156  usage(argv[0], optarg, ipath, niter);
157  return false; break;
158  }
159  }
160 
161  if ((c == 1) || (c == -1)) {
162  // standalone param or error
163  usage(argv[0], NULL, 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 
172 
173 
174 int
175 main(int argc, const char ** argv)
176 {
177  std::string env_ipath;
178  std::string opt_ipath;
179  std::string ipath;
180  std::string filename;
181  bool opt_click_allowed = true;
182  bool opt_display = true;
183  int opt_niter = 400;
184 
185  // Get the VISP_IMAGE_PATH environment variable value
186  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
187  if (ptenv != NULL)
188  env_ipath = ptenv;
189 
190  // Set the default input path
191  if (! env_ipath.empty())
192  ipath = env_ipath;
193 
194 
195  // Read the command line options
196  if (getOptions(argc, argv, opt_ipath, opt_click_allowed,
197  opt_display, opt_niter) == false) {
198  return (-1);
199  }
200 
201  // Get the option values
202  if (!opt_ipath.empty())
203  ipath = opt_ipath;
204 
205  // Compare ipath and env_ipath. If they differ, we take into account
206  // the input path comming from the command line option
207  if (!opt_ipath.empty() && !env_ipath.empty()) {
208  if (ipath != env_ipath) {
209  std::cout << std::endl
210  << "WARNING: " << std::endl;
211  std::cout << " Since -i <visp image path=" << ipath << "> "
212  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
213  << " we skip the environment variable." << std::endl;
214  }
215  }
216 
217  // Test if an input path is set
218  if (opt_ipath.empty() && env_ipath.empty()){
219  usage(argv[0], NULL, ipath, opt_niter);
220  std::cerr << std::endl
221  << "ERROR:" << std::endl;
222  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
223  << std::endl
224  << " environment variable to specify the location of the " << std::endl
225  << " image path where test images are located." << std::endl << std::endl;
226  exit(-1);
227  }
228 
229  vpImage<unsigned char> Itexture ;
230  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
231  vpImageIo::read(Itexture,filename) ;
232 
233  vpColVector X[4];
234  for (int i = 0; i < 4; i++) X[i].resize(3);
235  // Top left corner
236  X[0][0] = -0.3;
237  X[0][1] = -0.215;
238  X[0][2] = 0;
239 
240  // Top right corner
241  X[1][0] = 0.3;
242  X[1][1] = -0.215;
243  X[1][2] = 0;
244 
245  // Bottom right corner
246  X[2][0] = 0.3;
247  X[2][1] = 0.215;
248  X[2][2] = 0;
249 
250  //Bottom left corner
251  X[3][0] = -0.3;
252  X[3][1] = 0.215;
253  X[3][2] = 0;
254 
255  vpImageSimulator sim;
256 
258  sim.init(Itexture, X);
259 
260 
261 
262 
263  vpCameraParameters cam(870, 870, 160, 120);
264 
265  // ----------------------------------------------------------
266  // Create the framegraber (here a simulated image)
267  vpImage<unsigned char> I(240,320,0) ;
269 
270  //camera desired position
271  vpHomogeneousMatrix cdMo ;
272  cdMo[2][3] = 1 ;
273 
274  //set the robot at the desired position
275  sim.setCameraPosition(cdMo) ;
276  sim.getImage(I,cam); // and aquire the image Id
277  Id = I ;
278 
279 
280  // display the image
281 #if defined VISP_HAVE_X11
282  vpDisplayX d;
283 #elif defined VISP_HAVE_GDI
284  vpDisplayGDI d;
285 #elif defined VISP_HAVE_GTK
286  vpDisplayGTK d;
287 #endif
288 
289 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
290  if (opt_display) {
291  d.init(I, 20, 10, "Photometric visual servoing : s") ;
293  vpDisplay::flush(I);
294  }
295  if (opt_display && opt_click_allowed) {
296  std::cout << "Click in the image to continue..." << std::endl;
298  }
299 #endif
300 
301 
302  // ----------------------------------------------------------
303  // position the robot at the initial position
304  // ----------------------------------------------------------
305 
306  //camera desired position
307  vpHomogeneousMatrix cMo ;
308  cMo.buildFrom(0,0,1.2,vpMath::rad(15),vpMath::rad(-5),vpMath::rad(20));
309 
310  //set the robot at the desired position
311  sim.setCameraPosition(cMo) ;
312  I =0 ;
313  sim.getImage(I,cam); // and aquire the image Id
314 
315 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
316  if (opt_display) {
317  vpDisplay::display(I) ;
318  vpDisplay::flush(I) ;
319  }
320  if (opt_display && opt_click_allowed) {
321  std::cout << "Click in the image to continue..." << std::endl;
323  }
324 #endif
325 
326  vpImage<unsigned char> Idiff ;
327  Idiff = I ;
328 
329 
330  vpImageTools::imageDifference(I,Id,Idiff) ;
331 
332 
333  // Affiche de l'image de difference
334 #if defined VISP_HAVE_X11
335  vpDisplayX d1;
336 #elif defined VISP_HAVE_GDI
337  vpDisplayGDI d1;
338 #elif defined VISP_HAVE_GTK
339  vpDisplayGTK d1;
340 #endif
341 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
342  if (opt_display) {
343  d1.init(Idiff, 40+(int)I.getWidth(), 10, "photometric visual servoing : s-s* ") ;
344  vpDisplay::display(Idiff) ;
345  vpDisplay::flush(Idiff) ;
346  }
347 #endif
348  // create the robot (here a simulated free flying camera)
349  vpRobotCamera robot ;
350  robot.setSamplingTime(0.04);
351  robot.setPosition(cMo) ;
352 
353  // ------------------------------------------------------
354  // Visual feature, interaction matrix, error
355  // s, Ls, Lsd, Lt, Lp, etc
356  // ------------------------------------------------------
357 
358  // current visual feature built from the image
359  // (actually, this is the image...)
360  vpFeatureLuminance sI ;
361  sI.init( I.getHeight(), I.getWidth(), Z) ;
362  sI.setCameraParameters(cam) ;
363  sI.buildFrom(I) ;
364 
365 
366  // desired visual feature built from the image
367  vpFeatureLuminance sId ;
368  sId.init(I.getHeight(), I.getWidth(), Z) ;
369  sId.setCameraParameters(cam) ;
370  sId.buildFrom(Id) ;
371 
372 
373 
374  // Matrice d'interaction, Hessien, erreur,...
375  vpMatrix Lsd; // matrice d'interaction a la position desiree
376  vpMatrix Hsd; // hessien a la position desiree
377  vpMatrix H ; // Hessien utilise pour le levenberg-Marquartd
378  vpColVector error ; // Erreur I-I*
379 
380  // Compute the interaction matrix
381  // link the variation of image intensity to camera motion
382 
383  // here it is computed at the desired position
384  sId.interaction(Lsd) ;
385 
386 
387  // Compute the Hessian H = L^TL
388  Hsd = Lsd.AtA() ;
389 
390  // Compute the Hessian diagonal for the Levenberg-Marquartd
391  // optimization process
392  unsigned int n = 6 ;
393  vpMatrix diagHsd(n,n) ;
394  diagHsd.eye(n);
395  for(unsigned int i = 0 ; i < n ; i++) diagHsd[i][i] = Hsd[i][i];
396 
397 
398 
399  // ------------------------------------------------------
400  // Control law
401  double lambda ; //gain
402  vpColVector e ;
403  vpColVector v ; // camera velocity send to the robot
404 
405 
406  // ----------------------------------------------------------
407  // Minimisation
408 
409  double mu ; // mu = 0 : Gauss Newton ; mu != 0 : LM
410  double lambdaGN;
411 
412 
413  mu = 0.01;
414  lambda = 30 ;
415  lambdaGN = 30;
416 
417 
418 
419 
420 
421 
422  // set a velocity control mode
424 
425  // ----------------------------------------------------------
426  int iter = 1;
427  int iterGN = 90 ; // swicth to Gauss Newton after iterGN iterations
428 
429  double normeError = 0;
430  do
431  {
432 
433  std::cout << "--------------------------------------------" << iter++ << std::endl ;
434 
435 
436  // Acquire the new image
437  sim.setCameraPosition(cMo) ;
438  sim.getImage(I,cam) ;
439 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
440  if (opt_display) {
441  vpDisplay::display(I) ;
442  vpDisplay::flush(I) ;
443  }
444 #endif
445  vpImageTools::imageDifference(I,Id,Idiff) ;
446 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_GTK)
447  if (opt_display) {
448  vpDisplay::display(Idiff) ;
449  vpDisplay::flush(Idiff) ;
450  }
451 #endif
452  // Compute current visual feature
453  sI.buildFrom(I) ;
454 
455  // compute current error
456  sI.error(sId,error) ;
457 
458  normeError = (error.sumSquare());
459  std::cout << "|e| "<<normeError <<std::endl ;
460 
461  // double t = vpTime::measureTimeMs() ;
462 
463  // ---------- Levenberg Marquardt method --------------
464  {
465  if (iter > iterGN)
466  {
467  mu = 0.0001 ;
468  lambda = lambdaGN;
469  }
470 
471  // Compute the levenberg Marquartd term
472  {
473  H = ((mu * diagHsd) + Hsd).inverseByLU();
474  }
475  // compute the control law
476  e = H * Lsd.t() *error ;
477 
478  v = - lambda*e;
479  }
480 
481  std::cout << "lambda = " << lambda << " mu = " << mu ;
482  std::cout << " |Tc| = " << sqrt(v.sumSquare()) << std::endl;
483 
484  // send the robot velocity
486  robot.getPosition(cMo) ;
487 
488  }
489  while(normeError > 10000 && iter < opt_niter);
490 
491  v = 0 ;
493 
494 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
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:133
void setCameraParameters(vpCameraParameters &_cam)
Define the X11 console to display images.
Definition: vpDisplayX.h:152
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:760
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
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:154
virtual void setSamplingTime(const double &delta_t)
Initialize the velocity controller.
Definition: vpRobot.h:70
vpMatrix AtA() const
Definition: vpMatrix.cpp:1359
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:203
Generic class defining intrinsic camera parameters.
void init(const vpImage< unsigned char > &I, vpColVector *_X)
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 setInterpolationType(const vpInterpolationType interp)
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:1176
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
void setCameraPosition(const vpHomogeneousMatrix &_cMt)
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:277
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)