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