Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
trackDot2.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  * Example of dot tracking.
33  *
34 *****************************************************************************/
41 #include <visp3/core/vpConfig.h>
42 #include <visp3/core/vpDebug.h>
43 
44 #include <iomanip>
45 #include <sstream>
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #if defined(VISP_HAVE_MODULE_BLOB) && \
50  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
51 
52 #include <visp3/blob/vpDot2.h>
53 #include <visp3/core/vpImage.h>
54 #include <visp3/core/vpImagePoint.h>
55 #include <visp3/core/vpIoTools.h>
56 #include <visp3/gui/vpDisplayGDI.h>
57 #include <visp3/gui/vpDisplayGTK.h>
58 #include <visp3/gui/vpDisplayOpenCV.h>
59 #include <visp3/gui/vpDisplayX.h>
60 #include <visp3/io/vpImageIo.h>
61 #include <visp3/io/vpParseArgv.h>
62 
63 // List of allowed command line options
64 #define GETOPTARGS "cdf:i:l:p:s:h"
65 
66 #ifdef ENABLE_VISP_NAMESPACE
67 using namespace VISP_NAMESPACE_NAME;
68 #endif
69 
88 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first,
89  unsigned last, unsigned step)
90 {
91 #if VISP_HAVE_DATASET_VERSION >= 0x030600
92  std::string ext("png");
93 #else
94  std::string ext("pgm");
95 #endif
96  fprintf(stdout, "\n\
97 Test dot tracking using vpDot2 class.\n\
98 \n\
99 SYNOPSIS\n\
100  %s [-i <test image path>] [-p <personal image path>]\n\
101  [-f <first image>] [-l <last image>] [-s <step>]\n\
102  [-c] [-d] [-h]\n",
103  name);
104 
105  fprintf(stdout, "\n\
106 OPTIONS: Default\n\
107  -i <input image path> %s\n\
108  Set image input path.\n\
109  From this path read images \n\
110  \"mire-2/image.%%04d.%s\". These \n\
111  images come from visp-images-x.y.z.tar.gz available \n\
112  on the ViSP website.\n\
113  Setting the VISP_INPUT_IMAGE_PATH environment\n\
114  variable produces the same behaviour than using\n\
115  this option.\n\
116  \n\
117  -p <personal image path> %s\n\
118  Specify a personal sequence containing images \n\
119  to process.\n\
120  By image sequence, we mean one file per image.\n\
121  Example : \"C:/Temp/visp-images/cube/image.%%04d.%s\"\n\
122  %%04d is for the image numbering.\n\
123  \n\
124  -f <first image> %u\n\
125  First image number of the sequence.\n\
126  \n\
127  -l <last image> %u\n\
128  Last image number of the sequence.\n\
129  \n\
130  -s <step> %u\n\
131  Step between two images.\n\
132 \n\
133  -c\n\
134  Disable the mouse click. Useful to automate the \n\
135  execution of this program without human intervention.\n\
136 \n\
137  -d \n\
138  Turn off the display.\n\
139 \n\
140  -h\n\
141  Print the help.\n",
142  ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
143 
144  if (badparam)
145  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
146 }
164 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
165  unsigned &step, bool &click_allowed, bool &display)
166 {
167  const char *optarg_;
168  int c;
169  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
170 
171  switch (c) {
172  case 'c':
173  click_allowed = false;
174  break;
175  case 'd':
176  display = false;
177  break;
178  case 'i':
179  ipath = optarg_;
180  break;
181  case 'p':
182  ppath = optarg_;
183  break;
184  case 'f':
185  first = (unsigned)atoi(optarg_);
186  break;
187  case 'l':
188  last = (unsigned)atoi(optarg_);
189  break;
190  case 's':
191  step = (unsigned)atoi(optarg_);
192  break;
193  case 'h':
194  usage(argv[0], nullptr, ipath, ppath, first, last, step);
195  return false;
196  break;
197 
198  default:
199  usage(argv[0], optarg_, ipath, ppath, first, last, step);
200  return false;
201  break;
202  }
203  }
204 
205  if ((c == 1) || (c == -1)) {
206  // standalone param or error
207  usage(argv[0], nullptr, ipath, ppath, first, last, step);
208  std::cerr << "ERROR: " << std::endl;
209  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
210  return false;
211  }
212 
213  return true;
214 }
215 
216 int main(int argc, const char **argv)
217 {
218  try {
219  std::string env_ipath;
220  std::string opt_ipath;
221  std::string ipath;
222  std::string opt_ppath;
223  std::string dirname;
224  std::string filename;
225  unsigned opt_first = 1;
226  unsigned opt_last = 500;
227  unsigned opt_step = 1;
228  bool opt_click_allowed = true;
229  bool opt_display = true;
230 
231 #if VISP_HAVE_DATASET_VERSION >= 0x030600
232  std::string ext("png");
233 #else
234  std::string ext("pgm");
235 #endif
236 
237  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
238  // environment variable value
239  env_ipath = vpIoTools::getViSPImagesDataPath();
240 
241  // Set the default input path
242  if (!env_ipath.empty())
243  ipath = env_ipath;
244 
245  // Read the command line options
246  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
247  opt_display) == false) {
248  return EXIT_FAILURE;
249  }
250 
251  // Get the option values
252  if (!opt_ipath.empty())
253  ipath = opt_ipath;
254 
255  // Compare ipath and env_ipath. If they differ, we take into account
256  // the input path coming from the command line option
257  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
258  if (ipath != env_ipath) {
259  std::cout << std::endl << "WARNING: " << std::endl;
260  std::cout << " Since -i <visp image path=" << ipath << "> "
261  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
262  << " we skip the environment variable." << std::endl;
263  }
264  }
265 
266  // Test if an input path is set
267  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
268  usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
269  std::cerr << std::endl << "ERROR:" << std::endl;
270  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
271  << " environment variable to specify the location of the " << std::endl
272  << " image path where test images are located." << std::endl
273  << " Use -p <personal image path> option if you want to " << std::endl
274  << " use personal images." << std::endl
275  << std::endl;
276 
277  return EXIT_FAILURE;
278  }
279 
280  // Declare an image, this is a gray level image (unsigned char)
281  // it size is not defined yet, it will be defined when the image will
282  // read on the disk
284 
285  unsigned iter = opt_first;
286  std::ostringstream s;
287  char cfilename[FILENAME_MAX];
288 
289  if (opt_ppath.empty()) {
290 
291  // Warning :
292  // The image sequence is not provided with the ViSP package
293  // therefore the program will return an error :
294  // !! couldn't read file visp-images/mire-2/image.0001.png
295  //
296  // ViSP dataset is available on the visp www site
297  // https://visp.inria.fr/download/.
298 
299  // Set the path location of the image sequence
300  dirname = vpIoTools::createFilePath(ipath, "mire-2");
301 
302  // Build the name of the image file
303 
304  s.setf(std::ios::right, std::ios::adjustfield);
305  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
306  filename = vpIoTools::createFilePath(dirname, s.str());
307  }
308  else {
309  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
310  filename = cfilename;
311  }
312 
313  // Read the image named "filename", and put the bitmap into the image structure I.
314  // I is initialized to the correct size
315  //
316  // vpImageIo::read() may throw various exception if, for example,
317  // the file does not exist, or if the memory cannot be allocated
318  try {
319  vpCTRACE << "Load: " << filename << std::endl;
320 
321  vpImageIo::read(I, filename);
322  }
323  catch (...) {
324  // If an exception is thrown by vpImageIo::read() it will result in the end of the program.
325  std::cerr << std::endl << "ERROR:" << std::endl;
326  std::cerr << " Cannot read " << filename << std::endl;
327  if (opt_ppath.empty()) {
328  std::cerr << " Check your -i " << ipath << " option " << std::endl
329  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
330  }
331  else {
332  std::cerr << " Check your -p " << opt_ppath << " option " << std::endl;
333  }
334  return EXIT_FAILURE;
335  }
336 
337 // We open a window using either X11, GTK or GDI.
338 #if defined(VISP_HAVE_X11)
339  vpDisplayX display;
340 #elif defined(VISP_HAVE_GTK)
341  vpDisplayGTK display;
342 #elif defined(VISP_HAVE_GDI)
343  vpDisplayGDI display;
344 #elif defined(HAVE_OPENCV_HIGHGUI)
345  vpDisplayOpenCV display;
346 #endif
347 
348  if (opt_display) {
349  // Display size is automatically defined by the image (I) size
350  display.init(I, 100, 100, "Display...");
351  // Display the image
352  // The image class has a member that specify a pointer toward
353  // the display that has been initialized in the display declaration
354  // therefore is is no longer necessary to make a reference to the
355  // display variable.
357  vpDisplay::flush(I);
358  }
359  // define the vpDot structure.
360 
361  // vpDot and vpDot2 correspond to two different algorithms designed to
362  // track a dot. vpDot is based on recurse connex componants (all the
363  // pixels of the dot are parsed), while vpDot2 is based on freeman chain
364  // code (only the contour of the dot is parsed)
365 
366  vpDot2 d;
367  vpImagePoint cog;
368 
369  if (opt_display) {
370  // by using setGraphics, we request to see the all the pixel of the dot
371  // in green on the screen.
372  // It uses the overlay image plane.
373  // The default of this setting is that it is time consuming
374 
375  d.setGraphics(true);
376  }
377  else {
378 
379  d.setGraphics(false);
380  }
381  // We want to track an ellipsoid shape. If you want to track a non
382  // ellipsoid object, use d.setEllipsoidShape(0); we also request to
383  // compute the dot moment m00, m10, m01, m11, m20, m02
384  d.setComputeMoments(true);
385  d.setGrayLevelPrecision(0.90);
386 
387  // tracking is initalized if no other parameters are given to the
388  // iniTracking(..) method a right mouse click on the dot is expected
389  // dot location can also be specified explicitly in the
390  // initTracking method : d.initTracking(I,ip) where ip is the image
391  // point from which the dot is searched
392 
393  if (opt_display && opt_click_allowed) {
394  std::cout << "Click on a dot to track it." << std::endl;
395  d.initTracking(I);
396  }
397  else {
398  vpImagePoint ip;
399  ip.set_u(160);
400  ip.set_v(212);
401  d.initTracking(I, ip);
402  }
403  if (1) {
404  std::cout << "COG: " << std::endl;
405  cog = d.getCog();
406  std::cout << " u: " << cog.get_u() << " v: " << cog.get_v() << std::endl;
407  std::cout << "Size:" << std::endl;
408  std::cout << " w: " << d.getWidth() << " h: " << d.getHeight() << std::endl;
409  std::cout << "Area: " << d.getArea() << std::endl;
410  std::cout << "Centered normalized moments nij:" << std::endl;
411  std::cout << " n20: " << d.get_nij()[0] << std::endl;
412  std::cout << " n11: " << d.get_nij()[1] << std::endl;
413  std::cout << " n02: " << d.get_nij()[2] << std::endl;
414  std::cout << "Settings:" << std::endl;
415  std::cout << " gray level min: " << d.getGrayLevelMin() << std::endl;
416  std::cout << " gray level max: " << d.getGrayLevelMax() << std::endl;
417  std::cout << " size precision: " << d.getSizePrecision() << std::endl;
418  std::cout << " gray level precision: " << d.getGrayLevelPrecision() << std::endl;
419  }
420 
421  while (iter < opt_last) {
422  // set the new image name
423  if (opt_ppath.empty()) {
424  s.str("");
425  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
426  filename = vpIoTools::createFilePath(dirname, s.str());
427  }
428  else {
429  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
430  filename = cfilename;
431  }
432  // read the image
433  std::cout << "read : " << filename << std::endl;
434  vpImageIo::read(I, filename);
435 
436  // track the dot and returns its coordinates in the image
437  // results are given in float since many many are usually considered
438  //
439  // an exception is thrown by the track method if
440  // - dot is lost
441 
442  if (opt_display) {
443  // Display the image
445  }
446 
447  std::cout << "Tracking on image: " << filename << std::endl;
448  double time = vpTime::measureTimeMs();
449  d.track(I);
450 
451  std::cout << "COG (" << vpTime::measureTimeMs() - time << " ms): " << std::endl;
452  cog = d.getCog();
453  std::cout << " u: " << cog.get_u() << " v: " << cog.get_v() << std::endl;
454  std::cout << "Size:" << std::endl;
455  std::cout << " w: " << d.getWidth() << " h: " << d.getHeight() << std::endl;
456  std::cout << "Area: " << d.getArea() << std::endl;
457  std::cout << "Centered normalized moments nij:" << std::endl;
458  std::cout << " n20: " << d.get_nij()[0] << std::endl;
459  std::cout << " n11: " << d.get_nij()[1] << std::endl;
460  std::cout << " n02: " << d.get_nij()[2] << std::endl;
461  std::cout << "Settings:" << std::endl;
462  std::cout << " gray level min: " << d.getGrayLevelMin() << std::endl;
463  std::cout << " gray level max: " << d.getGrayLevelMax() << std::endl;
464  std::cout << " size precision: " << d.getSizePrecision() << std::endl;
465  std::cout << " gray level precision: " << d.getGrayLevelPrecision() << std::endl;
466 
467  if (opt_display) {
468  if (0) {
469  std::list<vpImagePoint> edges;
470  d.getEdges(edges);
471  std::list<vpImagePoint>::const_iterator it;
472  for (it = edges.begin(); it != edges.end(); ++it) {
474  }
475  }
476 
477  // display a green cross (size 10) in the image at the dot center
478  // of gravity location
480  // flush the X11 buffer
481 
482  vpDisplay::flush(I);
483  }
484  iter++;
485  }
486 
487  if (opt_display && opt_click_allowed) {
488  std::cout << "\nA click to exit..." << std::endl;
489  // Wait for a blocking mouse click
491  }
492  return EXIT_SUCCESS;
493  }
494  catch (const vpException &e) {
495  std::cout << "Catch an exception: " << e << std::endl;
496  return EXIT_FAILURE;
497  }
498 }
499 #else
500 #include <iostream>
501 
502 int main()
503 {
504  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
505  "functionalities are required..."
506  << std::endl;
507  return EXIT_SUCCESS;
508  }
509 
510 #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)
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
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
void set_u(double u)
Definition: vpImagePoint.h:335
void set_v(double v)
Definition: vpImagePoint.h:346
double get_v() const
Definition: vpImagePoint.h:147
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
VISP_EXPORT double measureTimeMs()