48 #include <visp/vpDebug.h>
49 #include <visp/vpConfig.h>
56 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
58 #if (defined (VISP_HAVE_OPENCV))
60 #include <visp/vpKltOpencv.h>
61 #include <visp/vpImage.h>
62 #include <visp/vpImageIo.h>
63 #include <visp/vpDisplayX.h>
64 #include <visp/vpDisplayGTK.h>
65 #include <visp/vpDisplayGDI.h>
66 #include <visp/vpParseArgv.h>
67 #include <visp/vpIoTools.h>
70 #define GETOPTARGS "cdf:i:n:p:s:h"
72 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
73 unsigned first,
unsigned nimages,
unsigned step);
74 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
75 unsigned &nimages,
unsigned &step,
bool &click_allowed,
bool &display);
97 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
98 unsigned first,
unsigned nimages,
unsigned step)
101 Example of KLT tracking using OpenCV library.\n\
104 %s [-i <test image path>] [-p <personal image path>]\n\
105 [-f <first image>] [-n <number of images>] [-s <step>]\n\
106 [-c] [-d] [-h]\n", name);
110 -i <input image path> %s\n\
111 Set image input path.\n\
112 From this path read images \n\
113 \"ViSP-images/mire-2/image.%%04d.pgm\". These \n\
114 images come from ViSP-images-x.y.z.tar.gz available \n\
115 on the ViSP website.\n\
116 Setting the VISP_INPUT_IMAGE_PATH environment\n\
117 variable produces the same behaviour than using\n\
120 -p <personal image path> %s\n\
121 Specify a personal sequence containing images \n\
123 By image sequence, we mean one file per image.\n\
124 The following image file formats PNM (PGM P5, PPM P6)\n\
125 are supported. The format is selected by analysing \n\
126 the filename extension.\n\
127 Example : \"/Temp/ViSP-images/cube/image.%%04d.pgm\"\n\
128 %%04d is for the image numbering.\n\
130 -f <first image> %u\n\
131 First image number of the sequence.\n\
133 -n <number of images> %u\n\
134 Number of images to load from the sequence.\n\
137 Step between two images.\n\
140 Disable the mouse click. Useful to automaze the \n\
141 execution of this program without humain intervention.\n\
144 Turn off the display.\n\
148 ipath.c_str(), ppath.c_str(), first, nimages, step);
151 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
170 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
171 unsigned &nimages,
unsigned &step,
bool &click_allowed,
bool &display)
178 case 'c': click_allowed =
false;
break;
179 case 'd': display =
false;
break;
180 case 'i': ipath = optarg_;
break;
181 case 'p': ppath = optarg_;
break;
182 case 'f': first = (unsigned) atoi(optarg_);
break;
183 case 'n': nimages = (unsigned) atoi(optarg_);
break;
184 case 's': step = (unsigned) atoi(optarg_);
break;
185 case 'h': usage(argv[0], NULL, ipath, ppath, first, nimages, step);
189 usage(argv[0], optarg_, ipath, ppath, first, nimages, step);
194 if ((c == 1) || (c == -1)) {
196 usage(argv[0], NULL, ipath, ppath, first, nimages, step);
197 std::cerr <<
"ERROR: " << std::endl;
198 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
206 main(
int argc,
const char ** argv)
209 std::string env_ipath;
210 std::string opt_ipath;
212 std::string opt_ppath;
214 std::string filename;
215 unsigned opt_first = 1;
216 unsigned opt_nimages = 500;
217 unsigned opt_step = 1;
218 bool opt_click_allowed =
true;
219 bool opt_display =
true;
222 char *ptenv = getenv(
"VISP_INPUT_IMAGE_PATH");
227 if (! env_ipath.empty())
232 if (getOptions(argc, argv, opt_ipath, opt_ppath,opt_first, opt_nimages,
233 opt_step, opt_click_allowed, opt_display) ==
false) {
238 if (!opt_ipath.empty())
243 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
244 if (ipath != env_ipath) {
245 std::cout << std::endl
246 <<
"WARNING: " << std::endl;
247 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
248 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
249 <<
" we skip the environment variable." << std::endl;
254 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty() ){
255 usage(argv[0], NULL, ipath, opt_ppath, opt_first, opt_nimages, opt_step);
256 std::cerr << std::endl
257 <<
"ERROR:" << std::endl;
258 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
260 <<
" environment variable to specify the location of the " << std::endl
261 <<
" image path where test images are located." << std::endl
262 <<
" Use -p <personal image path> option if you want to "<<std::endl
263 <<
" use personal images." << std::endl
275 unsigned iter = opt_first;
276 std::ostringstream s;
277 char cfilename[FILENAME_MAX];
279 if (opt_ppath.empty()){
299 s.setf(std::ios::right, std::ios::adjustfield);
300 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
301 filename = dirname + s.str();
305 sprintf(cfilename,opt_ppath.c_str(), iter) ;
306 filename = cfilename;
316 vpCTRACE <<
"Load: " << filename << std::endl;
321 if((cvI = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE))== NULL) {
322 printf(
"Cannot read image: %s\n", filename.c_str());
332 std::cerr << std::endl
333 <<
"ERROR:" << std::endl;
334 std::cerr <<
" Cannot read " << filename << std::endl;
335 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
336 <<
" or VISP_INPUT_IMAGE_PATH environment variable."
342 #if defined VISP_HAVE_X11
344 #elif defined VISP_HAVE_GTK
346 #elif defined VISP_HAVE_GDI
352 display.
init(vpI, 100, 100,
"Display...") ;
391 while (iter < opt_first + opt_nimages*opt_step) {
393 if (opt_ppath.empty()){
395 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
396 filename = dirname + s.str();
399 sprintf(cfilename, opt_ppath.c_str(), iter) ;
400 filename = cfilename;
403 std::cout <<
"read : " << filename << std::endl;
407 if((cvI = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE))
409 printf(
"Cannot read image: %s\n", filename.c_str());
424 std::cout <<
"Tracking on image: " << filename << std::endl;
428 std::cout <<
"Tracking performed in " <<
439 if (opt_display && opt_click_allowed) {
440 std::cout <<
"\nA click to exit..." << std::endl;
447 std::cout <<
"Catch an exception: " << e << std::endl;
462 vpERROR_TRACE(
"You do not have X11, GTK or GDI display functionalities...");
void setQuality(double input)
void track(const IplImage *I)
Display for windows using GDI (available on any windows 32 platform).
void setUseHarris(const int input)
Define the X11 console to display images.
void display(const vpImage< unsigned char > &I, vpColor color=vpColor::red, unsigned int thickness=1)
error that can be emited by ViSP classes.
void setPyramidLevels(const int input)
void setBlockSize(const int input)
static double measureTimeMs()
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
void setWindowSize(const int input)
static void display(const vpImage< unsigned char > &I)
void setTrackerId(int tid)
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void initTracking(const IplImage *I, const IplImage *mask=NULL)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
virtual bool getClick(bool blocking=true)=0
void setMaxFeatures(const int input)
static void read(vpImage< unsigned char > &I, const char *filename)
void setMinDistance(double input)
void setHarrisFreeParameter(double input)