44 #include <visp3/core/vpDebug.h>
45 #include <visp3/core/vpConfig.h>
52 #if defined(VISP_HAVE_MODULE_KLT) && (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined (VISP_HAVE_OPENCV))
54 #if defined (VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
56 #include <visp3/klt/vpKltOpencv.h>
57 #include <visp3/core/vpImage.h>
58 #include <visp3/io/vpImageIo.h>
59 #include <visp3/gui/vpDisplayX.h>
60 #include <visp3/gui/vpDisplayGTK.h>
61 #include <visp3/gui/vpDisplayGDI.h>
62 #include <visp3/gui/vpDisplayOpenCV.h>
63 #include <visp3/io/vpParseArgv.h>
64 #include <visp3/core/vpIoTools.h>
67 #define GETOPTARGS "cdf:i:n:p:s:h"
69 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
70 unsigned first,
unsigned nimages,
unsigned step);
71 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
72 unsigned &nimages,
unsigned &step,
bool &click_allowed,
bool &display);
94 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
95 unsigned first,
unsigned nimages,
unsigned step)
98 Example of KLT tracking using OpenCV library.\n\
101 %s [-i <test image path>] [-p <personal image path>]\n\
102 [-f <first image>] [-n <number of images>] [-s <step>]\n\
103 [-c] [-d] [-h]\n", name);
107 -i <input image path> %s\n\
108 Set image input path.\n\
109 From this path read images \n\
110 \"ViSP-images/mire-2/image.%%04d.pgm\". These \n\
111 images come from ViSP-images-x.y.z.tar.gz available \n\
112 on the ViSP website.\n\
113 Setting the VISP_INPUT_IMAGE_PATH environment\n\
114 variable produces the same behaviour than using\n\
117 -p <personal image path> %s\n\
118 Specify a personal sequence containing images \n\
120 By image sequence, we mean one file per image.\n\
121 The following image file formats PNM (PGM P5, PPM P6)\n\
122 are supported. The format is selected by analysing \n\
123 the filename extension.\n\
124 Example : \"/Temp/ViSP-images/cube/image.%%04d.pgm\"\n\
125 %%04d is for the image numbering.\n\
127 -f <first image> %u\n\
128 First image number of the sequence.\n\
130 -n <number of images> %u\n\
131 Number of images to load from the sequence.\n\
134 Step between two images.\n\
137 Disable the mouse click. Useful to automaze the \n\
138 execution of this program without humain intervention.\n\
141 Turn off the display.\n\
145 ipath.c_str(), ppath.c_str(), first, nimages, step);
148 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
167 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
168 unsigned &nimages,
unsigned &step,
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)
206 std::string env_ipath;
207 std::string opt_ipath;
209 std::string opt_ppath;
211 std::string filename;
212 unsigned opt_first = 1;
213 unsigned opt_nimages = 500;
214 unsigned opt_step = 1;
215 bool opt_click_allowed =
true;
216 bool opt_display =
true;
222 if (! env_ipath.empty())
226 if (getOptions(argc, argv, opt_ipath, opt_ppath,opt_first, opt_nimages,
227 opt_step, opt_click_allowed, opt_display) ==
false) {
232 if (!opt_ipath.empty())
237 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
238 if (ipath != env_ipath) {
239 std::cout << std::endl
240 <<
"WARNING: " << std::endl;
241 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
242 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
243 <<
" we skip the environment variable." << std::endl;
248 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty() ){
249 usage(argv[0], NULL, ipath, opt_ppath, opt_first, opt_nimages, opt_step);
250 std::cerr << std::endl
251 <<
"ERROR:" << std::endl;
252 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
254 <<
" environment variable to specify the location of the " << std::endl
255 <<
" image path where test images are located." << std::endl
256 <<
" Use -p <personal image path> option if you want to "<<std::endl
257 <<
" use personal images." << std::endl
267 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
268 IplImage * cvI = NULL;
273 unsigned iter = opt_first;
274 std::ostringstream s;
275 char cfilename[FILENAME_MAX];
277 if (opt_ppath.empty()){
297 s.setf(std::ios::right, std::ios::adjustfield);
298 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
302 sprintf(cfilename, opt_ppath.c_str(), iter) ;
303 filename = cfilename;
313 std::cout <<
"Load: " << filename << std::endl;
325 std::cerr << std::endl
326 <<
"ERROR:" << std::endl;
327 std::cerr <<
" Cannot read " << filename << std::endl;
328 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
329 <<
" or VISP_INPUT_IMAGE_PATH environment variable."
335 #if defined VISP_HAVE_X11
337 #elif defined VISP_HAVE_GTK
339 #elif defined VISP_HAVE_GDI
341 #elif defined VISP_HAVE_OPENCV
347 display.
init(vpI, 100, 100,
"Display...") ;
386 while (iter < opt_first + opt_nimages*opt_step) {
388 if (opt_ppath.empty()){
390 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
394 sprintf(cfilename, opt_ppath.c_str(), iter) ;
395 filename = cfilename;
412 std::cout <<
"Tracking on image: " << filename << std::endl;
416 std::cout <<
"Tracking performed in " <<
427 if (opt_display && opt_click_allowed) {
428 std::cout <<
"\nA click to exit..." << std::endl;
435 std::cout <<
"Catch an exception: " << e << std::endl;
451 std::cout <<
"visp_klt module or X11, GTK, GDI or OpenCV display functionalities are required..." << std::endl;
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
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. Thus to enable this class OpenC...
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 3rd party library. Thus to enable this class G...
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
void setPyramidLevels(const int pyrMaxLevel)
void setWindowSize(const int winSize)
static void read(vpImage< unsigned char > &I, const std::string &filename)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
void setBlockSize(const int blockSize)
void setUseHarris(const int useHarrisDetector)
void track(const cv::Mat &I)