41 #include <visp3/core/vpConfig.h>
43 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_DISPLAY)
45 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
49 #include <visp3/core/vpImage.h>
50 #include <visp3/core/vpIoTools.h>
51 #include <visp3/gui/vpDisplayFactory.h>
52 #include <visp3/io/vpImageIo.h>
53 #include <visp3/io/vpParseArgv.h>
54 #include <visp3/klt/vpKltOpencv.h>
57 #define GETOPTARGS "cdf:i:l:p:s:h"
59 #ifdef ENABLE_VISP_NAMESPACE
63 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
unsigned first,
64 unsigned last,
unsigned step);
65 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
unsigned &last,
66 unsigned &step,
bool &click_allowed,
bool &display);
88 void usage(
const char *name,
const char *badparam, std::string ipath, std::string ppath,
unsigned first,
89 unsigned last,
unsigned step)
91 #if defined(VISP_HAVE_DATASET)
92 #if VISP_HAVE_DATASET_VERSION >= 0x030600
93 std::string ext(
"png");
95 std::string ext(
"pgm");
99 std::string ext(
"png");
102 Example of KLT tracking using OpenCV library.\n\
105 %s [-i <test image path>] [-p <personal image path>]\n\
106 [-f <first image>] [-l <last image>] [-s <step>]\n\
112 -i <input image path> %s\n\
113 Set image input path.\n\
114 From this path read images \n\
115 \"mire-2/image.%%04d.%s\". These \n\
116 images come from visp-images-x.y.z.tar.gz available \n\
117 on the ViSP website.\n\
118 Setting the VISP_INPUT_IMAGE_PATH environment\n\
119 variable produces the same behaviour than using\n\
122 -p <personal image path> %s\n\
123 Specify a personal sequence containing images \n\
125 By image sequence, we mean one file per image.\n\
126 Example : \"/Temp/visp-images/mire-2/image.%%04d.%s\"\n\
127 %%04d is for the image numbering.\n\
129 -f <first image> %u\n\
130 First image number of the sequence.\n\
132 -l <last image> %u\n\
133 Last image number of the sequence.\n\
136 Step between two images.\n\
139 Disable the mouse click. Useful to automate the \n\
140 execution of this program without human intervention.\n\
143 Turn off the display.\n\
147 ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
150 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
170 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
unsigned &last,
171 unsigned &step,
bool &click_allowed,
bool &display)
179 click_allowed =
false;
191 first = (unsigned)atoi(optarg_);
194 last = (unsigned)atoi(optarg_);
197 step = (unsigned)atoi(optarg_);
200 usage(argv[0],
nullptr, ipath, ppath, first, last, step);
205 usage(argv[0], optarg_, ipath, ppath, first, last, step);
211 if ((c == 1) || (c == -1)) {
213 usage(argv[0],
nullptr, ipath, ppath, first, last, step);
214 std::cerr <<
"ERROR: " << std::endl;
215 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
222 int main(
int argc,
const char **argv)
225 std::string env_ipath;
226 std::string opt_ipath;
228 std::string opt_ppath;
230 std::string filename;
231 unsigned opt_first = 1;
232 unsigned opt_last = 500;
233 unsigned opt_step = 1;
234 bool opt_click_allowed =
true;
235 bool opt_display =
true;
237 #if defined(VISP_HAVE_DATASET)
238 #if VISP_HAVE_DATASET_VERSION >= 0x030600
239 std::string ext(
"png");
241 std::string ext(
"pgm");
245 std::string ext(
"png");
253 if (!env_ipath.empty())
257 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
258 opt_display) ==
false) {
263 if (!opt_ipath.empty())
268 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
269 if (ipath != env_ipath) {
270 std::cout << std::endl <<
"WARNING: " << std::endl;
271 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
272 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
273 <<
" we skip the environment variable." << std::endl;
278 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
279 usage(argv[0],
nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
280 std::cerr << std::endl <<
"ERROR:" << std::endl;
281 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
282 <<
" environment variable to specify the location of the " << std::endl
283 <<
" image path where test images are located." << std::endl
284 <<
" Use -p <personal image path> option if you want to " << std::endl
285 <<
" use personal images." << std::endl
298 unsigned iter = opt_first;
299 std::ostringstream s;
300 char cfilename[FILENAME_MAX];
302 if (opt_ppath.empty()) {
317 s.setf(std::ios::right, std::ios::adjustfield);
318 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
"." << ext;
322 snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
323 filename = cfilename;
332 std::cout <<
"Load: " << filename << std::endl;
340 std::cerr << std::endl <<
"ERROR:" << std::endl;
341 std::cerr <<
" Cannot read " << filename << std::endl;
342 if (opt_ppath.empty()) {
343 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
344 <<
" or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
347 std::cerr <<
" Check your -p " << opt_ppath <<
" option " << std::endl;
357 display->init(vpI, 100, 100,
"Display...");
398 while ((iter < opt_last) && (!quit)) {
400 if (opt_ppath.empty()) {
402 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
"." << ext;
406 snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
407 filename = cfilename;
424 std::cout <<
"Tracking on image: " << filename << std::endl;
441 if (opt_display && opt_click_allowed && !quit) {
442 std::cout <<
"\nA click to exit..." << std::endl;
452 std::cout <<
"Catch an exception: " << e << std::endl;
459 std::cout <<
"You do not have OpenCV functionalities to display images..." << std::endl;
460 std::cout <<
"Tip:" << std::endl;
461 std::cout <<
"- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
470 std::cout <<
"visp_klt module or X11, GTK, GDI or OpenCV display functionalities are required..." << std::endl;
Class that defines generic functionalities for display.
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)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
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)
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()