Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
trackDot.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 *****************************************************************************/
35 
48 #include <visp3/core/vpConfig.h>
49 #include <visp3/core/vpDebug.h>
50 
51 #include <iomanip>
52 #include <sstream>
53 #include <stdio.h>
54 #include <stdlib.h>
55 
56 #if defined(VISP_HAVE_MODULE_BLOB) && \
57  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
58 
59 #include <visp3/blob/vpDot.h>
60 #include <visp3/core/vpImage.h>
61 #include <visp3/core/vpImagePoint.h>
62 #include <visp3/core/vpIoTools.h>
63 #include <visp3/gui/vpDisplayGDI.h>
64 #include <visp3/gui/vpDisplayGTK.h>
65 #include <visp3/gui/vpDisplayOpenCV.h>
66 #include <visp3/gui/vpDisplayX.h>
67 #include <visp3/io/vpImageIo.h>
68 #include <visp3/io/vpParseArgv.h>
69 
70 // List of allowed command line options
71 #define GETOPTARGS "cdf:hi:l:p:s:"
72 
73 #ifdef ENABLE_VISP_NAMESPACE
74 using namespace VISP_NAMESPACE_NAME;
75 #endif
76 
90 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first,
91  unsigned last, unsigned step)
92 {
93 #if VISP_HAVE_DATASET_VERSION >= 0x030600
94  std::string ext("png");
95 #else
96  std::string ext("pgm");
97 #endif
98  fprintf(stdout, "\n\
99 Test dot tracking.\n\
100 \n\
101 SYNOPSIS\n\
102  %s [-i <test image path>] [-p <personal image path>]\n\
103  [-f <first image>] [-l <last image>] [-s <step>]\n\
104  [-c] [-d] [-h]\n", name);
105 
106  fprintf(stdout, "\n\
107 OPTIONS: Default\n\
108  -i <input image path> %s\n\
109  Set image input path.\n\
110  From this path read images \n\
111  \"mire-2/image.%%04d.%s\". These \n\
112  images come from visp-images-x.y.z.tar.gz available \n\
113  on the ViSP website.\n\
114  Setting the VISP_INPUT_IMAGE_PATH environment\n\
115  variable produces the same behaviour than using\n\
116  this option.\n\
117 \n\
118  -p <personal image path> %s\n\
119  Specify a personal sequence containing images \n\
120  to process.\n\
121  By image sequence, we mean one file per image.\n\
122  Example : \"C:/Temp/visp-images/cube/image.%%04d.%s\"\n\
123  %%04d is for the image numbering.\n\
124 \n\
125  -f <first image> %u\n\
126  First image number of the sequence.\n\
127 \n\
128  -l <last image> %u\n\
129  Last image number of the sequence.\n\
130 \n\
131  -s <step> %u\n\
132  Step between two images.\n\
133 \n\
134  -c\n\
135  Disable the mouse click. Useful to automate the \n\
136  execution of this program without human intervention.\n\
137 \n\
138  -d \n\
139  Turn off the display.\n\
140 \n\
141  -h\n\
142  Print the help.\n",
143  ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
144 
145  if (badparam)
146  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
147 }
165 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
166  unsigned &step, bool &click_allowed, bool &display)
167 {
168  const char *optarg_;
169  int c;
170  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
171 
172  switch (c) {
173  case 'c':
174  click_allowed = false;
175  break;
176  case 'd':
177  display = false;
178  break;
179  case 'i':
180  ipath = optarg_;
181  break;
182  case 'p':
183  ppath = optarg_;
184  break;
185  case 'f':
186  first = (unsigned)atoi(optarg_);
187  break;
188  case 'l':
189  last = (unsigned)atoi(optarg_);
190  break;
191  case 's':
192  step = (unsigned)atoi(optarg_);
193  break;
194  case 'h':
195  usage(argv[0], nullptr, ipath, ppath, first, last, step);
196  return false;
197  break;
198 
199  default:
200  usage(argv[0], optarg_, ipath, ppath, first, last, step);
201  return false;
202  break;
203  }
204  }
205 
206  if ((c == 1) || (c == -1)) {
207  // standalone param or error
208  usage(argv[0], nullptr, ipath, ppath, first, last, step);
209  std::cerr << "ERROR: " << std::endl;
210  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
211  return false;
212  }
213 
214  return true;
215 }
216 
217 int main(int argc, const char **argv)
218 {
219  try {
220  std::string env_ipath;
221  std::string opt_ipath;
222  std::string ipath;
223  std::string opt_ppath;
224  std::string dirname;
225  std::string filename;
226  unsigned int opt_first = 1;
227  unsigned int opt_last = 500;
228  unsigned int opt_step = 1;
229  bool opt_click_allowed = true;
230  bool opt_display = true;
231 
232 #if VISP_HAVE_DATASET_VERSION >= 0x030600
233  std::string ext("png");
234 #else
235  std::string ext("pgm");
236 #endif
237 
238  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
239  // environment variable value
240  env_ipath = vpIoTools::getViSPImagesDataPath();
241 
242  // Set the default input path
243  if (!env_ipath.empty())
244  ipath = env_ipath;
245 
246  // Read the command line options
247  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
248  opt_display) == false) {
249  return EXIT_FAILURE;
250  }
251 
252  // Get the option values
253  if (!opt_ipath.empty())
254  ipath = opt_ipath;
255 
256  // Compare ipath and env_ipath. If they differ, we take into account
257  // the input path coming from the command line option
258  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
259  if (ipath != env_ipath) {
260  std::cout << std::endl << "WARNING: " << std::endl;
261  std::cout << " Since -i <visp image path=" << ipath << "> "
262  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
263  << " we skip the environment variable." << std::endl;
264  }
265  }
266 
267  // Test if an input path is set
268  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
269  usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
270  std::cerr << std::endl << "ERROR:" << std::endl;
271  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
272  << " environment variable to specify the location of the " << std::endl
273  << " image path where test images are located." << std::endl
274  << " Use -p <personal image path> option if you want to " << std::endl
275  << " use personal images." << std::endl
276  << std::endl;
277 
278  return EXIT_FAILURE;
279  }
280 
281  // Declare an image, this is a gray level image (unsigned char)
282  // it size is not defined yet, it will be defined when the image will
283  // read on the disk
285 
286  unsigned iter = opt_first;
287  std::ostringstream s;
288  char cfilename[FILENAME_MAX];
289 
290  if (opt_ppath.empty()) {
291 
292  // Warning :
293  // The image sequence is not provided with the ViSP package
294  // therefore the program will return an error :
295  // !! couldn't read file visp-images/mire-2/image.0001.png
296  //
297  // ViSP dataset is available on the visp www site
298  // https://visp.inria.fr/download/.
299 
300  // Set the path location of the image sequence
301  dirname = vpIoTools::createFilePath(ipath, "mire-2");
302 
303  // Build the name of the image file
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 
360  // by using setGraphics, we request to see the all the pixel of the dot
361  vpDot d;
362  if (opt_display) {
363  // by using setGraphics, we request to see the all the pixel of the dot
364  // in green on the screen.
365  // It uses the overlay image plane.
366  // The default of this setting is that it is time consuming
367  d.setGraphics(true);
368  }
369  else {
370  d.setGraphics(false);
371  }
372  // we also request to compute the dot moment m00, m10, m01, m11, m20, m02
373  d.setComputeMoments(true);
374  d.setConnexity(vpDot::CONNEXITY_8);
375 
376  if (opt_display && opt_click_allowed) {
377  // tracking is initalized
378  // if no other parameters are given to the iniTracking(..) method
379  // a right mouse click on the dot is expected
380  std::cout << "Click on a white dot you want to track..." << std::endl;
381  d.initTracking(I);
382  }
383  else {
384  // dot location can also be specified explicitly in the
385  // initTracking method : d.initTracking(I,ip) where ip is the
386  // image point from which the dot is searched
387  vpImagePoint ip;
388  ip.set_u(160);
389  ip.set_v(212);
390  d.initTracking(I, ip);
391  }
392 
393  vpImagePoint cog;
394  while (iter < opt_last) {
395  // set the new image name
396  if (opt_ppath.empty()) {
397  s.str("");
398  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
399  filename = vpIoTools::createFilePath(dirname, s.str());
400  }
401  else {
402  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
403  filename = cfilename;
404  }
405  // read the image
406  std::cout << "read: " << filename << std::endl;
407  vpImageIo::read(I, filename);
408 
409  if (opt_display) {
410  // Display the image
412  }
413  std::cout << "Tracking on image: " << filename << std::endl;
414 
415  // track the dot
416  double time = vpTime::measureTimeMs();
417  d.track(I);
418 
419  std::cout << "COG (" << vpTime::measureTimeMs() - time << " ms): " << std::endl;
420  cog = d.getCog();
421  std::cout << cog.get_u() << " " << cog.get_v() << std::endl;
422  std::cout << "Size:" << std::endl;
423  std::cout << " w: " << d.getWidth() << " h: " << d.getHeight() << std::endl;
424  std::cout << "Area: " << d.getArea() << std::endl;
425  std::cout << "Centered normalized moments nij:" << std::endl;
426  std::cout << " n20: " << d.get_nij()[0] << std::endl;
427  std::cout << " n11: " << d.get_nij()[1] << std::endl;
428  std::cout << " n02: " << d.get_nij()[2] << std::endl;
429 
430  if (0) {
431  std::list<vpImagePoint> edges = d.getEdges();
432  std::list<vpImagePoint>::const_iterator it;
433  for (it = edges.begin(); it != edges.end(); ++it) {
435  }
436  }
437 
438  if (opt_display) {
439  // display a red cross (size 10) in the image at the dot center
440  // of gravity location
442  // flush the X11 buffer
443  vpDisplay::flush(I);
444  }
445  iter += opt_step;
446  }
447 
448  if (opt_display && opt_click_allowed) {
449  std::cout << "\nA click to exit..." << std::endl;
450  // Wait for a blocking mouse click
452  }
453  return EXIT_SUCCESS;
454  }
455  catch (const vpException &e) {
456  std::cout << "Catch an exception: " << e << std::endl;
457  return EXIT_FAILURE;
458  }
459 }
460 #else
461 #include <iostream>
462 
463 int main()
464 {
465  std::cout << "visp_blob module or X11, GTK, GDI or OpenCV display "
466  "functionalities are required..."
467  << std::endl;
468  return EXIT_SUCCESS;
469  }
470 
471 #endif
static const vpColor red
Definition: vpColor.h:217
static const vpColor blue
Definition: vpColor.h:223
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 dot (connected pixels with same gray level) on a vpImage.
Definition: vpDot.h:116
@ CONNEXITY_8
Definition: vpDot.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()