Visual Servoing Platform  version 3.6.1 under development (2025-03-03)
AROgreBasic.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  * Implementation of a simple augmented reality application using the vpAROgre
32  * class.
33  */
34 
40 #include <iostream>
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_OGRE) && defined(VISP_HAVE_DISPLAY)
44 
45 #include <visp3/ar/vpAROgre.h>
46 #include <visp3/blob/vpDot2.h>
47 #include <visp3/core/vpDebug.h>
48 #include <visp3/core/vpImagePoint.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/core/vpPixelMeterConversion.h>
51 #include <visp3/core/vpPoint.h>
52 #include <visp3/gui/vpDisplayFactory.h>
53 #include <visp3/io/vpParseArgv.h>
54 #include <visp3/io/vpVideoReader.h>
55 #include <visp3/vision/vpPose.h>
56 
57 // List of allowed command line options
58 #define GETOPTARGS "cdi:p:h"
59 
60 #ifdef ENABLE_VISP_NAMESPACE
61 using namespace VISP_NAMESPACE_NAME;
62 #endif
63 
74 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
75 {
76 #if defined(VISP_HAVE_DATASET)
77 #if VISP_HAVE_DATASET_VERSION >= 0x030600
78  std::string ext("png");
79 #else
80  std::string ext("pgm");
81 #endif
82 #else
83  // We suppose that the user will download a recent dataset
84  std::string ext("png");
85 #endif
86 
87  fprintf(stdout, "\n\
88 Test augmented reality using the vpAROgre class.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-i <test image path>] [-p <personal image path>]\n\
92  [-c] [-h]\n", name);
93 
94  fprintf(stdout, "\n\
95 OPTIONS: Default\n\
96  -i <input image path> %s\n\
97  Set image input path.\n\
98  From this path read images \n\
99  \"mire-2/image.%%04d.%s\". These \n\
100  images come from ViSP-images-x.y.z.tar.gz available \n\
101  on the ViSP website.\n\
102  Setting the VISP_INPUT_IMAGE_PATH environment\n\
103  variable produces the same behaviour than using\n\
104  this option.\n\
105  \n\
106  -p <personal image path> %s\n\
107  Specify a personal sequence containing images \n\
108  to process.\n\
109  By image sequence, we mean one file per image.\n\
110  Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
111  %%04d is for the image numbering.\n\
112 \n\
113  -c\n\
114  Disable the mouse click. Useful to automate the \n\
115  execution of this program without human intervention.\n\
116 \n\
117  -d\n\
118  Disable the display.\n\
119 \n\
120  -h\n\
121  Print the help.\n",
122  ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str());
123 
124  if (badparam)
125  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
126 }
127 
140 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, bool &click_allowed, bool &use_display)
141 {
142  const char *optarg_;
143  int c;
144  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
145 
146  switch (c) {
147  case 'c':
148  click_allowed = false;
149  break;
150  case 'd':
151  use_display = false;
152  break;
153  case 'i':
154  ipath = optarg_;
155  break;
156  case 'p':
157  ppath = optarg_;
158  break;
159  case 'h':
160  usage(argv[0], nullptr, ipath, ppath);
161  return false;
162  break;
163 
164  default:
165  usage(argv[0], optarg_, ipath, ppath);
166  return false;
167  break;
168  }
169  }
170 
171  if ((c == 1) || (c == -1)) {
172  // standalone param or error
173  usage(argv[0], nullptr, ipath, ppath);
174  std::cerr << "ERROR: " << std::endl;
175  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
176  return false;
177  }
178 
179  return true;
180 }
181 
188 void computeInitialPose(vpCameraParameters *mcam, vpImage<unsigned char> &I, vpPose *mPose, vpDot2 *md,
189  vpImagePoint *mcog, vpHomogeneousMatrix *cMo, vpPoint *mP, const bool &opt_click_allowed, bool opt_display)
190 {
191  vpDisplay *display = nullptr;
192  if (opt_display) {
193 #if defined(VISP_HAVE_DISPLAY)
195 #else
196  opt_display = false; // No display is available
197 #endif
198  }
199 
200  for (unsigned int i = 0; i < 4; ++i) {
201  if (opt_display) {
202  md[i].setGraphics(true);
203  }
204  else {
205  md[i].setGraphics(false);
206  }
207  }
208 
209  if (opt_display) {
210  // Display size is automatically defined by the image (I) size
211  display->init(I, 100, 100, "Preliminary Pose Calculation");
212  // display the image
213  // The image class has a member that specify a pointer toward
214  // the display that has been initialized in the display declaration
215  // therefore is is no longer necessary to make a reference to the
216  // display variable.
218  // Flush the display
219  vpDisplay::flush(I);
220  }
221 
222  std::cout << "**"<< std::endl;
223  std::cout << "** Preliminary Pose Calculation" << std::endl;
224  std::cout << "** Click on the 4 dots" << std::endl;
225  std::cout << "** Dot1: (-x,-y,0), Dot2: (x,-y,0), Dot3: (x,y,0), Dot4: (-x,y,0)" << std::endl;
226  std::cout << "**" << std::endl;
227 
228  vpImagePoint ip[4];
229  if (!opt_click_allowed) {
230  ip[0].set_i(265);
231  ip[0].set_j(93);
232  ip[1].set_i(248);
233  ip[1].set_j(242);
234  ip[2].set_i(166);
235  ip[2].set_j(215);
236  ip[3].set_i(178);
237  ip[3].set_j(85);
238  }
239 
240  for (unsigned int i = 0; i < 4; ++i) {
241  // by using setGraphics, we request to see the edges of the dot
242  // in red on the screen.
243  // It uses the overlay image plane.
244  // The default of this setting is that it is time consuming
245 
246  md[i].setGraphics(true);
247  md[i].setGrayLevelPrecision(0.7);
248  md[i].setSizePrecision(0.5);
249 
250  for (unsigned int j = 0; j < i; j++)
251  md[j].display(I);
252 
253  // flush the display buffer
254  if (opt_display) {
255  vpDisplay::flush(I);
256  }
257  try {
258  if (opt_click_allowed && opt_display) {
259  md[i].initTracking(I);
260  // std::cout << "click " << i << " " << md[i] << std::endl;
261  }
262  else {
263  md[i].initTracking(I, ip[i]);
264  }
265  }
266  catch (...) {
267  }
268 
269  mcog[i] = md[i].getCog();
270  // an exception is thrown by the track method if
271  // - dot is lost
272  // - the number of pixel is too small
273  // - too many pixels are detected (this is usual when a "big"
274  // specularity
275  // occurs. The threshold can be modified using the
276  // setNbMaxPoint(int) method
277  if (opt_display) {
278  md[i].display(I);
279  // flush the display buffer
280  vpDisplay::flush(I);
281  }
282  }
283 
284  if (opt_display) {
285  // display a red cross (size 10) in the image at the dot center
286  // of gravity location
287  //
288  // WARNING
289  // in the vpDisplay class member's when pixel coordinates
290  // are considered the first element is the row index and the second
291  // is the column index:
292  // vpDisplay::displayCross(Image, row index, column index, size, color)
293  // therefore u and v are inverted wrt to the vpDot specification
294  // Alternatively, to avoid this problem another set of member have
295  // been defined in the vpDisplay class.
296  // If the method name is postfixe with _uv the specification is :
297  // vpDisplay::displayCross_uv(Image, column index, row index, size,
298  // color)
299 
300  for (unsigned int i = 0; i < 4; ++i) {
301  vpDisplay::displayCross(I, mcog[i], 10, vpColor::red);
302  }
303 
304  // flush the X11 buffer
305  vpDisplay::flush(I);
306  }
307 
308  // --------------------------------------------------------
309  // Now we will compute the pose
310  // --------------------------------------------------------
311 
312  // the list of point is cleared (if that's not done before)
313  mPose->clearPoint();
314 
315  // we set the 3D points coordinates (in meter !) in the object/world frame
316  double l = 0.06;
317  double L = 0.07;
318  mP[0].setWorldCoordinates(-L, -l, 0); // (X,Y,Z)
319  mP[1].setWorldCoordinates(L, -l, 0);
320  mP[2].setWorldCoordinates(L, l, 0);
321  mP[3].setWorldCoordinates(-L, l, 0);
322 
323  // pixel-> meter conversion
324  for (unsigned int i = 0; i < 4; ++i) {
325  // u[i]. v[i] are expressed in pixel
326  // conversion in meter is achieved using
327  // x = (u-u0)/px
328  // y = (v-v0)/py
329  // where px, py, u0, v0 are the intrinsic camera parameters
330  double x = 0, y = 0;
331  vpPixelMeterConversion::convertPoint(*mcam, mcog[i], x, y);
332  mP[i].set_x(x);
333  mP[i].set_y(y);
334  }
335 
336  // The pose structure is build, we put in the point list the set of point
337  // here both 2D and 3D world coordinates are known
338  for (unsigned int i = 0; i < 4; ++i) {
339  mPose->addPoint(mP[i]); // and added to the pose computation point list
340  }
341 
342  // compute the initial pose using Dementhon method followed by a non linear
343  // minimization method
344 
345  // Compute initial pose
347 
348  // Display briefly just to get an overview of the ViSP pose
349  if (opt_display) {
350  // Display the computed pose
351  mPose->display(I, *cMo, *mcam, 0.05, vpColor::red);
352  vpDisplay::flush(I);
353  vpTime::wait(1000);
354  }
355 
356  if (opt_display && display != nullptr) {
357  delete display;
358  }
359 }
360 
361 int main(int argc, const char **argv)
362 {
363 #if defined(VISP_HAVE_DATASET)
364 #if VISP_HAVE_DATASET_VERSION >= 0x030600
365  std::string ext("png");
366 #else
367  std::string ext("pgm");
368 #endif
369 #else
370  // We suppose that the user will download a recent dataset
371  std::string ext("png");
372 #endif
373 
374  try {
375  std::string env_ipath;
376  std::string opt_ipath;
377  std::string ipath;
378  std::string opt_ppath;
379  std::string dirname;
380  std::string filename;
381  bool opt_click_allowed = true;
382  bool opt_display = true;
383 
384  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
385  // environment variable value
386  env_ipath = vpIoTools::getViSPImagesDataPath();
387 
388  // Set the default input path
389  if (!env_ipath.empty())
390  ipath = env_ipath;
391 
392  // Read the command line options
393  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed, opt_display) == false) {
394  return EXIT_FAILURE;
395  }
396 
397  // Get the option values
398  if (!opt_ipath.empty())
399  ipath = opt_ipath;
400 
401  // Compare ipath and env_ipath. If they differ, we take into account
402  // the input path coming from the command line option
403  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
404  if (ipath != env_ipath) {
405  std::cout << std::endl << "WARNING: " << std::endl;
406  std::cout << " Since -i <visp image path=" << ipath << "> "
407  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
408  << " we skip the environment variable." << std::endl;
409  }
410  }
411 
412  // Test if an input path is set
413  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
414  usage(argv[0], nullptr, ipath, opt_ppath);
415  std::cerr << std::endl << "ERROR:" << std::endl;
416  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
417  << " environment variable to specify the location of the " << std::endl
418  << " image path where test images are located." << std::endl
419  << " Use -p <personal image path> option if you want to " << std::endl
420  << " use personal images." << std::endl
421  << std::endl;
422 
423  return EXIT_FAILURE;
424  }
425 
426  if (!opt_display && opt_click_allowed) {
427  std::cerr << std::endl << "ERROR:" << std::endl;
428  std::cerr << " Display is disabled but clicks are required !" << std::endl;
429  return EXIT_FAILURE;
430  }
431 
432  std::ostringstream s;
433 
434  if (opt_ppath.empty()) {
435  // Set the path location of the image sequence
436  dirname = vpIoTools::createFilePath(ipath, "mire-2");
437 
438  // Build the name of the image file
439 
440  s.setf(std::ios::right, std::ios::adjustfield);
441  s << "image.%04d.";
442  s << ext;
443  filename = vpIoTools::createFilePath(dirname, s.str());
444  }
445  else {
446  filename = opt_ppath;
447  }
448 
449  // We will read a sequence of images
450  vpVideoReader grabber;
451  grabber.setFirstFrameIndex(1);
452  grabber.setFileName(filename.c_str());
453  // Grey level image associated to a display in the initial pose
454  // computation
455  vpImage<unsigned char> Idisplay;
456  // Grey level image to track points
458  // RGBa image to get background
459  vpImage<vpRGBa> IC;
460  // Matrix representing camera parameters
462 
463  // Variables used for pose computation purposes
464  vpPose mPose;
465  vpDot2 md[4];
466  vpImagePoint mcog[4];
467  vpPoint mP[4];
468 
469  // CameraParameters we got from calibration
470  // Keep u0 and v0 as center of the screen
471  vpCameraParameters mcam;
472 
473  try {
474  std::cout << "Load: " << filename << std::endl;
475  grabber.open(Idisplay);
476  grabber.acquire(Idisplay);
477  vpCameraParameters mcamTmp(592, 570, grabber.getWidth() / 2, grabber.getHeight() / 2);
478  // Compute the initial pose of the camera
479  computeInitialPose(&mcamTmp, Idisplay, &mPose, md, mcog, &cMo, mP, opt_click_allowed, opt_display);
480  // Close the framegrabber
481  grabber.close();
482 
483  // Associate the grabber to the RGBa image
484  grabber.open(IC);
485  mcam.init(mcamTmp);
486  }
487  catch (...) {
488  std::cerr << std::endl << "ERROR:" << std::endl;
489  std::cerr << " Cannot read " << filename << std::endl;
490  std::cerr << " Check your -i " << ipath << " option " << std::endl
491  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
492  return EXIT_FAILURE;
493  }
494 
495  // Create a vpAROgre object with color background
496  vpAROgre ogre(mcam, grabber.getWidth(), grabber.getHeight());
497  // Initialize it
498  ogre.setShowConfigDialog(opt_display);
499  ogre.init(IC, false, !opt_display);
500  ogre.load("Robot", "robot.mesh");
501  ogre.setScale("Robot", 0.001f, 0.001f, 0.001f);
502  ogre.setRotation("Robot", vpRotationMatrix(vpRxyzVector(M_PI / 2, -M_PI / 2, 0)));
503 
504  // Add an optional point light source
505  ogre.getSceneManager()->setAmbientLight(Ogre::ColourValue((float)0.6, (float)0.6, (float)0.6)); // Default value of lightning
506  Ogre::Light *light = ogre.getSceneManager()->createLight();
507  light->setDiffuseColour(1.0, 1.0, 1.0); // scaled RGB values
508  light->setSpecularColour(1.0, 1.0, 1.0); // scaled RGB values
509  // Lumiere ponctuelle
510 #if (VISP_HAVE_OGRE_VERSION < (1 << 16 | 10 << 8 | 0))
511  light->setPosition(-5, -5, 10);
512 #else
513  Ogre::SceneNode *spotLightNode = ogre.getSceneManager()->getRootSceneNode()->createChildSceneNode();
514  spotLightNode->attachObject(light);
515  spotLightNode->setPosition(Ogre::Vector3(-5, -5, 10));
516 #endif
517  light->setType(Ogre::Light::LT_POINT);
518  light->setAttenuation((Ogre::Real)100, (Ogre::Real)1.0, (Ogre::Real)0.045, (Ogre::Real)0.0075);
519  // Ombres
520  light->setCastShadows(true);
521 
522  // Rendering loop
523  while (ogre.continueRendering() && !grabber.end()) {
524  // Acquire a frame
525  grabber.acquire(IC);
526 
527  // Convert it to a grey level image for tracking purpose
529 
530  // kill the point list
531  mPose.clearPoint();
532 
533  // track the dot
534  for (int i = 0; i < 4; ++i) {
535  // track the point
536  md[i].track(I, mcog[i]);
537  md[i].setGrayLevelPrecision(0.90);
538  // pixel->meter conversion
539  {
540  double x = 0, y = 0;
541  vpPixelMeterConversion::convertPoint(mcam, mcog[i], x, y);
542  mP[i].set_x(x);
543  mP[i].set_y(y);
544  }
545 
546  // and added to the pose computation point list
547  mPose.addPoint(mP[i]);
548  }
549  // the pose structure has been updated
550 
551  // the pose is now updated using the virtual visual servoing approach
552  // Dementhon or lagrange is no longer necessary, pose at the
553  // previous iteration is sufficient
554  mPose.computePose(vpPose::VIRTUAL_VS, cMo);
555 
556  // Display with ogre
557  if (opt_display) {
558  ogre.display(IC, cMo);
559  }
560 
561  // Wait so that the video does not go too fast
562  vpTime::wait(15);
563  }
564  // Close the grabber
565  grabber.close();
566  return EXIT_SUCCESS;
567  }
568  catch (const vpException &e) {
569  std::cout << "Catch a ViSP exception: " << e << std::endl;
570  return EXIT_FAILURE;
571  }
572  catch (Ogre::Exception &e) {
573  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
574  return EXIT_FAILURE;
575  }
576  catch (...) {
577  std::cout << "Catch an exception " << std::endl;
578  return EXIT_FAILURE;
579  }
580 }
581 #else // VISP_HAVE_OGRE && VISP_HAVE_DISPLAY
582 int main()
583 {
584 #if (!(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)))
585  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..."
586  << std::endl;
587  std::cout << "Tip if you are on a unix-like system:" << std::endl;
588  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
589  std::cout << "Tip if you are on a windows-like system:" << std::endl;
590  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
591 #else
592  std::cout << "You do not have Ogre functionalities" << std::endl;
593  std::cout << "Tip:" << std::endl;
594  std::cout << "- Install Ogre3D, configure again ViSP using cmake and build again this example" << std::endl;
595 #endif
596  return EXIT_SUCCESS;
597  }
598 #endif
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:110
Generic class defining intrinsic camera parameters.
void init()
Basic initialization with the default parameters.
static const vpColor red
Definition: vpColor.h:198
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:125
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:452
void setGraphics(bool activate)
Definition: vpDot2.h:318
void display(const vpImage< unsigned char > &I, vpColor color=vpColor::red, unsigned int thickness=1) const
Definition: vpDot2.cpp:225
void setSizePrecision(const double &sizePrecision)
Definition: vpDot2.cpp:756
void setGrayLevelPrecision(const double &grayLevelPrecision)
Definition: vpDot2.cpp:726
vpImagePoint getCog() const
Definition: vpDot2.h:181
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:269
error that can be emitted by ViSP classes.
Definition: vpException.h:60
unsigned int getWidth() const
Return the number of columns in the image.
unsigned int getHeight() const
Return the number of rows in the image.
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_j(double jj)
Definition: vpImagePoint.h:309
void set_i(double ii)
Definition: vpImagePoint.h:298
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1439
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:468
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:470
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:77
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:96
@ DEMENTHON_LAGRANGE_VIRTUAL_VS
Definition: vpPose.h:98
@ VIRTUAL_VS
Definition: vpPose.h:92
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, FuncCheckValidityPose func=nullptr)
Definition: vpPose.cpp:385
void clearPoint()
Definition: vpPose.cpp:89
static void display(vpImage< unsigned char > &I, vpHomogeneousMatrix &cMo, vpCameraParameters &cam, double size, vpColor col=vpColor::none)
Definition: vpPose.cpp:567
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void setFirstFrameIndex(const long first_frame)
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)