ViSP
trackMeLine.cpp
1 /****************************************************************************
2  *
3  * $Id: trackMeLine.cpp 5108 2015-01-05 07:48:58Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Tracking of a line.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
54 #include <visp/vpDebug.h>
55 #include <visp/vpConfig.h>
56 
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <sstream>
60 #include <iomanip>
61 
62 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
63 
64 #include <visp/vpImage.h>
65 #include <visp/vpImageIo.h>
66 #include <visp/vpImagePoint.h>
67 #include <visp/vpDisplayX.h>
68 #include <visp/vpDisplayGTK.h>
69 #include <visp/vpDisplayGDI.h>
70 #include <visp/vpDisplayOpenCV.h>
71 #include <visp/vpColor.h>
72 
73 #include <visp/vpMeLine.h>
74 
75 #include <visp/vpFeatureLine.h>
76 #include <visp/vpFeatureBuilder.h>
77 
78 #include <visp/vpParseArgv.h>
79 #include <visp/vpIoTools.h>
80 
81 // List of allowed command line options
82 #define GETOPTARGS "cdi:h"
83 
84 void usage(const char *name, const char *badparam, std::string ipath);
85 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
86 
96 void usage(const char *name, const char *badparam, std::string ipath)
97 {
98  fprintf(stdout, "\n\
99 Tracking of a line.\n\
100 \n\
101 SYNOPSIS\n\
102  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
103 
104  fprintf(stdout, "\n\
105 OPTIONS: Default\n\
106  -i <input image path> %s\n\
107  Set image input path.\n\
108  From this path read \"ViSP-images/line/image.%%04d.pgm\"\n\
109  images. \n\
110  Setting the VISP_INPUT_IMAGE_PATH environment\n\
111  variable produces the same behaviour than using\n\
112  this option.\n\
113 \n\
114  -c\n\
115  Disable the mouse click. Useful to automaze the \n\
116  execution of this program without humain intervention.\n\
117 \n\
118  -d \n\
119  Turn off the display.\n\
120 \n\
121  -h\n\
122  Print the help.\n",
123  ipath.c_str());
124 
125  if (badparam)
126  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
127 }
141 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
142 {
143  const char *optarg_;
144  int c;
145  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
146 
147  switch (c) {
148  case 'c': click_allowed = false; break;
149  case 'd': display = false; break;
150  case 'i': ipath = optarg_; break;
151  case 'h': usage(argv[0], NULL, ipath); return false; break;
152 
153  default:
154  usage(argv[0], optarg_, ipath);
155  return false; break;
156  }
157  }
158 
159  if ((c == 1) || (c == -1)) {
160  // standalone param or error
161  usage(argv[0], NULL, 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 
171 int
172 main(int argc, const char ** argv)
173 {
174  try {
175  std::string env_ipath;
176  std::string opt_ipath;
177  std::string ipath;
178  std::string dirname;
179  std::string filename;
180  bool opt_click_allowed = true;
181  bool opt_display = true;
182 
183  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
184  env_ipath = vpIoTools::getViSPImagesDataPath();
185 
186  // Set the default input path
187  if (! env_ipath.empty())
188  ipath = env_ipath;
189 
190 
191  // Read the command line options
192  if (getOptions(argc, argv, opt_ipath, opt_click_allowed,
193  opt_display) == false) {
194  exit (-1);
195  }
196 
197  // Get the option values
198  if (!opt_ipath.empty())
199  ipath = opt_ipath;
200 
201  // Compare ipath and env_ipath. If they differ, we take into account
202  // the input path comming from the command line option
203  if (!opt_ipath.empty() && !env_ipath.empty()) {
204  if (ipath != env_ipath) {
205  std::cout << std::endl
206  << "WARNING: " << std::endl;
207  std::cout << " Since -i <visp image path=" << ipath << "> "
208  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
209  << " we skip the environment variable." << std::endl;
210  }
211  }
212 
213  // Test if an input path is set
214  if (opt_ipath.empty() && env_ipath.empty()){
215  usage(argv[0], NULL, ipath);
216  std::cerr << std::endl
217  << "ERROR:" << std::endl;
218  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
219  << std::endl
220  << " environment variable to specify the location of the " << std::endl
221  << " image path where test images are located." << std::endl << std::endl;
222  exit(-1);
223  }
224 
225  // Declare an image, this is a gray level image (unsigned char)
226  // it size is not defined yet, it will be defined when the image will
227  // read on the disk
229 
230  // Set the path location of the image sequence
231  dirname = vpIoTools::createFilePath(ipath, "ViSP-images/line");
232 
233  // Build the name of the image file
234  unsigned int iter = 1; // Image number
235  std::ostringstream s;
236  s.setf(std::ios::right, std::ios::adjustfield);
237  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
238  filename = vpIoTools::createFilePath(dirname, s.str());
239 
240  // Read the PGM image named "filename" on the disk, and put the
241  // bitmap into the image structure I. I is initialized to the
242  // correct size
243  //
244  // exception readPGM 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  {
253  // an exception is throwned if an exception from readPGM has been catched
254  // here this will result in the end of the program
255  // Note that another error message has been printed from readPGM
256  // to give more information about the error
257  std::cerr << std::endl
258  << "ERROR:" << std::endl;
259  std::cerr << " Cannot read " << filename << std::endl;
260  std::cerr << " Check your -i " << ipath << " option " << std::endl
261  << " or VISP_INPUT_IMAGE_PATH environment variable."
262  << std::endl;
263  exit(-1);
264  }
265 
266  // We open a window using either X11, GTK or GDI.
267 #if defined VISP_HAVE_X11
268  vpDisplayX display;
269 #elif defined VISP_HAVE_GTK
270  vpDisplayGTK display;
271 #elif defined VISP_HAVE_GDI
272  vpDisplayGDI display;
273 #elif defined VISP_HAVE_OPENCV
274  vpDisplayOpenCV display;
275 #endif
276 
277  if (opt_display) {
278  // Display size is automatically defined by the image (I) size
279  display.init(I, 100, 100,"Display...") ;
280  // Display the image
281  // The image class has a member that specify a pointer toward
282  // the display that has been initialized in the display declaration
283  // therefore is is no longuer necessary to make a reference to the
284  // display variable.
285  vpDisplay::display(I) ;
286  vpDisplay::flush(I) ;
287  }
288 
289  vpMeLine L1 ;
290 
291  vpMe me ;
292  me.setRange(15) ;
293  me.setPointsToTrack(160) ;
294  me.setThreshold(15000) ;
295 
296  L1.setMe(&me) ;
298 
299  if (opt_display && opt_click_allowed)
300  L1.initTracking(I) ;
301  else {
302  vpImagePoint ip1, ip2;
303  ip1.set_i( 96 );
304  ip1.set_j( 191 );
305  ip2.set_i( 122 );
306  ip2.set_j( 211 );
307  L1.initTracking(I, ip1, ip2) ;
308  }
309 
310  if (opt_display)
311  L1.display(I, vpColor::green) ;
312 
313  L1.track(I) ;
314  if (opt_display && opt_click_allowed) {
315  std::cout << "A click to continue..." << std::endl;
317  }
318  std::cout <<"----------------------------------------------------------"<<std::endl;
319 
320  vpFeatureLine l ;
321 
322  vpCameraParameters cam ;
323  vpImage<vpRGBa> Ic ;
324  for (iter = 1 ; iter < 30 ; iter++)
325  {
326  std::cout <<"----------------------------------------------------------"<<std::endl;
327  // set the new image name
328  s.str("");
329  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
330  filename = vpIoTools::createFilePath(dirname, s.str());
331  // read the image
332  vpImageIo::read(I, filename);
333  if (opt_display) {
334  // Display the image
335  vpDisplay::display(I) ;
336  }
337 
338  std::cout << "Tracking on image: " << filename << std::endl;
339  L1.track(I) ;
340 
341  vpTRACE("L1 : %f %f", L1.getRho(), vpMath::deg(L1.getTheta())) ;
342  vpFeatureBuilder::create(l,cam,L1) ;
343  vpTRACE("L1 : %f %f", l.getRho(), vpMath::deg(l.getTheta())) ;
344 
345  if (opt_display) {
346  L1.display(I,vpColor::green) ;
347  vpDisplay::flush(I) ;
348  if (opt_click_allowed) {
349  std::cout << "A click to continue..." << std::endl;
351  }
352  }
353  }
354  if (opt_display && opt_click_allowed) {
355  std::cout << "A click to exit..." << std::endl;
357  }
358  return 0;
359  }
360  catch(vpException e) {
361  std::cout << "Catch an exception: " << e << std::endl;
362  return 1;
363  }
364 }
365 
366 #else
367 int
368 main()
369 {
370  vpERROR_TRACE("You do not have X11, GTK, GDI or OpenCV display functionalities...");
371 }
372 
373 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
void setPointsToTrack(const int &n)
Definition: vpMe.h:215
#define vpERROR_TRACE
Definition: vpDebug.h:395
double getTheta() const
Definition: vpMeLine.cpp:1061
#define vpTRACE
Definition: vpDebug.h:418
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
void track(const vpImage< unsigned char > &Im)
Definition: vpMeLine.cpp:816
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
double getRho() const
static const vpColor green
Definition: vpColor.h:170
double getTheta() const
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
#define vpCTRACE
Definition: vpDebug.h:341
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeLine.cpp:245
void set_i(const double ii)
Definition: vpImagePoint.h:159
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:108
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
Class that tracks in an image a line moving edges.
Definition: vpMeLine.h:156
The vpDisplayOpenCV allows to display image using the opencv library.
Generic class defining intrinsic camera parameters.
Class that defines a 2D line visual feature which is composed by two parameters that are and ...
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeLine.cpp:259
void set_j(const double jj)
Definition: vpImagePoint.h:170
static double deg(double rad)
Definition: vpMath.h:93
void setThreshold(const double &t)
Definition: vpMe.h:299
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setRange(const unsigned int &r)
Definition: vpMe.h:229
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:142
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
double getRho() const
Definition: vpMeLine.cpp:1052