ViSP  2.9.0
trackMeLine.cpp
1 /****************************************************************************
2  *
3  * $Id: trackMeLine.cpp 4658 2014-02-09 09:50:14Z 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))
63 
64 
65 #include <visp/vpImage.h>
66 #include <visp/vpImageIo.h>
67 #include <visp/vpImagePoint.h>
68 #include <visp/vpDisplayX.h>
69 #include <visp/vpDisplayGTK.h>
70 #include <visp/vpDisplayGDI.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_IMAGE_PATH environment variable value
184  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
185  if (ptenv != NULL)
186  env_ipath = ptenv;
187 
188  // Set the default input path
189  if (! env_ipath.empty())
190  ipath = env_ipath;
191 
192 
193  // Read the command line options
194  if (getOptions(argc, argv, opt_ipath, opt_click_allowed,
195  opt_display) == false) {
196  exit (-1);
197  }
198 
199  // Get the option values
200  if (!opt_ipath.empty())
201  ipath = opt_ipath;
202 
203  // Compare ipath and env_ipath. If they differ, we take into account
204  // the input path comming from the command line option
205  if (!opt_ipath.empty() && !env_ipath.empty()) {
206  if (ipath != env_ipath) {
207  std::cout << std::endl
208  << "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], NULL, ipath);
218  std::cerr << std::endl
219  << "ERROR:" << std::endl;
220  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
221  << std::endl
222  << " environment variable to specify the location of the " << std::endl
223  << " image path where test images are located." << std::endl << std::endl;
224  exit(-1);
225  }
226 
227  // Declare an image, this is a gray level image (unsigned char)
228  // it size is not defined yet, it will be defined when the image will
229  // read on the disk
231 
232  // Set the path location of the image sequence
233  dirname = ipath + vpIoTools::path("/ViSP-images/line/");
234 
235  // Build the name of the image file
236  unsigned int iter = 1; // Image number
237  std::ostringstream s;
238  s.setf(std::ios::right, std::ios::adjustfield);
239  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
240  filename = dirname + s.str();
241 
242  // Read the PGM image named "filename" on the disk, and put the
243  // bitmap into the image structure I. I is initialized to the
244  // correct size
245  //
246  // exception readPGM may throw various exception if, for example,
247  // the file does not exist, or if the memory cannot be allocated
248  try{
249  vpCTRACE << "Load: " << filename << std::endl;
250 
251  vpImageIo::read(I, filename) ;
252  }
253  catch(...)
254  {
255  // an exception is throwned if an exception from readPGM has been catched
256  // here this will result in the end of the program
257  // Note that another error message has been printed from readPGM
258  // to give more information about the error
259  std::cerr << std::endl
260  << "ERROR:" << std::endl;
261  std::cerr << " Cannot read " << filename << std::endl;
262  std::cerr << " Check your -i " << ipath << " option " << std::endl
263  << " or VISP_INPUT_IMAGE_PATH environment variable."
264  << std::endl;
265  exit(-1);
266  }
267 
268  // We open a window using either X11, GTK or GDI.
269 #if defined VISP_HAVE_X11
270  vpDisplayX display;
271 #elif defined VISP_HAVE_GTK
272  vpDisplayGTK display;
273 #elif defined VISP_HAVE_GDI
274  vpDisplayGDI 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 
297  L1.setMe(&me) ;
299 
300  if (opt_display && opt_click_allowed)
301  L1.initTracking(I) ;
302  else {
303  vpImagePoint ip1, ip2;
304  ip1.set_i( 96 );
305  ip1.set_j( 191 );
306  ip2.set_i( 122 );
307  ip2.set_j( 211 );
308  L1.initTracking(I, ip1, ip2) ;
309  }
310 
311  if (opt_display)
312  L1.display(I, vpColor::green) ;
313 
314  L1.track(I) ;
315  if (opt_display && opt_click_allowed) {
316  std::cout << "A click to continue..." << std::endl;
318  }
319  std::cout <<"----------------------------------------------------------"<<std::endl;
320 
321  vpFeatureLine l ;
322 
323  vpCameraParameters cam ;
324  vpImage<vpRGBa> Ic ;
325  for (iter = 1 ; iter < 30 ; iter++)
326  {
327  std::cout <<"----------------------------------------------------------"<<std::endl;
328  // set the new image name
329  s.str("");
330  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
331  filename = dirname + s.str();
332  // read the image
333  vpImageIo::read(I, filename);
334  if (opt_display) {
335  // Display the image
336  vpDisplay::display(I) ;
337  }
338 
339  std::cout << "Tracking on image: " << filename << std::endl;
340  L1.track(I) ;
341 
342  vpTRACE("L1 : %f %f", L1.getRho(), vpMath::deg(L1.getTheta())) ;
343  vpFeatureBuilder::create(l,cam,L1) ;
344  vpTRACE("L1 : %f %f", l.getRho(), vpMath::deg(l.getTheta())) ;
345 
346  if (opt_display) {
347  L1.display(I,vpColor::green) ;
348  vpDisplay::flush(I) ;
349  if (opt_click_allowed) {
350  std::cout << "A click to continue..." << std::endl;
352  }
353  }
354  }
355  if (opt_display && opt_click_allowed) {
356  std::cout << "A click to exit..." << std::endl;
358  }
359  return 0;
360  }
361  catch(vpException e) {
362  std::cout << "Catch an exception: " << e << std::endl;
363  return 1;
364  }
365 }
366 
367 #else
368 int
369 main()
370 {
371  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
372 }
373 
374 #endif
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
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
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:1994
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
#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:158
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:108
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
Class that tracks in an image a line moving edges.
Definition: vpMeLine.h:156
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 init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void set_j(const double jj)
Definition: vpImagePoint.h:169
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:92
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:140
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
double getRho() const
Definition: vpMeLine.cpp:1052