Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
trackDot.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 
46 #include <visp3/core/vpConfig.h>
47 
48 #if defined(VISP_HAVE_MODULE_BLOB) && defined(VISP_HAVE_DISPLAY)
49 
50 #include <visp3/blob/vpDot.h>
51 #include <visp3/core/vpImage.h>
52 #include <visp3/core/vpImagePoint.h>
53 #include <visp3/core/vpIoTools.h>
54 #include <visp3/gui/vpDisplayFactory.h>
55 #include <visp3/io/vpImageIo.h>
56 #include <visp3/io/vpParseArgv.h>
57 
58 // List of allowed command line options
59 #define GETOPTARGS "cdf:hi:l:p:s:"
60 
61 #ifdef ENABLE_VISP_NAMESPACE
62 using namespace VISP_NAMESPACE_NAME;
63 #endif
64 
78 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first,
79  unsigned last, unsigned step)
80 {
81 #if VISP_HAVE_DATASET_VERSION >= 0x030600
82  std::string ext("png");
83 #else
84  std::string ext("pgm");
85 #endif
86  fprintf(stdout, "\n\
87 Test dot tracking.\n\
88 \n\
89 SYNOPSIS\n\
90  %s [-i <test image path>] [-p <personal image path>]\n\
91  [-f <first image>] [-l <last image>] [-s <step>]\n\
92  [-c] [-d] [-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 : \"C:/Temp/visp-images/cube/image.%%04d.%s\"\n\
111  %%04d is for the image numbering.\n\
112 \n\
113  -f <first image> %u\n\
114  First image number of the sequence.\n\
115 \n\
116  -l <last image> %u\n\
117  Last image number of the sequence.\n\
118 \n\
119  -s <step> %u\n\
120  Step between two images.\n\
121 \n\
122  -c\n\
123  Disable the mouse click. Useful to automate the \n\
124  execution of this program without human intervention.\n\
125 \n\
126  -d \n\
127  Turn off the display.\n\
128 \n\
129  -h\n\
130  Print the help.\n",
131  ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
132 
133  if (badparam)
134  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
135 }
153 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
154  unsigned &step, bool &click_allowed, bool &display)
155 {
156  const char *optarg_;
157  int c;
158  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
159 
160  switch (c) {
161  case 'c':
162  click_allowed = false;
163  break;
164  case 'd':
165  display = false;
166  break;
167  case 'i':
168  ipath = optarg_;
169  break;
170  case 'p':
171  ppath = optarg_;
172  break;
173  case 'f':
174  first = (unsigned)atoi(optarg_);
175  break;
176  case 'l':
177  last = (unsigned)atoi(optarg_);
178  break;
179  case 's':
180  step = (unsigned)atoi(optarg_);
181  break;
182  case 'h':
183  usage(argv[0], nullptr, ipath, ppath, first, last, step);
184  return false;
185  break;
186 
187  default:
188  usage(argv[0], optarg_, ipath, ppath, first, last, step);
189  return false;
190  break;
191  }
192  }
193 
194  if ((c == 1) || (c == -1)) {
195  // standalone param or error
196  usage(argv[0], nullptr, ipath, ppath, first, last, step);
197  std::cerr << "ERROR: " << std::endl;
198  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
199  return false;
200  }
201 
202  return true;
203 }
204 
205 int main(int argc, const char **argv)
206 {
207  try {
208  std::string env_ipath;
209  std::string opt_ipath;
210  std::string ipath;
211  std::string opt_ppath;
212  std::string dirname;
213  std::string filename;
214  unsigned int opt_first = 1;
215  unsigned int opt_last = 500;
216  unsigned int opt_step = 1;
217  bool opt_click_allowed = true;
218  bool opt_display = true;
219 
220 #if VISP_HAVE_DATASET_VERSION >= 0x030600
221  std::string ext("png");
222 #else
223  std::string ext("pgm");
224 #endif
225 
226  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
227  // environment variable value
228  env_ipath = vpIoTools::getViSPImagesDataPath();
229 
230  // Set the default input path
231  if (!env_ipath.empty())
232  ipath = env_ipath;
233 
234  // Read the command line options
235  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
236  opt_display) == false) {
237  return EXIT_FAILURE;
238  }
239 
240  // Get the option values
241  if (!opt_ipath.empty())
242  ipath = opt_ipath;
243 
244  // Compare ipath and env_ipath. If they differ, we take into account
245  // the input path coming from the command line option
246  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
247  if (ipath != env_ipath) {
248  std::cout << std::endl << "WARNING: " << std::endl;
249  std::cout << " Since -i <visp image path=" << ipath << "> "
250  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
251  << " we skip the environment variable." << std::endl;
252  }
253  }
254 
255  // Test if an input path is set
256  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
257  usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
258  std::cerr << std::endl << "ERROR:" << std::endl;
259  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
260  << " environment variable to specify the location of the " << std::endl
261  << " image path where test images are located." << std::endl
262  << " Use -p <personal image path> option if you want to " << std::endl
263  << " use personal images." << std::endl
264  << std::endl;
265 
266  return EXIT_FAILURE;
267  }
268 
269  // Declare an image, this is a gray level image (unsigned char)
270  // it size is not defined yet, it will be defined when the image will
271  // read on the disk
273  vpDisplay *display = nullptr;
274 
275  unsigned iter = opt_first;
276  std::ostringstream s;
277  char cfilename[FILENAME_MAX];
278 
279  if (opt_ppath.empty()) {
280 
281  // Warning :
282  // The image sequence is not provided with the ViSP package
283  // therefore the program will return an error :
284  // !! couldn't read file visp-images/mire-2/image.0001.png
285  //
286  // ViSP dataset is available on the visp www site
287  // https://visp.inria.fr/download/.
288 
289  // Set the path location of the image sequence
290  dirname = vpIoTools::createFilePath(ipath, "mire-2");
291 
292  // Build the name of the image file
293  s.setf(std::ios::right, std::ios::adjustfield);
294  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
295  filename = vpIoTools::createFilePath(dirname, s.str());
296  }
297  else {
298  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
299  filename = cfilename;
300  }
301 
302  // Read the image named "filename", and put the bitmap into the image structure I.
303  // I is initialized to the correct size
304  //
305  // vpImageIo::read() may throw various exception if, for example,
306  // the file does not exist, or if the memory cannot be allocated
307  try {
308  std::cout << "Load: " << filename << std::endl;
309 
310  vpImageIo::read(I, filename);
311  }
312  catch (...) {
313  // If an exception is thrown by vpImageIo::read() it will result in the end of the program.
314  std::cerr << std::endl << "ERROR:" << std::endl;
315  std::cerr << " Cannot read " << filename << std::endl;
316  if (opt_ppath.empty()) {
317  std::cerr << " Check your -i " << ipath << " option " << std::endl
318  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
319  }
320  else {
321  std::cerr << " Check your -p " << opt_ppath << " option " << std::endl;
322  }
323  return EXIT_FAILURE;
324  }
325 
326  if (opt_display) {
327  // We open a window using either X11, GTK, OpenCV or GDI
329  // Display size is automatically defined by the image (I) size
330  display->init(I, 100, 100, "Display...");
331  // Display the image
332  // The image class has a member that specify a pointer toward
333  // the display that has been initialized in the display declaration
334  // therefore is is no longer necessary to make a reference to the
335  // display variable.
337  vpDisplay::flush(I);
338  }
339 
340  // by using setGraphics, we request to see the all the pixel of the dot
341  vpDot d;
342  if (opt_display) {
343  // by using setGraphics, we request to see the all the pixel of the dot
344  // in green on the screen.
345  // It uses the overlay image plane.
346  // The default of this setting is that it is time consuming
347  d.setGraphics(true);
348  }
349  else {
350  d.setGraphics(false);
351  }
352  // we also request to compute the dot moment m00, m10, m01, m11, m20, m02
353  d.setComputeMoments(true);
355 
356  if (opt_display && opt_click_allowed) {
357  // tracking is initalized
358  // if no other parameters are given to the iniTracking(..) method
359  // a right mouse click on the dot is expected
360  std::cout << "Click on a white dot you want to track..." << std::endl;
361  d.initTracking(I);
362  }
363  else {
364  // dot location can also be specified explicitly in the
365  // initTracking method : d.initTracking(I,ip) where ip is the
366  // image point from which the dot is searched
367  vpImagePoint ip;
368  ip.set_u(160);
369  ip.set_v(212);
370  d.initTracking(I, ip);
371  }
372 
373  bool quit = false;
374  while ((iter < opt_last) && (!quit)) {
375  // set the new image name
376  if (opt_ppath.empty()) {
377  s.str("");
378  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
379  filename = vpIoTools::createFilePath(dirname, s.str());
380  }
381  else {
382  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
383  filename = cfilename;
384  }
385  // read the image
386  std::cout << "read: " << filename << std::endl;
387  vpImageIo::read(I, filename);
388 
389  if (opt_display) {
390  // Display the image
392  }
393  std::cout << "Tracking on image: " << filename << std::endl;
394 
395  // track the dot
396  double time = vpTime::measureTimeMs();
397  d.track(I);
398 
399  std::cout << "COG (" << vpTime::measureTimeMs() - time << " ms): " << std::endl;
400  vpImagePoint cog = d.getCog();
401  std::cout << cog.get_u() << " " << cog.get_v() << std::endl;
402  std::cout << "Size:" << std::endl;
403  std::cout << " w: " << d.getWidth() << " h: " << d.getHeight() << std::endl;
404  std::cout << "Area: " << d.getArea() << std::endl;
405  std::cout << "Centered normalized moments nij:" << std::endl;
406  std::cout << " n20: " << d.get_nij()[0] << std::endl;
407  std::cout << " n11: " << d.get_nij()[1] << std::endl;
408  std::cout << " n02: " << d.get_nij()[2] << std::endl;
409 
410  if (0) {
411  std::list<vpImagePoint> edges = d.getEdges();
412  std::list<vpImagePoint>::const_iterator it;
413  for (it = edges.begin(); it != edges.end(); ++it) {
415  }
416  }
417 
418  if (opt_display) {
419  // display a red cross (size 10) in the image at the dot center
420  // of gravity location
422 
423  vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
424  if (vpDisplay::getClick(I, false)) {
425  quit = true;
426  }
427  // flush the X11 buffer
428  vpDisplay::flush(I);
429  }
430  iter += opt_step;
431  }
432 
433  if (opt_display && opt_click_allowed && !quit) {
434  std::cout << "\nA click to exit..." << std::endl;
435  // Wait for a blocking mouse click
437  }
438  if (display) {
439  delete display;
440  }
441  return EXIT_SUCCESS;
442  }
443  catch (const vpException &e) {
444  std::cout << "Catch an exception: " << e << std::endl;
445  return EXIT_FAILURE;
446  }
447 }
448 #else
449 #include <iostream>
450 
451 int main()
452 {
453  std::cout << "visp_blob module or X11, GTK, GDI or OpenCV display "
454  "functionalities are required..."
455  << std::endl;
456  return EXIT_SUCCESS;
457 }
458 
459 #endif
static const vpColor red
Definition: vpColor.h:198
static const vpColor blue
Definition: vpColor.h:204
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 dot (connected pixels with same gray level) on a vpImage.
Definition: vpDot.h:116
unsigned int getWidth() const
Definition: vpDot.h:290
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:630
void setGraphics(bool activate)
Definition: vpDot.h:354
void setConnexity(const vpConnexityType &connexityType)
Definition: vpDot.h:334
double getArea() const
Definition: vpDot.h:227
std::list< vpImagePoint > getEdges() const
Definition: vpDot.h:255
void setComputeMoments(bool activate)
Definition: vpDot.h:329
vpColVector get_nij() const
Definition: vpDot.h:209
@ CONNEXITY_8
Definition: vpDot.h:125
vpImagePoint getCog() const
Definition: vpDot.h:247
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:760
unsigned int getHeight() const
Definition: vpDot.h:297
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()