Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
trackMeLine.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 a line.
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/vpImagePoint.h>
60 #include <visp3/core/vpIoTools.h>
61 #include <visp3/gui/vpDisplayGDI.h>
62 #include <visp3/gui/vpDisplayGTK.h>
63 #include <visp3/gui/vpDisplayOpenCV.h>
64 #include <visp3/gui/vpDisplayX.h>
65 #include <visp3/io/vpImageIo.h>
66 #include <visp3/io/vpVideoReader.h>
67 #include <visp3/io/vpParseArgv.h>
68 #include <visp3/me/vpMeLine.h>
69 #include <visp3/visual_features/vpFeatureBuilder.h>
70 #include <visp3/visual_features/vpFeatureLine.h>
71 
72 // List of allowed command line options
73 #define GETOPTARGS "cdf:hi:l:p:s:"
74 
75 #ifdef ENABLE_VISP_NAMESPACE
76 using namespace VISP_NAMESPACE_NAME;
77 #endif
78 
92 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first,
93  unsigned last, unsigned step)
94 {
95 #if VISP_HAVE_DATASET_VERSION >= 0x030600
96  std::string ext("png");
97 #else
98  std::string ext("pgm");
99 #endif
100  fprintf(stdout, "\n\
101 Tracking of a line.\n\
102 \n\
103 SYNOPSIS\n\
104  %s [-i <input image path>] [-p <personal image path>]\n\
105  [-f <first image>] [-l <last image>] [-s <step>]\n\
106  [-c] [-d] [-h]\n", name);
107 
108  fprintf(stdout, "\n\
109 OPTIONS: Default\n\
110  -i <input image path> %s\n\
111  Set image input path.\n\
112  From this path read \"line/image.%%04d.%s\"\n\
113  images. \n\
114  Setting the VISP_INPUT_IMAGE_PATH environment\n\
115  variable produces the same behaviour than using\n\
116  this option.\n\
117 \n\
118  -p <personal image path> %s\n\
119  Specify a personal sequence containing images \n\
120  to process.\n\
121  By image sequence, we mean one file per image.\n\
122  Example : \"C:/Temp/visp-images/line/image.%%04d.%s\"\n\
123  %%04d is for the image numbering.\n\
124 \n\
125  -f <first image> %u\n\
126  First image number of the sequence.\n\
127 \n\
128  -l <last image> %u\n\
129  Last image number of the sequence.\n\
130 \n\
131  -s <step> %u\n\
132  Step between two images.\n\
133 \n\
134  -c\n\
135  Disable the mouse click. Useful to automate the \n\
136  execution of this program without human intervention.\n\
137 \n\
138  -d \n\
139  Turn off the display.\n\
140 \n\
141  -h\n\
142  Print the help.\n",
143  ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
144 
145  if (badparam)
146  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
147 }
148 
166 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
167  unsigned &step, bool &click_allowed, bool &display)
168 {
169  const char *optarg_;
170  int c;
171  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
172 
173  switch (c) {
174  case 'c':
175  click_allowed = false;
176  break;
177  case 'd':
178  display = false;
179  break;
180  case 'i':
181  ipath = optarg_;
182  break;
183  case 'p':
184  ppath = optarg_;
185  break;
186  case 'f':
187  first = (unsigned)atoi(optarg_);
188  break;
189  case 'l':
190  last = (unsigned)atoi(optarg_);
191  break;
192  case 's':
193  step = (unsigned)atoi(optarg_);
194  break;
195  case 'h':
196  usage(argv[0], nullptr, ipath, ppath, first, last, step);
197  return false;
198  break;
199 
200  default:
201  usage(argv[0], optarg_, ipath, ppath, first, last, step);
202  return false;
203  break;
204  }
205  }
206 
207  if ((c == 1) || (c == -1)) {
208  // standalone param or error
209  usage(argv[0], nullptr, ipath, ppath, first, last, step);
210  std::cerr << "ERROR: " << std::endl;
211  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
212  return false;
213  }
214 
215  return true;
216 }
217 
218 int main(int argc, const char **argv)
219 {
220 #if defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV)
221  std::string env_ipath;
222  std::string opt_ipath;
223  std::string ipath;
224  std::string opt_ppath;
225  std::string videoname;
226  unsigned int opt_first = 1;
227  unsigned int opt_last = 30;
228  unsigned int opt_step = 1;
229  bool opt_click_allowed = true;
230  bool opt_display = true;
231  unsigned int thickness = 1;
232 
234  vpDisplay *display = nullptr;
235  vpVideoReader g;
236 
237  try {
238  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
239  // environment variable value
240  env_ipath = vpIoTools::getViSPImagesDataPath();
241 
242  // Set the default input path
243  if (!env_ipath.empty())
244  ipath = env_ipath;
245 
246  // Read the command line options
247  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
248  opt_display) == false) {
249  return EXIT_FAILURE;
250  }
251 
252  // Get the option values
253  if (!opt_ipath.empty()) {
254  ipath = opt_ipath;
255  }
256 
257  // Compare ipath and env_ipath. If they differ, we take into account
258  // the input path coming from the command line option
259  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
260  if (ipath != env_ipath) {
261  std::cout << std::endl << "WARNING: " << std::endl;
262  std::cout << " Since -i <visp image path=" << ipath << "> "
263  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
264  << " we skip the environment variable." << std::endl;
265  }
266  }
267 
268  // Test if an input path is set
269  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
270  usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
271  std::cerr << std::endl << "ERROR:" << std::endl;
272  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
273  << " environment variable to specify the location of the " << std::endl
274  << " image path where test images are located." << std::endl
275  << " Use -p <personal image path> option if you want to " << std::endl
276  << " use personal images." << std::endl
277  << std::endl;
278 
279  return EXIT_FAILURE;
280  }
281 
282  vpVideoReader g;
283  if (opt_ppath.empty()) {
284  // Set the path location of the image sequence
285 #if VISP_HAVE_DATASET_VERSION >= 0x030600
286  videoname = vpIoTools::createFilePath(ipath, "line/image.%04d.png");
287 #else
288  videoname = vpIoTools::createFilePath(ipath, "line/image.%04d.pgm");
289 #endif
290  g.setFileName(videoname);
291  }
292  else {
293  g.setFileName(opt_ppath);
294  }
295 
296  if (opt_first > 0) {
297  g.setFirstFrameIndex(opt_first);
298  }
299  if (opt_last > 0) {
300  g.setLastFrameIndex(opt_last);
301  }
302  g.setFrameStep(opt_step);
303  g.open(I);
304 
305  if (opt_display) {
306  // We open a window using either X11, GTK, GDI or OpenCV
307 #if defined(VISP_HAVE_X11)
308  display = new vpDisplayX;
309 #elif defined(VISP_HAVE_GTK)
310  display = new vpDisplayGTK;
311 #elif defined(VISP_HAVE_GDI)
312  display = new vpDisplayGDI;
313 #elif defined(HAVE_OPENCV_HIGHGUI)
314  display = new vpDisplayOpenCV;
315 #endif
316  // Display size is automatically defined by the image (I) size
317  display->init(I, 10, 10, "Current image");
318  // Display the image
319  // The image class has a member that specify a pointer toward
320  // the display that has been initialized in the display declaration
321  // therefore is is no longer necessary to make a reference to the
322  // display variable.
324  vpDisplay::flush(I);
325  }
326 
327  vpMeLine me_line;
328 
329  vpMe me;
330  me.setRange(15);
331  me.setPointsToTrack(160);
333  me.setThreshold(20);
334 
335  me_line.setMe(&me);
337 
338  std::cout << "Video settings" << std::endl;
339  std::cout << " Name : " << g.getFrameName() << std::endl;
340  std::cout << " First image: " << g.getFirstFrameIndex() << std::endl;
341  std::cout << " Last image : " << g.getLastFrameIndex() << std::endl;
342  std::cout << " Step : " << g.getFrameStep() << std::endl;
343  std::cout << " Image size : " << I.getWidth() << " x " << I.getHeight() << std::endl;
344 
345  std::cout << "Moving-edges settings" << std::endl;
346  std::cout << " Sample step : " << me_line.getMe()->getSampleStep() << std::endl;
347  std::cout << " Range : " << me_line.getMe()->getRange() << std::endl;
348  std::cout << " Threshold type: " << (me_line.getMe()->getLikelihoodThresholdType() == vpMe::NORMALIZED_THRESHOLD ? "normalized" : "old threshold (to be avoided)") << std::endl;
349  std::cout << " Threshold : " << me_line.getMe()->getThreshold() << std::endl;
350 
351  if (opt_display && opt_click_allowed)
352  me_line.initTracking(I);
353  else {
354  vpImagePoint ip1, ip2;
355  ip1.set_i(96);
356  ip1.set_j(191);
357  ip2.set_i(122);
358  ip2.set_j(211);
359  me_line.initTracking(I, ip1, ip2);
360  }
361 
362  if (opt_display) {
363  me_line.display(I, vpColor::green);
364  }
365 
366  me_line.track(I);
367  if (opt_display && opt_click_allowed) {
368  std::cout << "A click to continue..." << std::endl;
370  }
371  std::cout << "----------------------------------------------------------" << std::endl;
372 
373  vpFeatureLine l;
374 
375  vpCameraParameters cam;
376 
377  bool quit = false;
378  while (!g.end() && !quit) {
379  g.acquire(I);
380  std::cout << "Process image " << g.getFrameIndex() << std::endl;
381  if (opt_display) {
382  // Display the image
384  if (opt_click_allowed) {
385  vpDisplay::displayText(I, 40, 10, "Click to exit...", vpColor::red);
386  }
387  }
388 
389  me_line.track(I);
390 
391  vpTRACE("me_line : %f %f", me_line.getRho(), vpMath::deg(me_line.getTheta()));
392  vpFeatureBuilder::create(l, cam, me_line);
393  vpTRACE("me_line : %f %f", l.getRho(), vpMath::deg(l.getTheta()));
394 
395  if (opt_display) {
396  me_line.display(I, vpColor::green, thickness);
397  vpDisplay::flush(I);
398  }
399  if (opt_display && opt_click_allowed) {
400  if (vpDisplay::getClick(I, false)) {
401  quit = true;
402  }
403  }
404  }
405  if (opt_display && opt_click_allowed && !quit) {
407  }
408  if (display) {
409  delete display;
410  }
411  return EXIT_SUCCESS;
412  }
413  catch (const vpException &e) {
414  std::cout << "Catch an exception: " << e << std::endl;
415  return EXIT_FAILURE;
416  }
417 #else
418  (void)argc;
419  (void)argv;
420  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
421 #endif
422 }
423 
424 #else
425 #include <iostream>
426 
427 int main()
428 {
429  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
430  "functionalities are required..."
431  << std::endl;
432  return EXIT_SUCCESS;
433 }
434 
435 #endif
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition: vpColor.h:217
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...
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines a 2D line visual feature which is composed by two parameters that are and ,...
double getTheta() const
double getRho() const
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
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
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 double deg(double rad)
Definition: vpMath.h:119
Class that tracks in an image a line moving edges.
Definition: vpMeLine.h:152
double getRho() const
Definition: vpMeLine.cpp:843
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1)
Definition: vpMeLine.cpp:194
double getTheta() const
Definition: vpMeLine.cpp:845
void track(const vpImage< unsigned char > &I)
Definition: vpMeLine.cpp:673
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeLine.cpp:199
@ 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 setPointsToTrack(const int &points_to_track)
Definition: vpMe.h:408
vpLikelihoodThresholdType getLikelihoodThresholdType() const
Definition: vpMe.h:327
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
double getThreshold() const
Definition: vpMe.h:291
double getSampleStep() const
Definition: vpMe.h:275
unsigned int getRange() const
Definition: vpMe.h:268
@ 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 acquire(vpImage< vpRGBa > &I)
void setLastFrameIndex(const long last_frame)
long getLastFrameIndex()
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void setFirstFrameIndex(const long first_frame)
long getFirstFrameIndex()
void setFrameStep(const long frame_step)
long getFrameStep() const
std::string getFrameName() const
long getFrameIndex() const