Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
trackMeCircle.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  * Tracking of an ellipse.
32  */
33 
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpDebug.h>
48 
49 #include <iomanip>
50 #include <sstream>
51 #include <stdio.h>
52 #include <stdlib.h>
53 
54 #if defined(VISP_HAVE_MODULE_ME) && \
55  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
56 
57 #include <visp3/core/vpColor.h>
58 #include <visp3/core/vpImage.h>
59 #include <visp3/core/vpIoTools.h>
60 #include <visp3/gui/vpDisplayGDI.h>
61 #include <visp3/gui/vpDisplayGTK.h>
62 #include <visp3/gui/vpDisplayOpenCV.h>
63 #include <visp3/gui/vpDisplayX.h>
64 #include <visp3/io/vpImageIo.h>
65 #include <visp3/io/vpParseArgv.h>
66 #include <visp3/me/vpMeEllipse.h>
67 
68 // List of allowed command line options
69 #define GETOPTARGS "cdi:h"
70 
71 #ifdef ENABLE_VISP_NAMESPACE
72 using namespace VISP_NAMESPACE_NAME;
73 #endif
74 
75 void usage(const char *name, const char *badparam, std::string ipath);
76 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
77 
87 void usage(const char *name, const char *badparam, std::string ipath)
88 {
89 #if VISP_HAVE_DATASET_VERSION >= 0x030600
90  std::string ext("png");
91 #else
92  std::string ext("pgm");
93 #endif
94  fprintf(stdout, "\n\
95 Test auto detection of dots using vpDot2.\n\
96 \n\
97 SYNOPSIS\n\
98  %s [-i <input image path>] [-c] [-d] [-h]\n",
99  name);
100 
101  fprintf(stdout, "\n\
102 OPTIONS: Default\n\
103  -i <input image path> %s\n\
104  Set image input path.\n\
105  From this path read \"circle/circle.%s\"\n\
106  image. \n\
107  Setting the VISP_INPUT_IMAGE_PATH environment\n\
108  variable produces the same behaviour than using\n\
109  this option.\n\
110 \n\
111  -c\n\
112  Disable the mouse click. Useful to automate the \n\
113  execution of this program without human intervention.\n\
114 \n\
115  -d \n\
116  Turn off the display.\n\
117 \n\
118  -h\n\
119  Print the help.\n",
120  ipath.c_str(), ext.c_str());
121 
122  if (badparam)
123  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
124 }
138 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
139 {
140  const char *optarg_;
141  int c;
142  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
143 
144  switch (c) {
145  case 'c':
146  click_allowed = false;
147  break;
148  case 'd':
149  display = false;
150  break;
151  case 'i':
152  ipath = optarg_;
153  break;
154  case 'h':
155  usage(argv[0], nullptr, ipath);
156  return false;
157  break;
158 
159  default:
160  usage(argv[0], optarg_, ipath);
161  return false;
162  break;
163  }
164  }
165 
166  if ((c == 1) || (c == -1)) {
167  // standalone param or error
168  usage(argv[0], nullptr, ipath);
169  std::cerr << "ERROR: " << std::endl;
170  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
171  return false;
172  }
173 
174  return true;
175 }
176 
177 int main(int argc, const char **argv)
178 {
179 #if defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV)
180  try {
181  std::string env_ipath;
182  std::string opt_ipath;
183  std::string ipath;
184  std::string dirname;
185  std::string filename;
186  bool opt_click_allowed = true;
187  bool opt_display = true;
188 
189 #if VISP_HAVE_DATASET_VERSION >= 0x030600
190  std::string ext("png");
191 #else
192  std::string ext("pgm");
193 #endif
194 
195  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
196  // environment variable value
197  env_ipath = vpIoTools::getViSPImagesDataPath();
198 
199  // Set the default input path
200  if (!env_ipath.empty())
201  ipath = env_ipath;
202 
203  // Read the command line options
204  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
205  return EXIT_FAILURE;
206  }
207 
208  // Get the option values
209  if (!opt_ipath.empty())
210  ipath = opt_ipath;
211 
212  // Compare ipath and env_ipath. If they differ, we take into account
213  // the input path coming from the command line option
214  if (!opt_ipath.empty() && !env_ipath.empty()) {
215  if (ipath != env_ipath) {
216  std::cout << std::endl << "WARNING: " << std::endl;
217  std::cout << " Since -i <visp image path=" << ipath << "> "
218  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
219  << " we skip the environment variable." << std::endl;
220  }
221  }
222 
223  // Test if an input path is set
224  if (opt_ipath.empty() && env_ipath.empty()) {
225  usage(argv[0], nullptr, ipath);
226  std::cerr << std::endl << "ERROR:" << std::endl;
227  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
228  << " environment variable to specify the location of the " << std::endl
229  << " image path where test images are located." << std::endl
230  << std::endl;
231  return EXIT_FAILURE;
232  }
233 
234  // Declare an image, this is a gray level image (unsigned char)
235  // it size is not defined yet, it will be defined when the image will
236  // read on the disk
238 
239  // Set the path location of the image sequence
240  dirname = vpIoTools::createFilePath(ipath, "circle");
241 
242  // Build the name of the image file
243  filename = vpIoTools::createFilePath(dirname, "circle." + ext);
244 
245  // Read the image into the image structure I. I is initialized to the correct size
246  // vpImageIo::read() may throw various exception if, for example,
247  // the file does not exist, or if the memory cannot be allocated
248  try {
249  vpCTRACE << "Load: " << filename << std::endl;
250 
251  vpImageIo::read(I, filename);
252  }
253  catch (...) {
254  // If an exception is throwned it is catched here and will result in the end of the program.
255  // Note that another error message can be printed from vpImageIo::read() to give more
256  // information about the error
257  std::cerr << std::endl << "ERROR:" << std::endl;
258  std::cerr << " Cannot read " << filename << std::endl;
259  std::cerr << " Check your -i " << ipath << " option " << std::endl
260  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
261  return EXIT_FAILURE;
262  }
263 
264  // We open a window using either X11, GTK or GDI.
265 #if defined(VISP_HAVE_X11)
266  vpDisplayX display;
267 #elif defined(VISP_HAVE_GTK)
268  vpDisplayGTK display;
269 #elif defined(VISP_HAVE_GDI)
270  vpDisplayGDI display;
271 #elif defined(HAVE_OPENCV_HIGHGUI)
272  vpDisplayOpenCV display;
273 #endif
274 
275  if (opt_display) {
276  // Display size is automatically defined by the image (I) size
277  display.init(I, 100, 100, "Display...");
278  // Display the image
279  // The image class has a member that specify a pointer toward
280  // the display that has been initialized in the display declaration
281  // therefore is is no longer necessary to make a reference to the
282  // display variable.
284  vpDisplay::flush(I);
285  }
286 
287  vpMeEllipse E1;
288 
289  vpMe me;
290  me.setRange(20);
291  me.setSampleStep(2);
293  me.setThreshold(20);
294 
295  E1.setMe(&me);
297  // If click is allowed, wait for a mouse click to select the points
298  // on the ellipse
299  if (opt_display && opt_click_allowed) {
300  E1.initTracking(I);
301  }
302  else {
303  // Create a list of points to automate the test
304  std::vector<vpImagePoint> ip;
305  ip.push_back(vpImagePoint(78, 203));
306  ip.push_back(vpImagePoint(62, 125));
307  ip.push_back(vpImagePoint(128, 101));
308  ip.push_back(vpImagePoint(167, 147));
309  ip.push_back(vpImagePoint(147, 200));
310 
311  E1.initTracking(I, ip);
312  }
313 
314  if (opt_display) {
315  E1.display(I, vpColor::green);
316  vpDisplay::flush(I);
317  }
318 
319  vpTRACE("sample step %f ", E1.getMe()->getSampleStep());
320  std::cout << "Tracking on image: " << filename << std::endl;
321  E1.track(I);
322  if (opt_display) {
323  vpDisplay::flush(I);
324  }
325 
326  if (opt_display && opt_click_allowed) {
327  std::cout << "A click to exit..." << std::endl;
329  }
330  std::cout << "------------------------------------------------------------" << std::endl;
331  return EXIT_SUCCESS;
332  }
333  catch (const vpException &e) {
334  std::cout << "Catch an exception: " << e << std::endl;
335  return EXIT_FAILURE;
336  }
337 #else
338  (void)argc;
339  (void)argv;
340  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
341 #endif
342 }
343 #else
344 #include <iostream>
345 
346 int main()
347 {
348  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
349  "functionalities are required..."
350  << std::endl;
351  return EXIT_SUCCESS;
352 }
353 
354 #endif
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 flush(const vpImage< unsigned char > &I)
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
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
Class that tracks an ellipse using moving edges.
Definition: vpMeEllipse.h:96
void display(const vpImage< unsigned char > &I, const vpColor &col, unsigned int thickness=1)
void initTracking(const vpImage< unsigned char > &I, bool trackCircle=false, bool trackArc=false)
void track(const vpImage< unsigned char > &I)
@ RANGE_RESULT
Definition: vpMeSite.h:78
vpMe * getMe()
Definition: vpMeTracker.h:153
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:232
void setMe(vpMe *me)
Definition: vpMeTracker.h:260
Definition: vpMe.h:134
void setRange(const unsigned int &range)
Definition: vpMe.h:415
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:505
void setThreshold(const double &threshold)
Definition: vpMe.h:466
void setSampleStep(const double &sample_step)
Definition: vpMe.h:422
double getSampleStep() const
Definition: vpMe.h:275
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:145
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70