45 #include <visp3/core/vpConfig.h> 46 #include <visp3/core/vpDebug.h> 53 #if defined(VISP_HAVE_MODULE_KLT) && \ 54 (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) 56 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100) 58 #include <visp3/core/vpImage.h> 59 #include <visp3/core/vpIoTools.h> 60 #include <visp3/gui/vpDisplayGDI.h> 61 #include <visp3/gui/vpDisplayGTK.h> 62 #include <visp3/gui/vpDisplayOpenCV.h> 63 #include <visp3/gui/vpDisplayX.h> 64 #include <visp3/io/vpImageIo.h> 65 #include <visp3/io/vpParseArgv.h> 66 #include <visp3/klt/vpKltOpencv.h> 69 #define GETOPTARGS "cdf:i:n:p:s:h" 71 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
unsigned first,
72 unsigned nimages,
unsigned step);
73 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
unsigned &nimages,
74 unsigned &step,
bool &click_allowed,
bool &display);
96 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
unsigned first,
97 unsigned nimages,
unsigned step)
100 Example of KLT tracking using OpenCV library.\n\ 103 %s [-i <test image path>] [-p <personal image path>]\n\ 104 [-f <first image>] [-n <number of images>] [-s <step>]\n\ 105 [-c] [-d] [-h]\n", name);
109 -i <input image path> %s\n\ 110 Set image input path.\n\ 111 From this path read images \n\ 112 \"mire-2/image.%%04d.pgm\". These \n\ 113 images come from ViSP-images-x.y.z.tar.gz available \n\ 114 on the ViSP website.\n\ 115 Setting the VISP_INPUT_IMAGE_PATH environment\n\ 116 variable produces the same behaviour than using\n\ 119 -p <personal image path> %s\n\ 120 Specify a personal sequence containing images \n\ 122 By image sequence, we mean one file per image.\n\ 123 The following image file formats PNM (PGM P5, PPM P6)\n\ 124 are supported. The format is selected by analysing \n\ 125 the filename extension.\n\ 126 Example : \"/Temp/ViSP-images/cube/image.%%04d.pgm\"\n\ 127 %%04d is for the image numbering.\n\ 129 -f <first image> %u\n\ 130 First image number of the sequence.\n\ 132 -n <number of images> %u\n\ 133 Number of images to load from the sequence.\n\ 136 Step between two images.\n\ 139 Disable the mouse click. Useful to automaze the \n\ 140 execution of this program without humain intervention.\n\ 143 Turn off the display.\n\ 146 Print the help.\n", ipath.c_str(), ppath.c_str(), first, nimages, step);
149 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
168 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
unsigned &nimages,
169 unsigned &step,
bool &click_allowed,
bool &display)
177 click_allowed =
false;
189 first = (unsigned)atoi(optarg_);
192 nimages = (unsigned)atoi(optarg_);
195 step = (unsigned)atoi(optarg_);
198 usage(argv[0], NULL, ipath, ppath, first, nimages, step);
203 usage(argv[0], optarg_, ipath, ppath, first, nimages, step);
209 if ((c == 1) || (c == -1)) {
211 usage(argv[0], NULL, ipath, ppath, first, nimages, step);
212 std::cerr <<
"ERROR: " << std::endl;
213 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
220 int main(
int argc,
const char **argv)
223 std::string env_ipath;
224 std::string opt_ipath;
226 std::string opt_ppath;
228 std::string filename;
229 unsigned opt_first = 1;
230 unsigned opt_nimages = 500;
231 unsigned opt_step = 1;
232 bool opt_click_allowed =
true;
233 bool opt_display =
true;
240 if (!env_ipath.empty())
244 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_nimages, opt_step, opt_click_allowed,
245 opt_display) ==
false) {
250 if (!opt_ipath.empty())
255 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
256 if (ipath != env_ipath) {
257 std::cout << std::endl <<
"WARNING: " << std::endl;
258 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 259 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
260 <<
" we skip the environment variable." << std::endl;
265 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
266 usage(argv[0], NULL, ipath, opt_ppath, opt_first, opt_nimages, opt_step);
267 std::cerr << std::endl <<
"ERROR:" << std::endl;
268 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
269 <<
" environment variable to specify the location of the " << std::endl
270 <<
" image path where test images are located." << std::endl
271 <<
" Use -p <personal image path> option if you want to " << std::endl
272 <<
" use personal images." << std::endl
282 #if (VISP_HAVE_OPENCV_VERSION < 0x020408) 283 IplImage *cvI = NULL;
288 unsigned iter = opt_first;
289 std::ostringstream s;
290 char cfilename[FILENAME_MAX];
292 if (opt_ppath.empty()) {
311 s.setf(std::ios::right, std::ios::adjustfield);
312 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
315 sprintf(cfilename, opt_ppath.c_str(), iter);
316 filename = cfilename;
326 std::cout <<
"Load: " << filename << std::endl;
336 std::cerr << std::endl <<
"ERROR:" << std::endl;
337 std::cerr <<
" Cannot read " << filename << std::endl;
338 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
339 <<
" or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
344 #if defined VISP_HAVE_X11 346 #elif defined VISP_HAVE_GTK 348 #elif defined VISP_HAVE_GDI 350 #elif defined VISP_HAVE_OPENCV 356 display.
init(vpI, 100, 100,
"Display...");
395 while (iter < opt_first + opt_nimages * opt_step) {
397 if (opt_ppath.empty()) {
399 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
402 sprintf(cfilename, opt_ppath.c_str(), iter);
403 filename = cfilename;
420 std::cout <<
"Tracking on image: " << filename << std::endl;
434 if (opt_display && opt_click_allowed) {
435 std::cout <<
"\nA click to exit..." << std::endl;
441 std::cout <<
"Catch an exception: " << e << std::endl;
448 std::cout <<
"You do not have OpenCV functionalities to display images..." << std::endl;
449 std::cout <<
"Tip:" << std::endl;
450 std::cout <<
"- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
459 std::cout <<
"visp_klt module or X11, GTK, GDI or OpenCV display " 460 "functionalities are required..." 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)
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)