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"
93 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
94 unsigned first,
unsigned nimages,
unsigned step)
97 Example of KLT tracking using OpenCV library.\n\
100 %s [-i <test image path>] [-p <personal image path>]\n\
101 [-f <first image>] [-n <number of images>] [-s <step>]\n\
102 [-c] [-d] [-h]\n", name);
106 -i <input image path> %s\n\
107 Set image input path.\n\
108 From this path read images \n\
109 \"ViSP-images/mire-2/image.%%04d.pgm\". These \n\
110 images come from ViSP-images-x.y.z.tar.gz available \n\
111 on the ViSP website.\n\
112 Setting the VISP_INPUT_IMAGE_PATH environment\n\
113 variable produces the same behaviour than using\n\
116 -p <personal image path> %s\n\
117 Specify a personal sequence containing images \n\
119 By image sequence, we mean one file per image.\n\
120 The following image file formats PNM (PGM P5, PPM P6)\n\
121 are supported. The format is selected by analysing \n\
122 the filename extension.\n\
123 Example : \"/Temp/ViSP-images/cube/image.%%04d.pgm\"\n\
124 %%04d is for the image numbering.\n\
126 -f <first image> %u\n\
127 First image number of the sequence.\n\
129 -n <number of images> %u\n\
130 Number of images to load from the sequence.\n\
133 Step between two images.\n\
136 Disable the mouse click. Useful to automaze the \n\
137 execution of this program without humain intervention.\n\
140 Turn off the display.\n\
144 ipath.c_str(), ppath.c_str(), first, nimages, step);
147 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
166 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
167 unsigned &first,
unsigned &nimages,
unsigned &step,
168 bool &click_allowed,
bool &display)
175 case 'c': click_allowed =
false;
break;
176 case 'd': display =
false;
break;
177 case 'i': ipath = optarg;
break;
178 case 'p': ppath = optarg;
break;
179 case 'f': first = (unsigned) atoi(optarg);
break;
180 case 'n': nimages = (unsigned) atoi(optarg);
break;
181 case 's': step = (unsigned) atoi(optarg);
break;
182 case 'h': usage(argv[0], NULL, ipath, ppath, first, nimages, step);
186 usage(argv[0], optarg, ipath, ppath, first, nimages, step);
191 if ((c == 1) || (c == -1)) {
193 usage(argv[0], NULL, ipath, ppath, first, nimages, step);
194 std::cerr <<
"ERROR: " << std::endl;
195 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
203 main(
int argc,
const char ** argv)
205 std::string env_ipath;
206 std::string opt_ipath;
208 std::string opt_ppath;
210 std::string filename;
211 unsigned opt_first = 1;
212 unsigned opt_nimages = 500;
213 unsigned opt_step = 1;
214 bool opt_click_allowed =
true;
215 bool opt_display =
true;
218 char *ptenv = getenv(
"VISP_INPUT_IMAGE_PATH");
223 if (! env_ipath.empty())
228 if (getOptions(argc, argv, opt_ipath, opt_ppath,opt_first, opt_nimages,
229 opt_step, opt_click_allowed, opt_display) ==
false) {
234 if (!opt_ipath.empty())
239 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
240 if (ipath != env_ipath) {
241 std::cout << std::endl
242 <<
"WARNING: " << std::endl;
243 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
244 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
245 <<
" we skip the environment variable." << std::endl;
250 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty() ){
251 usage(argv[0], NULL, ipath, opt_ppath, opt_first, opt_nimages, opt_step);
252 std::cerr << std::endl
253 <<
"ERROR:" << std::endl;
254 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
256 <<
" environment variable to specify the location of the " << std::endl
257 <<
" image path where test images are located." << std::endl
258 <<
" Use -p <personal image path> option if you want to "<<std::endl
259 <<
" use personal images." << std::endl
271 unsigned iter = opt_first;
272 std::ostringstream s;
273 char cfilename[FILENAME_MAX];
275 if (opt_ppath.empty()){
295 s.setf(std::ios::right, std::ios::adjustfield);
296 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
297 filename = dirname + s.str();
301 sprintf(cfilename,opt_ppath.c_str(), iter) ;
302 filename = cfilename;
312 vpCTRACE <<
"Load: " << filename << std::endl;
317 if((cvI = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE))== NULL) {
318 printf(
"Cannot read image: %s\n", filename.c_str());
328 std::cerr << std::endl
329 <<
"ERROR:" << std::endl;
330 std::cerr <<
" Cannot read " << filename << std::endl;
331 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
332 <<
" or VISP_INPUT_IMAGE_PATH environment variable."
338 #if defined VISP_HAVE_X11
340 #elif defined VISP_HAVE_GTK
342 #elif defined VISP_HAVE_GDI
349 display.
init(vpI, 100, 100,
"Display...") ;
401 while (iter < opt_first + opt_nimages*opt_step) {
403 if (opt_ppath.empty()){
405 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
406 filename = dirname + s.str();
409 sprintf(cfilename, opt_ppath.c_str(), iter) ;
410 filename = cfilename;
413 std::cout <<
"read : " << filename << std::endl;
417 if((cvI = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE))
419 printf(
"Cannot read image: %s\n", filename.c_str());
434 std::cout <<
"Tracking on image: " << filename << std::endl;
438 std::cout <<
"Tracking performed in " <<
451 std::cerr <<
"Error during the tracking..." << std::endl;
452 std::cerr <<
"The progam was stopped." << std::endl;
455 if (opt_display && opt_click_allowed) {
456 std::cout <<
"\nA click to exit..." << std::endl;
472 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)
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)