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