Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
trackDot2WithAutoDetection.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 auto detection of dots.
33  *
34 *****************************************************************************/
47 #include <visp3/core/vpConfig.h>
48 #include <visp3/core/vpDebug.h>
49 
50 #include <iomanip>
51 #include <sstream>
52 #include <stdio.h>
53 #include <stdlib.h>
54 
55 #if defined(VISP_HAVE_MODULE_BLOB) && \
56  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
57 
58 #include <visp3/blob/vpDot2.h>
59 #include <visp3/core/vpImage.h>
60 #include <visp3/core/vpImagePoint.h>
61 #include <visp3/core/vpIoTools.h>
62 #include <visp3/gui/vpDisplayGDI.h>
63 #include <visp3/gui/vpDisplayGTK.h>
64 #include <visp3/gui/vpDisplayOpenCV.h>
65 #include <visp3/gui/vpDisplayX.h>
66 #include <visp3/io/vpImageIo.h>
67 #include <visp3/io/vpParseArgv.h>
68 
69 // List of allowed command line options
70 #define GETOPTARGS "cdi:p:f:l:s:S:G:E:h"
71 
72 #ifdef ENABLE_VISP_NAMESPACE
73 using namespace VISP_NAMESPACE_NAME;
74 #endif
75 
93 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first,
94  unsigned last, unsigned step, double sizePrecision, double grayLevelPrecision,
95  double ellipsoidShapePrecision)
96 {
97 #if VISP_HAVE_DATASET_VERSION >= 0x030600
98  std::string ext("png");
99 #else
100  std::string ext("pgm");
101 #endif
102  fprintf(stdout, "\n\
103 Test auto detection of dots using vpDot2.\n\
104  \n\
105 SYNOPSIS\n\
106  %s [-i <input image path>] [-p <personal image path>]\n\
107  [-f <first image>] [-l <last image>] [-s <step>] \n\
108  [-S <size precision>] [-G <gray level precision>]\n\
109  [-E <ellipsoid shape precision>] [-c] [-d] [-h]\n",
110  name);
111 
112  fprintf(stdout, "\n\
113 OPTIONS: Default\n\
114  -i <input image path> %s\n\
115  Set image input path.\n\
116  From this path read images \n\
117  \"mire-2/image.%%04d.%s\"\n\
118  Setting the VISP_INPUT_IMAGE_PATH environment\n\
119  variable produces the same behaviour than using\n\
120  this option.\n\
121  \n\
122  -p <personal image path> %s\n\
123  Specify a personal sequence containing images \n\
124  to process.\n\
125  The format is selected by analysing the filename extension.\n\
126  Example : \"/Temp/visp-images/mire-2/image.%%04d.%s\"\n\
127  %%04d is for the image numbering.\n\
128  \n\
129  -f <first image> %u\n\
130  First image number of the sequence.\n\
131  \n\
132  -l <last image> %u\n\
133  Last image number of the sequence.\n\
134  \n\
135  -s <step> %u\n\
136  Step between two images.\n\
137  \n\
138  -S <size precision> %f\n\
139  Precision of the size of the dot. \n\
140  It is a double precision float witch value is in ]0,1].\n\
141  1 means full precision, the sizes (width, heigth, surface) \n\
142  of the dots must the same, whereas values close to 0 \n\
143  show a very bad precision.\n\
144 \n\
145  -G <gray level precision> %f\n\
146  Precision of the gray level of the dot. \n\
147  It is a double precision float witch value is in ]0,1].\n\
148  1 means full precision, the gray level must the same in \n\
149  the wall dot, whereas values close to 0 \n\
150  show a very bad precision.\n\
151 \n\
152  -E <ellipsoid shape precision> %f\n\
153  Precision of the ellipsoid shape of the dot. \n\
154  It is a double precision float witch value is in [0,1].\n\
155  1 means full precision, the shape should be a perfect ellipsoid,\n\
156  whereas values close to 0 show a very bad precision.\n\
157  0 means the shape of dots is not tested \n\
158 \n",
159 ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step, sizePrecision, grayLevelPrecision,
160 ellipsoidShapePrecision);
161 
162  fprintf(stdout, "\
163  -c\n\
164  Disable the mouse click. Useful to automate the \n\
165  execution of this program without human intervention.\n\
166  \n\
167  -d \n\
168  Turn off the display.\n\
169  \n\
170  -h\n\
171  Print the help.\n");
172 
173  if (badparam)
174  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
175 }
196 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
197  unsigned &step, double &sizePrecision, double &grayLevelPrecision, double &ellipsoidShapePrecision,
198  bool &click_allowed, bool &display)
199 {
200  const char *optarg_;
201  int c;
202  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
203 
204  switch (c) {
205  case 'c':
206  click_allowed = false;
207  break;
208  case 'd':
209  display = false;
210  break;
211  case 'i':
212  ipath = optarg_;
213  break;
214  case 'p':
215  ppath = optarg_;
216  break;
217  case 'f':
218  first = (unsigned)atoi(optarg_);
219  break;
220  case 'l':
221  last = (unsigned)atoi(optarg_);
222  break;
223  case 's':
224  step = (unsigned)atoi(optarg_);
225  break;
226  case 'S':
227  sizePrecision = atof(optarg_);
228  break;
229  case 'G':
230  grayLevelPrecision = atof(optarg_);
231  break;
232  case 'E':
233  ellipsoidShapePrecision = atof(optarg_);
234  break;
235  case 'h':
236  usage(argv[0], nullptr, ipath, ppath, first, last, step, sizePrecision, grayLevelPrecision,
237  ellipsoidShapePrecision);
238  return false;
239  break;
240 
241  default:
242  usage(argv[0], optarg_, ipath, ppath, first, last, step, sizePrecision, grayLevelPrecision,
243  ellipsoidShapePrecision);
244  return false;
245  break;
246  }
247  }
248 
249  if ((c == 1) || (c == -1)) {
250  // standalone param or error
251  usage(argv[0], nullptr, ipath, ppath, first, last, step, sizePrecision, grayLevelPrecision,
252  ellipsoidShapePrecision);
253  std::cerr << "ERROR: " << std::endl;
254  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
255  return false;
256  }
257 
258  return true;
259 }
260 
261 int main(int argc, const char **argv)
262 {
263  try {
264  std::string env_ipath;
265  std::string opt_ipath;
266  std::string ipath;
267  std::string opt_ppath;
268  std::string dirname;
269  std::string filename;
270  unsigned opt_first = 1;
271  unsigned opt_last = 10;
272  unsigned opt_step = 1;
273  double opt_sizePrecision = 0.65;
274  double opt_grayLevelPrecision = 0.85;
275  double opt_ellipsoidShapePrecision = 0.8;
276  bool opt_click_allowed = true;
277  bool opt_display = true;
278 
279 #if VISP_HAVE_DATASET_VERSION >= 0x030600
280  std::string ext("png");
281 #else
282  std::string ext("pgm");
283 #endif
284 
285  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
286  // environment variable value
287  env_ipath = vpIoTools::getViSPImagesDataPath();
288 
289  // Set the default input path
290  if (!env_ipath.empty())
291  ipath = env_ipath;
292 
293  // Read the command line options
294  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_sizePrecision,
295  opt_grayLevelPrecision, opt_ellipsoidShapePrecision, opt_click_allowed, opt_display) == false) {
296  return EXIT_FAILURE;
297  }
298 
299  // Get the option values
300  if (!opt_ipath.empty())
301  ipath = opt_ipath;
302 
303  // Compare ipath and env_ipath. If they differ, we take into account
304  // the input path coming from the command line option
305  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
306  if (ipath != env_ipath) {
307  std::cout << std::endl << "WARNING: " << std::endl;
308  std::cout << " Since -i <visp image path=" << ipath << "> "
309  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
310  << " we skip the environment variable." << std::endl;
311  }
312  }
313 
314  // Test if an input path is set
315  if (opt_ipath.empty() && env_ipath.empty()) {
316  usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step, opt_sizePrecision,
317  opt_grayLevelPrecision, opt_ellipsoidShapePrecision);
318  std::cerr << std::endl << "ERROR:" << std::endl;
319  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
320  << " environment variable to specify the location of the " << std::endl
321  << " image path where test images are located." << std::endl
322  << std::endl
323  << " Use -p <personal image path> option if you want to " << std::endl
324  << " use personal images." << std::endl;
325  return EXIT_FAILURE;
326  }
327 
328  // Declare an image, this is a gray level image (unsigned char)
329  // it size is not defined yet, it will be defined when the image will
330  // read on the disk
332  std::ostringstream s;
333  char cfilename[FILENAME_MAX];
334  unsigned iter = opt_first; // Image number
335 
336  if (opt_ppath.empty()) {
337 
338  // Warning :
339  // The image sequence is not provided with the ViSP package
340  // therefore the program will return an error :
341  // !! couldn't read file visp-images/mire-2/image.0001.png
342  //
343  // ViSP dataset is available on the visp www site
344  // https://visp.inria.fr/download/.
345 
346  // Set the path location of the image sequence
347  dirname = vpIoTools::createFilePath(ipath, "mire-2");
348 
349  // Build the name of the image file
350 
351  s.setf(std::ios::right, std::ios::adjustfield);
352  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
353  filename = vpIoTools::createFilePath(dirname, s.str());
354  }
355  else {
356  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
357  filename = cfilename;
358  }
359  // Read the image named "filename", and put the bitmap into the image structure I.
360  // I is initialized to the correct size
361  //
362  // vpImageIo::read() may throw various exception if, for example,
363  // the file does not exist, or if the memory cannot be allocated
364  try {
365  vpCTRACE << "Load: " << filename << std::endl;
366 
367  vpImageIo::read(I, filename);
368  }
369  catch (...) {
370  // If an exception is thrown by vpImageIo::read() it will result in the end of the program.
371  std::cerr << std::endl << "ERROR:" << std::endl;
372  std::cerr << " Cannot read " << filename << std::endl;
373  if (opt_ppath.empty()) {
374  std::cerr << " Check your -i " << ipath << " option " << std::endl
375  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
376  }
377  else {
378  std::cerr << " Check your -p " << opt_ppath << " option " << std::endl;
379  }
380  return EXIT_FAILURE;
381  }
382 
383 // We open a window using either GTK, X11 or GDI.
384 #if defined(VISP_HAVE_GTK)
385  vpDisplayGTK display;
386 #elif defined(VISP_HAVE_X11)
387  vpDisplayX display;
388 #elif defined(VISP_HAVE_GDI)
389  vpDisplayGDI display;
390 #elif defined(HAVE_OPENCV_HIGHGUI)
391  vpDisplayOpenCV display;
392 #endif
393 
394  if (opt_display) {
395  // Display size is automatically defined by the image (I) size
396  display.init(I, 100, 100, "Display...");
397  // Display the image
398  // The image class has a member that specify a pointer toward
399  // the display that has been initialized in the display declaration
400  // therefore is is no longer necessary to make a reference to the
401  // display variable.
403  vpDisplay::flush(I);
404  }
405 
406  // Dot declaration
407  vpDot2 d;
408 
409  d.setGraphics(true);
410  if (opt_click_allowed & opt_display) {
411  d.setGrayLevelPrecision(opt_grayLevelPrecision);
412 
413  std::cout << "Please click on a dot to initialize detection" << std::endl;
414 
415  d.initTracking(I);
416  if (opt_display) {
417  vpImagePoint cog;
418  cog = d.getCog();
420  vpDisplay::flush(I);
421  }
422  d.setSizePrecision(opt_sizePrecision);
423  d.setEllipsoidShapePrecision(opt_ellipsoidShapePrecision);
424  printf("Dot characteristics: \n");
425  printf(" width : %lf\n", d.getWidth());
426  printf(" height: %lf\n", d.getHeight());
427  printf(" area: %lf\n", d.getArea());
428  printf(" gray level min: %u\n", d.getGrayLevelMin());
429  printf(" gray level max: %u\n", d.getGrayLevelMax());
430  printf(" grayLevelPrecision: %lf\n", d.getGrayLevelPrecision());
431  printf(" sizePrecision: %lf\n", d.getSizePrecision());
432  printf(" ellipsoidShapePrecision: %lf\n", d.getEllipsoidShapePrecision());
433  }
434  else {
435  // Set dot characteristics for the auto detection
436  d.setGraphics(true);
437  d.setWidth(15.0);
438  d.setHeight(12.0);
439  d.setArea(124);
440  d.setGrayLevelMin(164);
441  d.setGrayLevelMax(255);
442  d.setGrayLevelPrecision(opt_grayLevelPrecision);
443  d.setSizePrecision(opt_sizePrecision);
444  d.setEllipsoidShapePrecision(opt_ellipsoidShapePrecision);
445  }
446 
447  while (iter < opt_last) {
448  // set the new image name
449 
450  if (opt_ppath.empty()) {
451 
452  s.str("");
453  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
454  filename = vpIoTools::createFilePath(dirname, s.str());
455  }
456  else {
457  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
458  filename = cfilename;
459  }
460  // read the image
461  vpImageIo::read(I, filename);
462 
463  if (opt_display) {
464  // Display the image
466  }
467 
468  std::cout << "Search dots in image" << filename << std::endl;
469  std::list<vpDot2> list_d;
470  d.searchDotsInArea(I, 0, 0, I.getWidth(), I.getHeight(), list_d);
471 
472  if (list_d.empty()) {
473  std::cout << "Dot auto detection did not work." << std::endl;
474  return EXIT_FAILURE;
475  }
476  else {
477  std::cout << std::endl << list_d.size() << " dots are detected" << std::endl;
478 
479  if (opt_display) {
480  int i = 0;
481  // Parse all founded dots for display
482  for (std::list<vpDot2>::const_iterator it = list_d.begin(); it != list_d.end(); ++it) {
483  vpImagePoint cog = (*it).getCog();
484 
485  std::cout << "Dot " << i++ << " : " << cog.get_u() << " " << cog.get_v() << std::endl;
486 
487  vpDisplay::displayCross(I, cog, 16, vpColor::blue, 3);
488  }
489  vpDisplay::flush(I);
490  }
491  }
492 
493  // If click is allowed, wait for a mouse click to launch the next
494  // iteration
495  if (opt_display && opt_click_allowed) {
496  std::cout << "\nA click to continue..." << std::endl;
497  // Wait for a blocking mouse click
499  }
500 
501  iter += opt_step;
502  }
503  if (opt_display && opt_click_allowed) {
504  std::cout << "\nA click to exit..." << std::endl;
505  // Wait for a blocking mouse click
507  }
508 
509  return EXIT_SUCCESS;
510  }
511  catch (const vpException &e) {
512  std::cout << "Catch an exception: " << e << std::endl;
513  return EXIT_FAILURE;
514  }
515 }
516 #else
517 #include <iostream>
518 
519 int main()
520 {
521  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
522  "functionalities are required..."
523  << std::endl;
524  return EXIT_SUCCESS;
525  }
526 
527 #endif
static const vpColor blue
Definition: vpColor.h:223
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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)
unsigned int getHeight() const
Definition: vpDisplay.h:226
static void flush(const vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpDisplay.h:231
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:125
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_u() const
Definition: vpImagePoint.h:136
double get_v() const
Definition: vpImagePoint.h:147
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70