ViSP  2.8.0
trackMeLine.cpp
1 /****************************************************************************
2  *
3  * $Id: trackMeLine.cpp 4323 2013-07-18 09:24:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
93 void usage(const char *name, const char *badparam, std::string ipath)
94 {
95  fprintf(stdout, "\n\
96 Tracking of a line.\n\
97 \n\
98 SYNOPSIS\n\
99  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
100 
101  fprintf(stdout, "\n\
102 OPTIONS: Default\n\
103  -i <input image path> %s\n\
104  Set image input path.\n\
105  From this path read \"ViSP-images/line/image.%%04d.pgm\"\n\
106  images. \n\
107  Setting the VISP_INPUT_IMAGE_PATH environment\n\
108  variable produces the same behaviour than using\n\
109  this option.\n\
110 \n\
111  -c\n\
112  Disable the mouse click. Useful to automaze the \n\
113  execution of this program without humain intervention.\n\
114 \n\
115  -d \n\
116  Turn off the display.\n\
117 \n\
118  -h\n\
119  Print the help.\n",
120  ipath.c_str());
121 
122  if (badparam)
123  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
124 }
138 bool getOptions(int argc, const char **argv, std::string &ipath,
139  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': click_allowed = false; break;
147  case 'd': display = false; break;
148  case 'i': ipath = optarg; break;
149  case 'h': usage(argv[0], NULL, ipath); return false; break;
150 
151  default:
152  usage(argv[0], optarg, ipath);
153  return false; break;
154  }
155  }
156 
157  if ((c == 1) || (c == -1)) {
158  // standalone param or error
159  usage(argv[0], NULL, ipath);
160  std::cerr << "ERROR: " << std::endl;
161  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
162  return false;
163  }
164 
165  return true;
166 }
167 
168 
169 int
170 main(int argc, const char ** argv)
171 {
172  std::string env_ipath;
173  std::string opt_ipath;
174  std::string ipath;
175  std::string dirname;
176  std::string filename;
177  bool opt_click_allowed = true;
178  bool opt_display = true;
179 
180  // Get the VISP_IMAGE_PATH environment variable value
181  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
182  if (ptenv != NULL)
183  env_ipath = ptenv;
184 
185  // Set the default input path
186  if (! env_ipath.empty())
187  ipath = env_ipath;
188 
189 
190  // Read the command line options
191  if (getOptions(argc, argv, opt_ipath, opt_click_allowed,
192  opt_display) == false) {
193  exit (-1);
194  }
195 
196  // Get the option values
197  if (!opt_ipath.empty())
198  ipath = opt_ipath;
199 
200  // Compare ipath and env_ipath. If they differ, we take into account
201  // the input path comming from the command line option
202  if (!opt_ipath.empty() && !env_ipath.empty()) {
203  if (ipath != env_ipath) {
204  std::cout << std::endl
205  << "WARNING: " << std::endl;
206  std::cout << " Since -i <visp image path=" << ipath << "> "
207  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
208  << " we skip the environment variable." << std::endl;
209  }
210  }
211 
212  // Test if an input path is set
213  if (opt_ipath.empty() && env_ipath.empty()){
214  usage(argv[0], NULL, ipath);
215  std::cerr << std::endl
216  << "ERROR:" << std::endl;
217  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
218  << std::endl
219  << " environment variable to specify the location of the " << std::endl
220  << " image path where test images are located." << std::endl << std::endl;
221  exit(-1);
222  }
223 
224  // Declare an image, this is a gray level image (unsigned char)
225  // it size is not defined yet, it will be defined when the image will
226  // read on the disk
228 
229  // Set the path location of the image sequence
230  dirname = ipath + vpIoTools::path("/ViSP-images/line/");
231 
232  // Build the name of the image file
233  unsigned iter = 1; // Image number
234  std::ostringstream s;
235  s.setf(std::ios::right, std::ios::adjustfield);
236  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
237  filename = dirname + s.str();
238 
239  // Read the PGM image named "filename" on the disk, and put the
240  // bitmap into the image structure I. I is initialized to the
241  // correct size
242  //
243  // exception readPGM may throw various exception if, for example,
244  // the file does not exist, or if the memory cannot be allocated
245  try{
246  vpCTRACE << "Load: " << filename << std::endl;
247 
248  vpImageIo::read(I, filename) ;
249  }
250  catch(...)
251  {
252  // an exception is throwned if an exception from readPGM has been catched
253  // here this will result in the end of the program
254  // Note that another error message has been printed from readPGM
255  // to give more information about the error
256  std::cerr << std::endl
257  << "ERROR:" << std::endl;
258  std::cerr << " Cannot read " << filename << std::endl;
259  std::cerr << " Check your -i " << ipath << " option " << std::endl
260  << " or VISP_INPUT_IMAGE_PATH environment variable."
261  << std::endl;
262  exit(-1);
263  }
264 
265  // We open a window using either X11, GTK or GDI.
266 #if defined VISP_HAVE_X11
267  vpDisplayX display;
268 #elif defined VISP_HAVE_GTK
269  vpDisplayGTK display;
270 #elif defined VISP_HAVE_GDI
271  vpDisplayGDI display;
272 #endif
273 
274  if (opt_display) {
275  try{
276  // Display size is automatically defined by the image (I) size
277  display.init(I, 100, 100,"Display...") ;
278  // Display the image
279  // The image class has a member that specify a pointer toward
280  // the display that has been initialized in the display declaration
281  // therefore is is no longuer necessary to make a reference to the
282  // display variable.
283  vpDisplay::display(I) ;
284  vpDisplay::flush(I) ;
285  }
286  catch(...)
287  {
288  vpERROR_TRACE("Error while displaying the image") ;
289  exit(-1);
290  }
291  }
292 
293 
294  vpMeLine L1 ;
295 
296  vpMe me ;
297  me.setRange(15) ;
298  me.setPointsToTrack(160) ;
299  me.setThreshold(15000) ;
300 
301 
302  L1.setMe(&me) ;
304 
305  if (opt_display && opt_click_allowed)
306  L1.initTracking(I) ;
307  else {
308  vpImagePoint ip1, ip2;
309  ip1.set_i( 96 );
310  ip1.set_j( 191 );
311  ip2.set_i( 122 );
312  ip2.set_j( 211 );
313  L1.initTracking(I, ip1, ip2) ;
314  }
315 
316  if (opt_display)
317  L1.display(I, vpColor::green) ;
318 
319  L1.track(I) ;
320  if (opt_display && opt_click_allowed) {
321  std::cout << "A click to continue..." << std::endl;
323  }
324  std::cout <<"----------------------------------------------------------"<<std::endl;
325 
326  vpFeatureLine l ;
327 
328  vpCameraParameters cam ;
329  vpImage<vpRGBa> Ic ;
330  for (int iter = 1 ; iter < 30 ; iter++)
331  {
332  std::cout <<"----------------------------------------------------------"<<std::endl;
333  // set the new image name
334  s.str("");
335  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
336  filename = dirname + s.str();
337  // read the image
338  vpImageIo::read(I, filename);
339  if (opt_display) {
340  // Display the image
341  vpDisplay::display(I) ;
342  }
343 
344  try
345  {
346  std::cout << "Tracking on image: " << filename << std::endl;
347  L1.track(I) ;
348  }
349  catch(...)
350  {
351  vpERROR_TRACE("Error in tracking vpMeLine ") ;
352  exit(1) ;
353  }
354 
355  vpTRACE("L1 : %f %f", L1.getRho(), vpMath::deg(L1.getTheta())) ;
356  vpFeatureBuilder::create(l,cam,L1) ;
357  vpTRACE("L1 : %f %f", l.getRho(), vpMath::deg(l.getTheta())) ;
358 
359  if (opt_display) {
360  L1.display(I,vpColor::green) ;
361  vpDisplay::flush(I) ;
362  if (opt_click_allowed) {
363  std::cout << "A click to continue..." << std::endl;
365  }
366  }
367  }
368  if (opt_display && opt_click_allowed) {
369  std::cout << "A click to exit..." << std::endl;
371  }
372 }
373 
374 #else
375 int
376 main()
377 {
378  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
379 }
380 
381 #endif
void set_j(const double j)
Definition: vpImagePoint.h:156
void setPointsToTrack(const int &n)
Definition: vpMe.h:215
#define vpERROR_TRACE
Definition: vpDebug.h:379
double getTheta() const
Definition: vpMeLine.cpp:1055
#define vpTRACE
Definition: vpDebug.h:401
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void set_i(const double i)
Definition: vpImagePoint.h:145
void track(const vpImage< unsigned char > &Im)
Definition: vpMeLine.cpp:810
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:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
#define vpCTRACE
Definition: vpDebug.h:327
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeLine.cpp:238
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:108
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
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:252
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
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
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:277
double getRho() const
Definition: vpMeLine.cpp:1046
void setMe(vpMe *me)
Definition: vpMeTracker.h:140