Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
trackMeNurbs.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 a nurbs.
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_ME) && \
57  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
58 
59 #include <visp3/core/vpColor.h>
60 #include <visp3/core/vpImage.h>
61 #include <visp3/core/vpImagePoint.h>
62 #include <visp3/gui/vpDisplayGDI.h>
63 #include <visp3/gui/vpDisplayGTK.h>
64 #include <visp3/gui/vpDisplayOpenCV.h>
65 #include <visp3/gui/vpDisplayX.h>
66 #include <visp3/io/vpImageIo.h>
67 
68 #include <visp3/core/vpIoTools.h>
69 #include <visp3/io/vpParseArgv.h>
70 #include <visp3/io/vpVideoReader.h>
71 #include <visp3/me/vpMeNurbs.h>
72 
73 // List of allowed command line options
74 #define GETOPTARGS "cdi:h"
75 
76 #ifdef ENABLE_VISP_NAMESPACE
77 using namespace VISP_NAMESPACE_NAME;
78 #endif
79 
80 void usage(const char *name, const char *badparam, std::string ipath);
81 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
82 
92 void usage(const char *name, const char *badparam, std::string ipath)
93 {
94 #if VISP_HAVE_DATASET_VERSION >= 0x030600
95  std::string ext("png");
96 #else
97  std::string ext("pgm");
98 #endif
99  fprintf(stdout, "\n\
100 Tracking of a nurbs using vpMe.\n\
101 \n\
102 SYNOPSIS\n\
103  %s [-i <input image path>] [-c] [-d] [-h]\n",
104  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  \"ellipse-1/image.%%04d.%s\"\n\
112  Setting the VISP_INPUT_IMAGE_PATH environment\n\
113  variable produces the same behaviour than using\n\
114  this option.\n\
115 \n\
116  -c\n\
117  Disable the mouse click. Useful to automate the \n\
118  execution of this program without human intervention.\n\
119 \n\
120  -d \n\
121  Turn off the display.\n\
122 \n\
123  -h\n\
124  Print the help.\n",
125  ipath.c_str(), ext.c_str());
126 
127  if (badparam)
128  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
129 }
143 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
144 {
145  const char *optarg_;
146  int c;
147  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
148 
149  switch (c) {
150  case 'c':
151  click_allowed = false;
152  break;
153  case 'd':
154  display = false;
155  break;
156  case 'i':
157  ipath = optarg_;
158  break;
159  case 'h':
160  usage(argv[0], nullptr, ipath);
161  return false;
162  break;
163 
164  default:
165  usage(argv[0], optarg_, ipath);
166  return false;
167  break;
168  }
169  }
170 
171  if ((c == 1) || (c == -1)) {
172  // standalone param or error
173  usage(argv[0], nullptr, ipath);
174  std::cerr << "ERROR: " << std::endl;
175  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
176  return false;
177  }
178 
179  return true;
180 }
181 
182 int main(int argc, const char **argv)
183 {
184  try {
185  std::string env_ipath;
186  std::string opt_ipath;
187  std::string ipath;
188  std::string filename;
189  bool opt_click_allowed = true;
190  bool opt_display = true;
191 
192 #if VISP_HAVE_DATASET_VERSION >= 0x030600
193  std::string ext("png");
194 #else
195  std::string ext("pgm");
196 #endif
197 
198  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
199  // environment variable value
200  env_ipath = vpIoTools::getViSPImagesDataPath();
201 
202  // Set the default input path
203  if (!env_ipath.empty())
204  ipath = env_ipath;
205 
206  // Read the command line options
207  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
208  return EXIT_FAILURE;
209  }
210 
211  // Get the option values
212  if (!opt_ipath.empty())
213  ipath = opt_ipath;
214 
215  // Compare ipath and env_ipath. If they differ, we take into account
216  // the input path coming from the command line option
217  if (!opt_ipath.empty() && !env_ipath.empty()) {
218  if (ipath != env_ipath) {
219  std::cout << std::endl << "WARNING: " << std::endl;
220  std::cout << " Since -i <visp image path=" << ipath << "> "
221  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
222  << " we skip the environment variable." << std::endl;
223  }
224  }
225 
226  // Test if an input path is set
227  if (opt_ipath.empty() && env_ipath.empty()) {
228  usage(argv[0], nullptr, ipath);
229  std::cerr << std::endl << "ERROR:" << std::endl;
230  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
231  << " environment variable to specify the location of the " << std::endl
232  << " image path where test images are located." << std::endl
233  << std::endl;
234  return EXIT_FAILURE;
235  }
236 
237  // Declare an image, this is a gray level image (unsigned char)
238  // it size is not defined yet, it will be defined when the image is
239  // read on the disk
241 
242  // Set the path location of the image sequence
243  filename = vpIoTools::createFilePath(ipath, "ellipse-1/image.%04d." + ext);
244 
245  // Build the name of the image file
246  vpVideoReader reader;
247  // Initialize the reader and get the first frame.
248  reader.setFileName(filename.c_str());
249  reader.setFirstFrameIndex(1);
250  reader.open(I);
251 
252  // We open a window using either X11, GTK or GDI.
253 #if defined(VISP_HAVE_X11)
254  vpDisplayX display;
255 #elif defined(VISP_HAVE_GTK)
256  vpDisplayGTK display;
257 #elif defined(VISP_HAVE_GDI)
258  vpDisplayGDI display;
259 #elif defined(HAVE_OPENCV_HIGHGUI)
260  vpDisplayOpenCV display;
261 #endif
262 
263  if (opt_display) {
264  // Display size is automatically defined by the image (I) size
265  display.init(I, 100, 100, "Display...");
266  // Display the image
267  // The image class has a member that specify a pointer toward
268  // the display that has been initialized in the display declaration
269  // therefore is is no longer necessary to make a reference to the
270  // display variable.
272  vpDisplay::flush(I);
273  }
274 
275  vpMeNurbs nurbs;
276 
277  vpMe me;
278  me.setRange(30);
279  me.setSampleStep(5);
280  me.setPointsToTrack(60);
282  me.setThreshold(20);
283 
284  nurbs.setMe(&me);
286  nurbs.setNbControlPoints(14);
287 
288  if (opt_click_allowed) {
289  std::cout << "Click on points along the edge with the left button." << std::endl;
290  std::cout << "Then click on the right button to continue." << std::endl;
291  nurbs.initTracking(I);
292  }
293  else {
294  // Create a list of points to automate the test
295  std::list<vpImagePoint> list;
296  list.push_back(vpImagePoint(178, 357));
297  list.push_back(vpImagePoint(212, 287));
298  list.push_back(vpImagePoint(236, 210));
299  list.push_back(vpImagePoint(240, 118));
300  list.push_back(vpImagePoint(210, 40));
301 
302  nurbs.initTracking(I, list);
303  }
304  if (opt_display) {
305  nurbs.display(I, vpColor::green);
306  }
307 
308  nurbs.track(I);
309  if (opt_display && opt_click_allowed) {
310  std::cout << "A click to continue..." << std::endl;
312  }
313  std::cout << "------------------------------------------------------------" << std::endl;
314 
315  for (int iter = 1; iter < 40; iter++) {
316  // read the image
317  reader.getFrame(I, iter);
318  if (opt_display) {
319  // Display the image
321  }
322 
323  // Track the nurbs
324  nurbs.track(I);
325 
326  if (opt_display) {
327  nurbs.display(I, vpColor::green);
328  vpDisplay::flush(I);
329  vpTime::wait(100);
330  }
331  }
332  if (opt_display && opt_click_allowed) {
333  std::cout << "A click to exit..." << std::endl;
335  }
336  return EXIT_SUCCESS;
337  }
338  catch (vpException &e) {
339  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
340  return EXIT_SUCCESS;
341  }
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  }
352 
353 #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
const char * getMessage() const
Definition: vpException.cpp:65
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 in an image a edge defined by a Nurbs.
Definition: vpMeNurbs.h:133
void track(const vpImage< unsigned char > &I)
Definition: vpMeNurbs.cpp:812
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeNurbs.cpp:202
void setNbControlPoints(unsigned int nb_point)
Definition: vpMeNurbs.h:177
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1)
Definition: vpMeNurbs.cpp:858
@ RANGE_RESULT
Definition: vpMeSite.h:78
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:232
void setMe(vpMe *me)
Definition: vpMeTracker.h:260
Definition: vpMe.h:134
void setPointsToTrack(const int &points_to_track)
Definition: vpMe.h:408
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
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:145
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void setFirstFrameIndex(const long first_frame)
bool getFrame(vpImage< vpRGBa > &I, long frame)
VISP_EXPORT int wait(double t0, double t)