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
63void usage(
const char *name,
const char *badparam,
const std::string &ipath,
const std::string &ppath,
unsigned first,
64 unsigned last,
unsigned step);
65bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath,
unsigned &first,
unsigned &last,
66 unsigned &step,
bool &click_allowed,
bool &display);
88void usage(
const char *name,
const char *badparam,
const std::string &ipath,
const 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");
102Example 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);
170bool 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 =
static_cast<unsigned int>(atoi(optarg_));
194 last =
static_cast<unsigned int>(atoi(optarg_));
197 step =
static_cast<unsigned int>(atoi(optarg_));
200 usage(argv[0],
nullptr, ipath, ppath, first, last, step);
204 usage(argv[0], optarg_, ipath, ppath, first, last, step);
209 if ((c == 1) || (c == -1)) {
211 usage(argv[0],
nullptr, ipath, ppath, first, last, step);
212 std::cerr <<
"ERROR: " << std::endl;
213 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
220int main(
int argc,
const char **argv)
223 std::string env_ipath;
224 std::string opt_ipath;
226 std::string opt_ppath;
229 unsigned opt_first = 1;
230 unsigned opt_last = 500;
231 unsigned opt_step = 1;
232 bool opt_click_allowed =
true;
233 bool opt_display =
true;
235#if defined(VISP_HAVE_DATASET)
236#if VISP_HAVE_DATASET_VERSION >= 0x030600
237 std::string ext(
"png");
239 std::string ext(
"pgm");
243 std::string ext(
"png");
251 if (!env_ipath.empty())
255 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
256 opt_display) ==
false) {
261 if (!opt_ipath.empty())
266 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
267 if (ipath != env_ipath) {
268 std::cout << std::endl <<
"WARNING: " << std::endl;
269 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
270 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
271 <<
" we skip the environment variable." << std::endl;
276 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
277 usage(argv[0],
nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
278 std::cerr << std::endl <<
"ERROR:" << std::endl;
279 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
280 <<
" environment variable to specify the location of the " << std::endl
281 <<
" image path where test images are located." << std::endl
282 <<
" Use -p <personal image path> option if you want to " << std::endl
283 <<
" use personal images." << std::endl
296 unsigned iter = opt_first;
298 if (opt_ppath.empty()) {
325 std::cout <<
"Load: " <<
filename << std::endl;
333 std::cerr << std::endl <<
"ERROR:" << std::endl;
334 std::cerr <<
" Cannot read " <<
filename << std::endl;
335 if (opt_ppath.empty()) {
336 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
337 <<
" or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
340 std::cerr <<
" Check your -p " << opt_ppath <<
" option " << std::endl;
350 display->init(vpI, 100, 100,
"Display...");
375 tracker.setHarrisFreeParameter(0.04);
391 while ((iter < opt_last) && (!quit)) {
393 if (opt_ppath.empty()) {
415 std::cout <<
"Tracking on image: " <<
filename << std::endl;
432 if (opt_display && opt_click_allowed && !quit) {
433 std::cout <<
"\nA click to exit..." << std::endl;
443 std::cout <<
"Catch an exception: " <<
e << std::endl;
450 std::cout <<
"You do not have OpenCV functionalities to display images..." << std::endl;
451 std::cout <<
"Tip:" << std::endl;
452 std::cout <<
"- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
461 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)
Definition of the vpImage class member functions.
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
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()