48 #include <visp/vpDebug.h>
49 #include <visp/vpConfig.h>
56 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined (VISP_HAVE_OPENCV))
58 #if defined (VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
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/vpDisplayOpenCV.h>
67 #include <visp/vpParseArgv.h>
68 #include <visp/vpIoTools.h>
71 #define GETOPTARGS "cdf:i:n:p:s:h"
73 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
74 unsigned first,
unsigned nimages,
unsigned step);
75 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
76 unsigned &nimages,
unsigned &step,
bool &click_allowed,
bool &display);
98 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
99 unsigned first,
unsigned nimages,
unsigned step)
102 Example of KLT tracking using OpenCV library.\n\
105 %s [-i <test image path>] [-p <personal image path>]\n\
106 [-f <first image>] [-n <number of images>] [-s <step>]\n\
107 [-c] [-d] [-h]\n", name);
111 -i <input image path> %s\n\
112 Set image input path.\n\
113 From this path read images \n\
114 \"ViSP-images/mire-2/image.%%04d.pgm\". These \n\
115 images come from ViSP-images-x.y.z.tar.gz available \n\
116 on the ViSP website.\n\
117 Setting the VISP_INPUT_IMAGE_PATH environment\n\
118 variable produces the same behaviour than using\n\
121 -p <personal image path> %s\n\
122 Specify a personal sequence containing images \n\
124 By image sequence, we mean one file per image.\n\
125 The following image file formats PNM (PGM P5, PPM P6)\n\
126 are supported. The format is selected by analysing \n\
127 the filename extension.\n\
128 Example : \"/Temp/ViSP-images/cube/image.%%04d.pgm\"\n\
129 %%04d is for the image numbering.\n\
131 -f <first image> %u\n\
132 First image number of the sequence.\n\
134 -n <number of images> %u\n\
135 Number of images to load from the sequence.\n\
138 Step between two images.\n\
141 Disable the mouse click. Useful to automaze the \n\
142 execution of this program without humain intervention.\n\
145 Turn off the display.\n\
149 ipath.c_str(), ppath.c_str(), first, nimages, step);
152 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
171 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
172 unsigned &nimages,
unsigned &step,
bool &click_allowed,
bool &display)
179 case 'c': click_allowed =
false;
break;
180 case 'd': display =
false;
break;
181 case 'i': ipath = optarg_;
break;
182 case 'p': ppath = optarg_;
break;
183 case 'f': first = (unsigned) atoi(optarg_);
break;
184 case 'n': nimages = (unsigned) atoi(optarg_);
break;
185 case 's': step = (unsigned) atoi(optarg_);
break;
186 case 'h': usage(argv[0], NULL, ipath, ppath, first, nimages, step);
190 usage(argv[0], optarg_, ipath, ppath, first, nimages, step);
195 if ((c == 1) || (c == -1)) {
197 usage(argv[0], NULL, ipath, ppath, first, nimages, step);
198 std::cerr <<
"ERROR: " << std::endl;
199 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
207 main(
int argc,
const char ** argv)
210 std::string env_ipath;
211 std::string opt_ipath;
213 std::string opt_ppath;
215 std::string filename;
216 unsigned opt_first = 1;
217 unsigned opt_nimages = 500;
218 unsigned opt_step = 1;
219 bool opt_click_allowed =
true;
220 bool opt_display =
true;
226 if (! env_ipath.empty())
230 if (getOptions(argc, argv, opt_ipath, opt_ppath,opt_first, opt_nimages,
231 opt_step, opt_click_allowed, opt_display) ==
false) {
236 if (!opt_ipath.empty())
241 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
242 if (ipath != env_ipath) {
243 std::cout << std::endl
244 <<
"WARNING: " << std::endl;
245 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
246 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
247 <<
" we skip the environment variable." << std::endl;
252 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty() ){
253 usage(argv[0], NULL, ipath, opt_ppath, opt_first, opt_nimages, opt_step);
254 std::cerr << std::endl
255 <<
"ERROR:" << std::endl;
256 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
258 <<
" environment variable to specify the location of the " << std::endl
259 <<
" image path where test images are located." << std::endl
260 <<
" Use -p <personal image path> option if you want to "<<std::endl
261 <<
" use personal images." << std::endl
271 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
272 IplImage * cvI = NULL;
277 unsigned iter = opt_first;
278 std::ostringstream s;
279 char cfilename[FILENAME_MAX];
281 if (opt_ppath.empty()){
301 s.setf(std::ios::right, std::ios::adjustfield);
302 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
306 sprintf(cfilename, opt_ppath.c_str(), iter) ;
307 filename = cfilename;
317 std::cout <<
"Load: " << filename << std::endl;
329 std::cerr << std::endl
330 <<
"ERROR:" << std::endl;
331 std::cerr <<
" Cannot read " << filename << std::endl;
332 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
333 <<
" or VISP_INPUT_IMAGE_PATH environment variable."
339 #if defined VISP_HAVE_X11
341 #elif defined VISP_HAVE_GTK
343 #elif defined VISP_HAVE_GDI
345 #elif defined VISP_HAVE_OPENCV
351 display.
init(vpI, 100, 100,
"Display...") ;
390 while (iter < opt_first + opt_nimages*opt_step) {
392 if (opt_ppath.empty()){
394 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
398 sprintf(cfilename, opt_ppath.c_str(), iter) ;
399 filename = cfilename;
416 std::cout <<
"Tracking on image: " << filename << std::endl;
420 std::cout <<
"Tracking performed in " <<
431 if (opt_display && opt_click_allowed) {
432 std::cout <<
"\nA click to exit..." << std::endl;
439 std::cout <<
"Catch an exception: " << e << std::endl;
454 vpERROR_TRACE(
"You do not have X11, GTK, GDI or OpenCV display functionalities...");
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setHarrisFreeParameter(double harris_k)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Display for windows using GDI (available on any windows 32 platform).
void setMaxFeatures(const int maxCount)
Define the X11 console to display images.
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
static double measureTimeMs()
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1)
void setQuality(double qualityLevel)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the opencv library.
void setTrackerId(int tid)
Does nothing. Just here for compat with previous releases that use OpenCV C api to do the tracking...
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
void setPyramidLevels(const int pyrMaxLevel)
void setWindowSize(const int winSize)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
void setBlockSize(const int blockSize)
virtual bool getClick(bool blocking=true)=0
void setUseHarris(const int useHarrisDetector)
static void read(vpImage< unsigned char > &I, const char *filename)
void track(const cv::Mat &I)