41 #include <visp3/core/vpConfig.h>
42 #include <visp3/core/vpDebug.h>
49 #if defined(VISP_HAVE_MODULE_KLT) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
51 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
53 #include <visp3/core/vpImage.h>
54 #include <visp3/core/vpIoTools.h>
55 #include <visp3/gui/vpDisplayGDI.h>
56 #include <visp3/gui/vpDisplayGTK.h>
57 #include <visp3/gui/vpDisplayOpenCV.h>
58 #include <visp3/gui/vpDisplayX.h>
59 #include <visp3/io/vpImageIo.h>
60 #include <visp3/io/vpParseArgv.h>
61 #include <visp3/klt/vpKltOpencv.h>
64 #define GETOPTARGS "cdf:i:l:p:s:h"
66 #ifdef ENABLE_VISP_NAMESPACE
70 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
unsigned first,
71 unsigned last,
unsigned step);
72 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
unsigned &last,
73 unsigned &step,
bool &click_allowed,
bool &display);
95 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
unsigned first,
96 unsigned last,
unsigned step)
98 #if VISP_HAVE_DATASET_VERSION >= 0x030600
99 std::string ext(
"png");
101 std::string ext(
"pgm");
104 Example of KLT tracking using OpenCV library.\n\
107 %s [-i <test image path>] [-p <personal image path>]\n\
108 [-f <first image>] [-l <last image>] [-s <step>]\n\
114 -i <input image path> %s\n\
115 Set image input path.\n\
116 From this path read images \n\
117 \"mire-2/image.%%04d.%s\". These \n\
118 images come from visp-images-x.y.z.tar.gz available \n\
119 on the ViSP website.\n\
120 Setting the VISP_INPUT_IMAGE_PATH environment\n\
121 variable produces the same behaviour than using\n\
124 -p <personal image path> %s\n\
125 Specify a personal sequence containing images \n\
127 By image sequence, we mean one file per image.\n\
128 Example : \"/Temp/visp-images/mire-2/image.%%04d.%s\"\n\
129 %%04d is for the image numbering.\n\
131 -f <first image> %u\n\
132 First image number of the sequence.\n\
134 -l <last image> %u\n\
135 Last image number of the sequence.\n\
138 Step between two images.\n\
141 Disable the mouse click. Useful to automate the \n\
142 execution of this program without human intervention.\n\
145 Turn off the display.\n\
149 ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
152 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
172 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
unsigned &last,
173 unsigned &step,
bool &click_allowed,
bool &display)
181 click_allowed =
false;
193 first = (unsigned)atoi(optarg_);
196 last = (unsigned)atoi(optarg_);
199 step = (unsigned)atoi(optarg_);
202 usage(argv[0],
nullptr, ipath, ppath, first, last, step);
207 usage(argv[0], optarg_, ipath, ppath, first, last, step);
213 if ((c == 1) || (c == -1)) {
215 usage(argv[0],
nullptr, ipath, ppath, first, last, step);
216 std::cerr <<
"ERROR: " << std::endl;
217 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
224 int main(
int argc,
const char **argv)
227 std::string env_ipath;
228 std::string opt_ipath;
230 std::string opt_ppath;
232 std::string filename;
233 unsigned opt_first = 1;
234 unsigned opt_last = 500;
235 unsigned opt_step = 1;
236 bool opt_click_allowed =
true;
237 bool opt_display =
true;
239 #if VISP_HAVE_DATASET_VERSION >= 0x030600
240 std::string ext(
"png");
242 std::string ext(
"pgm");
250 if (!env_ipath.empty())
254 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
255 opt_display) ==
false) {
260 if (!opt_ipath.empty())
265 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
266 if (ipath != env_ipath) {
267 std::cout << std::endl <<
"WARNING: " << std::endl;
268 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
269 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
270 <<
" we skip the environment variable." << std::endl;
275 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
276 usage(argv[0],
nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
277 std::cerr << std::endl <<
"ERROR:" << std::endl;
278 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
279 <<
" environment variable to specify the location of the " << std::endl
280 <<
" image path where test images are located." << std::endl
281 <<
" Use -p <personal image path> option if you want to " << std::endl
282 <<
" use personal images." << std::endl
294 unsigned iter = opt_first;
295 std::ostringstream s;
296 char cfilename[FILENAME_MAX];
298 if (opt_ppath.empty()) {
313 s.setf(std::ios::right, std::ios::adjustfield);
314 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
"." << ext;
318 snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
319 filename = cfilename;
328 std::cout <<
"Load: " << filename << std::endl;
336 std::cerr << std::endl <<
"ERROR:" << std::endl;
337 std::cerr <<
" Cannot read " << filename << std::endl;
338 if (opt_ppath.empty()) {
339 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
340 <<
" or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
343 std::cerr <<
" Check your -p " << opt_ppath <<
" option " << std::endl;
349 #if defined(VISP_HAVE_X11)
351 #elif defined(VISP_HAVE_GTK)
353 #elif defined(VISP_HAVE_GDI)
355 #elif defined(HAVE_OPENCV_HIGHGUI)
361 display.init(vpI, 100, 100,
"Display...");
400 while (iter < opt_last) {
402 if (opt_ppath.empty()) {
404 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
"." << ext;
408 snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
409 filename = cfilename;
426 std::cout <<
"Tracking on image: " << filename << std::endl;
440 if (opt_display && opt_click_allowed) {
441 std::cout <<
"\nA click to exit..." << std::endl;
448 std::cout <<
"Catch an exception: " << e << std::endl;
455 std::cout <<
"You do not have OpenCV functionalities to display images..." << std::endl;
456 std::cout <<
"Tip:" << std::endl;
457 std::cout <<
"- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
466 std::cout <<
"visp_klt module or X11, GTK, GDI or OpenCV display "
467 "functionalities are required..."
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1) const
void setBlockSize(int blockSize)
void setQuality(double qualityLevel)
void track(const cv::Mat &I)
void setTrackerId(int tid)
void setHarrisFreeParameter(double harris_k)
void setMaxFeatures(int maxCount)
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
void setMinDistance(double minDistance)
void setUseHarris(int useHarrisDetector)
void setWindowSize(int winSize)
void setPyramidLevels(int pyrMaxLevel)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT double measureTimeMs()