Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testTrackDot.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  * Test auto detection of dots.
32  */
33 
34 #include <iomanip>
35 #include <sstream>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpDebug.h>
40 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
41 
42 #include <visp3/blob/vpDot2.h>
43 #include <visp3/core/vpCameraParameters.h>
44 #include <visp3/core/vpImage.h>
45 #include <visp3/core/vpIoTools.h>
46 #include <visp3/gui/vpDisplayGDI.h>
47 #include <visp3/gui/vpDisplayGTK.h>
48 #include <visp3/gui/vpDisplayX.h>
49 #include <visp3/io/vpImageIo.h>
50 #include <visp3/io/vpParseArgv.h>
51 #ifdef VISP_HAVE_MODULE_FEATURES
52 #include <visp3/visual_features/vpFeatureBuilder.h>
53 #include <visp3/visual_features/vpFeatureEllipse.h>
54 #endif
55 
62 // List of allowed command line options
63 #define GETOPTARGS "cdi:h"
64 
65 #ifdef ENABLE_VISP_NAMESPACE
66 using namespace VISP_NAMESPACE_NAME;
67 #endif
68 
69 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
70 
71 void usage(const char *name, const char *badparam, std::string ipath);
81 void usage(const char *name, const char *badparam, std::string ipath)
82 {
83 #if VISP_HAVE_DATASET_VERSION >= 0x030600
84  std::string ext("png");
85 #else
86  std::string ext("pgm");
87 #endif
88  fprintf(stdout, "\n\
89 Test dot tracking.\n\
90 \n\
91 SYNOPSIS\n\
92  %s [-i <input image path>] [-c] [-d] [-h]\n",
93  name);
94 
95  fprintf(stdout, "\n\
96 OPTIONS: Default\n\
97  -i <input image path> %s\n\
98  Set image input path.\n\
99  From this path read image \n\
100  \"ellipse/ellipse.%s\"\n\
101  Setting the VISP_INPUT_IMAGE_PATH environment\n\
102  variable produces the same behaviour than using\n\
103  this option.\n\
104 \n\
105  -c\n\
106  Disable the mouse click. Useful to automate the \n\
107  execution of this program without human intervention.\n\
108 \n\
109  -d \n\
110  Turn off the display.\n\
111 \n\
112  -h\n\
113  Print the help.\n",
114  ipath.c_str(), ext.c_str());
115 
116  if (badparam)
117  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
118 }
131 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
132 {
133  const char *optarg_;
134  int c;
135  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
136 
137  switch (c) {
138  case 'c':
139  click_allowed = false;
140  break;
141  case 'd':
142  display = false;
143  break;
144  case 'i':
145  ipath = optarg_;
146  break;
147  case 'h':
148  usage(argv[0], nullptr, ipath);
149  return false;
150  break;
151 
152  default:
153  usage(argv[0], optarg_, ipath);
154  return false;
155  break;
156  }
157  }
158 
159  if ((c == 1) || (c == -1)) {
160  // standalone param or error
161  usage(argv[0], nullptr, ipath);
162  std::cerr << "ERROR: " << std::endl;
163  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
164  return false;
165  }
166 
167  return true;
168 }
169 
170 int main(int argc, const char **argv)
171 {
172  try {
173  std::string env_ipath;
174  std::string opt_ipath;
175  std::string ipath;
176  std::string dirname;
177  std::string filename;
178  bool opt_click_allowed = true;
179  bool opt_display = true;
180 
181 #if VISP_HAVE_DATASET_VERSION >= 0x030600
182  std::string ext("png");
183 #else
184  std::string ext("pgm");
185 #endif
186 
187  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
188  // environment variable value
189  env_ipath = vpIoTools::getViSPImagesDataPath();
190 
191  // Set the default input path
192  if (!env_ipath.empty())
193  ipath = env_ipath;
194 
195  // Read the command line options
196  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
197  return EXIT_FAILURE;
198  }
199 
200  // Get the option values
201  if (!opt_ipath.empty())
202  ipath = opt_ipath;
203 
204  // Compare ipath and env_ipath. If they differ, we take into account
205  // the input path coming from the command line option
206  if (!opt_ipath.empty() && !env_ipath.empty()) {
207  if (ipath != env_ipath) {
208  std::cout << std::endl << "WARNING: " << std::endl;
209  std::cout << " Since -i <visp image path=" << ipath << "> "
210  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
211  << " we skip the environment variable." << std::endl;
212  }
213  }
214 
215  // Test if an input path is set
216  if (opt_ipath.empty() && env_ipath.empty()) {
217  usage(argv[0], nullptr, ipath);
218  std::cerr << std::endl << "ERROR:" << std::endl;
219  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
220  << " environment variable to specify the location of the " << std::endl
221  << " image path where test images are located." << std::endl
222  << std::endl;
223  return EXIT_FAILURE;
224  }
225 
226  // Declare an image, this is a gray level image (unsigned char)
227  // it size is not defined yet, it will be defined when the image will
228  // read on the disk
230 
231  // Set the path location of the image sequence
232  dirname = vpIoTools::createFilePath(ipath, "ellipse");
233 
234  // Build the name of the image file
235  filename = vpIoTools::createFilePath(dirname, "ellipse." + ext);
236 
237  // Read image named "filename" and put the bitmap in I
238  try {
239  vpCTRACE << "Load: " << filename << std::endl;
240 
241  vpImageIo::read(I, filename);
242  }
243  catch (...) {
244  std::cerr << std::endl << "ERROR:" << std::endl;
245  std::cerr << " Cannot read " << filename << std::endl;
246  std::cerr << " Check your -i " << ipath << " option " << std::endl
247  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
248  return EXIT_FAILURE;
249  }
250 
251 // We open a window using either X11, GTK or GDI.
252 #if defined(VISP_HAVE_X11)
253  vpDisplayX display;
254 #elif defined(VISP_HAVE_GTK)
255  vpDisplayGTK display;
256 #elif defined(VISP_HAVE_GDI)
257  vpDisplayGDI display;
258 #endif
259 
260  if (opt_display) {
261  // Display size is automatically defined by the image (I) size
262  display.init(I, 100, 100, "Display...");
263  // Display the image
264  // The image class has a member that specify a pointer toward
265  // the display that has been initialized in the display declaration
266  // therefore is is no longer necessary to make a reference to the
267  // display variable.
269  // Flush the display
270  vpDisplay::flush(I);
271  }
272 
273  vpDot2 dot;
274  std::cout << "debut 1\n";
275  // dot.setMaxDotSize(0.50); // dot max size = 50% of the image size
276  vpImagePoint ip;
277  ip.set_i(140);
278  ip.set_j(140);
279  dot.initTracking(I, ip);
280  if (opt_display) {
281  dot.setGraphics(true);
282  }
283  else {
284  dot.setGraphics(false);
285  }
286  dot.setComputeMoments(true);
287  dot.track(I);
288 
289  vpCameraParameters cam;
290 
291 #ifdef VISP_HAVE_MODULE_FEATURES
293  vpFeatureBuilder::create(e, cam, dot);
294 #endif
295  if (opt_display) {
296 #ifdef VISP_HAVE_MODULE_FEATURES
297  e.display(cam, I, vpColor::red);
298 #endif
299  vpDisplay::flush(I);
300  if (opt_click_allowed) {
301  std::cout << "A click to exit..." << std::endl;
303  }
304  }
305  return EXIT_SUCCESS;
306  }
307  catch (const vpException &e) {
308  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
309  return EXIT_FAILURE;
310  }
311 }
312 #else
313 int main() { vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities..."); }
314 
315 #endif
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition: vpColor.h:217
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
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:125
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:452
void setGraphics(bool activate)
Definition: vpDot2.h:318
void setComputeMoments(bool activate)
Definition: vpDot2.h:276
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:269
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * getMessage() const
Definition: vpException.cpp:65
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines 2D ellipse visual feature.
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const VP_OVERRIDE
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
void set_j(double jj)
Definition: vpImagePoint.h:309
void set_i(double ii)
Definition: vpImagePoint.h:298
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
#define vpCTRACE
Definition: vpDebug.h:348
#define vpERROR_TRACE
Definition: vpDebug.h:409