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