47 #include <visp3/core/vpConfig.h>
49 #if defined(VISP_HAVE_MODULE_TT) && defined(VISP_HAVE_DISPLAY)
51 #include <visp3/core/vpDebug.h>
52 #include <visp3/core/vpHomogeneousMatrix.h>
53 #include <visp3/core/vpIoTools.h>
54 #include <visp3/core/vpMath.h>
55 #include <visp3/gui/vpDisplayD3D.h>
56 #include <visp3/gui/vpDisplayGDI.h>
57 #include <visp3/gui/vpDisplayGTK.h>
58 #include <visp3/gui/vpDisplayOpenCV.h>
59 #include <visp3/gui/vpDisplayX.h>
60 #include <visp3/io/vpImageIo.h>
61 #include <visp3/io/vpParseArgv.h>
62 #include <visp3/io/vpVideoReader.h>
64 #include <visp3/tt/vpTemplateTrackerSSD.h>
65 #include <visp3/tt/vpTemplateTrackerSSDESM.h>
66 #include <visp3/tt/vpTemplateTrackerSSDForwardAdditional.h>
67 #include <visp3/tt/vpTemplateTrackerSSDForwardCompositional.h>
68 #include <visp3/tt/vpTemplateTrackerSSDInverseCompositional.h>
69 #include <visp3/tt/vpTemplateTrackerZNCCForwardAdditional.h>
70 #include <visp3/tt/vpTemplateTrackerZNCCInverseCompositional.h>
72 #include <visp3/tt/vpTemplateTrackerWarpAffine.h>
73 #include <visp3/tt/vpTemplateTrackerWarpHomography.h>
74 #include <visp3/tt/vpTemplateTrackerWarpHomographySL3.h>
75 #include <visp3/tt/vpTemplateTrackerWarpRT.h>
76 #include <visp3/tt/vpTemplateTrackerWarpSRT.h>
77 #include <visp3/tt/vpTemplateTrackerWarpTranslation.h>
79 #ifdef VISP_HAVE_MODULE_TT_MI
80 #include <visp3/tt_mi/vpTemplateTrackerMIESM.h>
81 #include <visp3/tt_mi/vpTemplateTrackerMIForwardAdditional.h>
82 #include <visp3/tt_mi/vpTemplateTrackerMIForwardCompositional.h>
83 #include <visp3/tt_mi/vpTemplateTrackerMIInverseCompositional.h>
86 #define GETOPTARGS "cdhi:l:Lprs:t:w:"
88 #ifdef ENABLE_VISP_NAMESPACE
92 #ifndef DOXYGEN_SHOULD_SKIP_THIS
100 #ifdef VISP_HAVE_MODULE_TT_MI
109 TRACKER_SSD_FORWARD_ADDITIONAL,
110 TRACKER_SSD_FORWARD_COMPOSITIONAL,
111 TRACKER_SSD_INVERSE_COMPOSITIONAL,
112 TRACKER_ZNCC_FORWARD_ADDITIONEL,
113 TRACKER_ZNCC_INVERSE_COMPOSITIONAL,
114 #ifdef VISP_HAVE_MODULE_TT_MI
116 TRACKER_MI_FORWARD_ADDITIONAL,
117 TRACKER_MI_FORWARD_COMPOSITIONAL,
118 TRACKER_MI_INVERSE_COMPOSITIONAL,
125 void usage(
const char *name,
const char *badparam,
const WarpType &warp_type, TrackerType &tracker_type,
126 const long &last_frame,
const double &residual_threhold)
128 #if VISP_HAVE_DATASET_VERSION >= 0x030600
129 std::string ext(
"png");
131 std::string ext(
"pgm");
135 Example of template tracking.\n\
138 %s [-i <test image path>] [-c] [-d] [-p] \n\
139 [-w <warp type>] [-t <tracker type>] \n\
140 [-l <last frame number>] [-r] [-L] [-h]\n",
145 -i <input image path> \n\
146 Set image input path.\n\
147 From this path read images \n\
148 \"mire-2/image%%04d.%s\". These \n\
149 images come from ViSP-images-x.y.z.tar.gz available \n\
150 on the ViSP website.\n\
151 Setting the VISP_INPUT_IMAGE_PATH environment\n\
152 variable produces the same behaviour than using\n\
155 -l <last frame number> %ld\n\
156 Last frame number to consider.\n\
159 Turn off the display.\n\
162 Disable the mouse click. Useful to automate the \n\
163 execution of this program without human intervention.\n\
165 ext.c_str(), last_frame);
167 #ifdef VISP_HAVE_MODULE_TT_MI
169 -w <warp type=[0,1,2,3,4,5]> %d\n\
170 Set the model used to warp the template. \n\
171 Authorized values are:\n\
174 %d : Homography in SL3\n\
175 %d : SRT (scale, rotation, translation)\n\
177 %d : RT (rotation, translation)\n\n",
178 (
int)warp_type, (
int)WARP_AFFINE, (
int)WARP_HOMOGRAPHY, (
int)WARP_HOMOGRAPHY_SL3, (
int)WARP_SRT,
179 (
int)WARP_TRANSLATION, (
int)WARP_RT);
182 -w <warp type=[0,1,2,3,4]> %d\n\
183 Set the model used to warp the template. \n\
184 Authorized values are:\n\
187 %d : Homography in SL3\n\
188 %d : SRT (scale, rotation, translation)\n\
189 %d : Translation\n\n",
190 (
int)warp_type, (
int)WARP_AFFINE, (
int)WARP_HOMOGRAPHY, (
int)WARP_HOMOGRAPHY_SL3, (
int)WARP_SRT,
191 (
int)WARP_TRANSLATION);
194 #ifdef VISP_HAVE_MODULE_TT_MI
196 -t <tracker type=[0,1,2,3,4,5,6,7,8,9]> %d\n\
197 Set the tracker used to track the template. \n\
198 Authorized values are:\n\
200 %d : SSD forward additional\n\
201 %d : SSD forward compositional\n\
202 %d : SSD inverse compositional\n\
203 %d : ZNCC forward additional\n\
204 %d : ZNCC inverse compositional\n\
206 %d : MI forward additional\n\
207 %d : MI forward compositional\n\
208 %d : MI inverse compositional\n",
209 (
int)tracker_type, (
int)TRACKER_SSD_ESM, (
int)TRACKER_SSD_FORWARD_ADDITIONAL,
210 (
int)TRACKER_SSD_FORWARD_COMPOSITIONAL, (
int)TRACKER_SSD_INVERSE_COMPOSITIONAL,
211 (
int)TRACKER_ZNCC_FORWARD_ADDITIONEL, (
int)TRACKER_ZNCC_INVERSE_COMPOSITIONAL, (
int)TRACKER_MI_ESM,
212 (
int)TRACKER_MI_FORWARD_ADDITIONAL, (
int)TRACKER_MI_FORWARD_COMPOSITIONAL,
213 (
int)TRACKER_MI_INVERSE_COMPOSITIONAL);
216 -t <tracker type=[0,1,2,3,4,5]> %d\n\
217 Set the tracker used to track the template. \n\
218 Authorized values are:\n\
220 %d : SSD forward additional\n\
221 %d : SSD forward compositional\n\
222 %d : SSD inverse compositional\n\
223 %d : ZNCC forward additional\n\
224 %d : ZNCC inverse compositional\n",
225 (
int)tracker_type, (
int)TRACKER_SSD_ESM, (
int)TRACKER_SSD_FORWARD_ADDITIONAL,
226 (
int)TRACKER_SSD_FORWARD_COMPOSITIONAL, (
int)TRACKER_SSD_INVERSE_COMPOSITIONAL,
227 (
int)TRACKER_ZNCC_FORWARD_ADDITIONEL, (
int)TRACKER_ZNCC_INVERSE_COMPOSITIONAL);
232 Enable pyramidal tracking.\n\
235 Disable re-init at frame 10.\n\
237 -s <residual threshold> %g\n\
238 Threshold used to stop optimization when residual difference\n\
239 between two successive optimization iteration becomes lower\n\
240 that this parameter.\n\
246 Print the help.\n\n",
250 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
253 bool getOptions(
int argc,
const char **argv, std::string &ipath,
bool &click_allowed,
bool &display,
bool &pyramidal,
254 WarpType &warp_type, TrackerType &tracker_type,
long &last_frame,
bool &reinit,
255 double &threshold_residual,
bool &log)
263 click_allowed =
false;
269 usage(argv[0],
nullptr, warp_type, tracker_type, last_frame, threshold_residual);
276 last_frame = (long)atoi(optarg_);
288 tracker_type = (TrackerType)atoi(optarg_);
291 warp_type = (WarpType)atoi(optarg_);
294 threshold_residual = std::atof(optarg_);
298 usage(argv[0], optarg_, warp_type, tracker_type, last_frame, threshold_residual);
304 if (warp_type >= WARP_LAST) {
305 usage(argv[0],
nullptr, warp_type, tracker_type, last_frame, threshold_residual);
306 std::cerr <<
"ERROR: " << std::endl;
307 std::cerr <<
" Bad argument -w <warp type> with \"warp type\"=" << (int)warp_type << std::endl << std::endl;
310 if (tracker_type >= TRACKER_LAST) {
311 usage(argv[0],
nullptr, warp_type, tracker_type, last_frame, threshold_residual);
312 std::cerr <<
"ERROR: " << std::endl;
313 std::cerr <<
" Bad argument -t <tracker type> with \"tracker type\"=" << (int)tracker_type << std::endl
317 if ((c == 1) || (c == -1)) {
319 usage(argv[0],
nullptr, warp_type, tracker_type, last_frame, threshold_residual);
320 std::cerr <<
"ERROR: " << std::endl;
321 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
328 int main(
int argc,
const char **argv)
330 #if defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV)
332 std::string env_ipath;
333 std::string opt_ipath;
335 bool opt_click_allowed =
true;
336 bool opt_display =
true;
337 bool opt_pyramidal =
false;
338 TrackerType opt_tracker_type = TRACKER_SSD_INVERSE_COMPOSITIONAL;
339 WarpType opt_warp_type = WARP_AFFINE;
340 long opt_last_frame = 30;
341 bool opt_reinit =
true;
342 double opt_threshold_residual = 1e-4;
343 bool opt_log =
false;
347 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
348 std::string opath =
"/tmp";
349 #elif defined(_WIN32)
350 std::string opath =
"C:\\temp";
353 #if VISP_HAVE_DATASET_VERSION >= 0x030600
354 std::string ext(
"png");
356 std::string ext(
"pgm");
360 std::string username;
368 if (!env_ipath.empty())
376 if (!getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display, opt_pyramidal, opt_warp_type,
377 opt_tracker_type, opt_last_frame, opt_reinit, opt_threshold_residual, opt_log)) {
382 if (opt_ipath.empty() && env_ipath.empty()) {
383 usage(argv[0],
nullptr, opt_warp_type, opt_tracker_type, opt_last_frame, opt_threshold_residual);
384 std::cerr << std::endl <<
"ERROR:" << std::endl;
385 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
386 <<
" environment variable to specify the location of the " << std::endl
387 <<
" image path where test images are located." << std::endl
394 if (!opt_ipath.empty())
400 ofs.open(logfilename.c_str());
413 std::cout <<
"Cannot open sequence: " << ipath << std::endl;
421 #if defined(VISP_HAVE_X11)
422 display =
new vpDisplayX;
423 #elif defined(VISP_HAVE_GDI)
425 #elif defined(HAVE_OPENCV_HIGHGUI)
427 #elif defined(VISP_HAVE_D3D9)
429 #elif defined(VISP_HAVE_GTK)
434 #if defined(VISP_HAVE_DISPLAY)
435 display->init(I, 100, 100,
"Test tracking");
442 switch (opt_warp_type) {
446 case WARP_HOMOGRAPHY:
449 case WARP_HOMOGRAPHY_SL3:
455 case WARP_TRANSLATION:
458 #ifdef VISP_HAVE_MODULE_TT_MI
468 switch (opt_tracker_type) {
469 case TRACKER_SSD_ESM:
472 case TRACKER_SSD_FORWARD_ADDITIONAL:
475 case TRACKER_SSD_FORWARD_COMPOSITIONAL:
478 case TRACKER_SSD_INVERSE_COMPOSITIONAL:
481 case TRACKER_ZNCC_FORWARD_ADDITIONEL:
484 case TRACKER_ZNCC_INVERSE_COMPOSITIONAL:
487 #ifdef VISP_HAVE_MODULE_TT_MI
491 case TRACKER_MI_FORWARD_ADDITIONAL:
494 case TRACKER_MI_FORWARD_COMPOSITIONAL:
497 case TRACKER_MI_INVERSE_COMPOSITIONAL:
513 bool delaunay =
false;
514 if (opt_display && opt_click_allowed)
517 std::vector<vpImagePoint> v_ip;
538 while (!reader.
end()) {
541 std::cout <<
"Process image number " << reader.
getFrameIndex() << std::endl;
553 std::cout <<
"re-init simulation" << std::endl;
554 if (opt_click_allowed)
559 if (opt_display && opt_click_allowed) {
565 std::vector<vpImagePoint> v_ip;
591 warp_->
warpZone(zoneRef_, p_, zoneWarped_);
601 std::cout <<
"Total time: " << t_end - t_init <<
" ms" << std::endl;
602 std::cout <<
"Total mean: " << (t_end - t_init) / niter <<
" ms" << std::endl;
605 std::cout <<
"Log are saved in: " << logfilename << std::endl;
609 if (opt_click_allowed) {
624 std::cout <<
"Catch an exception: " << e << std::endl;
630 std::cout <<
"Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
638 std::cout <<
"visp_tt module or display not available." << std::endl;
Implementation of column vector and the associated operations.
static const vpColor green
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
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...
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.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void set_ij(double ii, double jj)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
void warpZone(const vpTemplateTrackerZone &in, const vpColVector &p, vpTemplateTrackerZone &out)
void display(const vpImage< unsigned char > &I, const vpColor &col=vpColor::green, unsigned int thickness=3)
void display(const vpImage< unsigned char > &I, const vpColor &col=vpColor::green, unsigned int thickness=3)
void setThresholdGradient(double threshold)
unsigned int getNbIteration() const
void setThresholdResidualDifference(double threshold)
void initFromPoints(const vpImage< unsigned char > &I, const std::vector< vpImagePoint > &v_ip, bool delaunay=false)
void setIterationMax(const unsigned int &n)
void setPyramidal(unsigned int nlevels=2, unsigned int level_to_stop=1)
void track(const vpImage< unsigned char > &I)
vpTemplateTrackerZone getZoneRef() const
void setSampling(int sample_i, int sample_j)
vpTemplateTrackerWarp * getWarp() const
void initClick(const vpImage< unsigned char > &I, bool delaunay=false)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void setLastFrameIndex(const long last_frame)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void setFirstFrameIndex(const long first_frame)
long getFrameIndex() const
VISP_EXPORT double measureTimeMs()