41#include <visp3/core/vpConfig.h>
43#if defined(VISP_HAVE_MODULE_TT) && defined(VISP_HAVE_DISPLAY)
45#include <visp3/core/vpDebug.h>
46#include <visp3/core/vpHomogeneousMatrix.h>
47#include <visp3/core/vpIoTools.h>
48#include <visp3/core/vpMath.h>
49#include <visp3/gui/vpDisplayD3D.h>
50#include <visp3/gui/vpDisplayGDI.h>
51#include <visp3/gui/vpDisplayGTK.h>
52#include <visp3/gui/vpDisplayOpenCV.h>
53#include <visp3/gui/vpDisplayX.h>
54#include <visp3/io/vpImageIo.h>
55#include <visp3/io/vpParseArgv.h>
56#include <visp3/io/vpVideoReader.h>
58#include <visp3/tt/vpTemplateTrackerSSD.h>
59#include <visp3/tt/vpTemplateTrackerSSDESM.h>
60#include <visp3/tt/vpTemplateTrackerSSDForwardAdditional.h>
61#include <visp3/tt/vpTemplateTrackerSSDForwardCompositional.h>
62#include <visp3/tt/vpTemplateTrackerSSDInverseCompositional.h>
63#include <visp3/tt/vpTemplateTrackerZNCCForwardAdditional.h>
64#include <visp3/tt/vpTemplateTrackerZNCCInverseCompositional.h>
66#include <visp3/tt/vpTemplateTrackerWarpAffine.h>
67#include <visp3/tt/vpTemplateTrackerWarpHomography.h>
68#include <visp3/tt/vpTemplateTrackerWarpHomographySL3.h>
69#include <visp3/tt/vpTemplateTrackerWarpRT.h>
70#include <visp3/tt/vpTemplateTrackerWarpSRT.h>
71#include <visp3/tt/vpTemplateTrackerWarpTranslation.h>
73#ifdef VISP_HAVE_MODULE_TT_MI
74#include <visp3/tt_mi/vpTemplateTrackerMIESM.h>
75#include <visp3/tt_mi/vpTemplateTrackerMIForwardAdditional.h>
76#include <visp3/tt_mi/vpTemplateTrackerMIForwardCompositional.h>
77#include <visp3/tt_mi/vpTemplateTrackerMIInverseCompositional.h>
80#define GETOPTARGS "cdhi:l:Lprs:t:w:"
82#ifdef ENABLE_VISP_NAMESPACE
86#ifndef DOXYGEN_SHOULD_SKIP_THIS
94#ifdef VISP_HAVE_MODULE_TT_MI
103 TRACKER_SSD_FORWARD_ADDITIONAL,
104 TRACKER_SSD_FORWARD_COMPOSITIONAL,
105 TRACKER_SSD_INVERSE_COMPOSITIONAL,
106 TRACKER_ZNCC_FORWARD_ADDITIONEL,
107 TRACKER_ZNCC_INVERSE_COMPOSITIONAL,
108#ifdef VISP_HAVE_MODULE_TT_MI
110 TRACKER_MI_FORWARD_ADDITIONAL,
111 TRACKER_MI_FORWARD_COMPOSITIONAL,
112 TRACKER_MI_INVERSE_COMPOSITIONAL,
119void usage(
const char *name,
const char *badparam,
const WarpType &warp_type, TrackerType &tracker_type,
120 const long &last_frame,
const double &residual_threhold)
122#if defined(VISP_HAVE_DATASET)
123#if VISP_HAVE_DATASET_VERSION >= 0x030600
124 std::string ext(
"png");
126 std::string ext(
"pgm");
130 std::string ext(
"png");
134Example of template tracking.\n\
137 %s [-i <test image path>] [-c] [-d] [-p] \n\
138 [-w <warp type>] [-t <tracker type>] \n\
139 [-l <last frame number>] [-r] [-L] [-h]\n",
144 -i <input image path> \n\
145 Set image input path.\n\
146 From this path read images \n\
147 \"mire-2/image%%04d.%s\". These \n\
148 images come from ViSP-images-x.y.z.tar.gz available \n\
149 on the ViSP website.\n\
150 Setting the VISP_INPUT_IMAGE_PATH environment\n\
151 variable produces the same behaviour than using\n\
154 -l <last frame number> %ld\n\
155 Last frame number to consider.\n\
158 Turn off the display.\n\
161 Disable the mouse click. Useful to automate the \n\
162 execution of this program without human intervention.\n\
164 ext.c_str(), last_frame);
166#ifdef VISP_HAVE_MODULE_TT_MI
168 -w <warp type=[0,1,2,3,4,5]> %d\n\
169 Set the model used to warp the template. \n\
170 Authorized values are:\n\
173 %d : Homography in SL3\n\
174 %d : SRT (scale, rotation, translation)\n\
176 %d : RT (rotation, translation)\n\n",
177 static_cast<int>(warp_type),
static_cast<int>(WARP_AFFINE),
static_cast<int>(WARP_HOMOGRAPHY),
178 static_cast<int>(WARP_HOMOGRAPHY_SL3),
static_cast<int>(WARP_SRT),
179 static_cast<int>(WARP_TRANSLATION),
static_cast<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 static_cast<int>(warp_type),
static_cast<int>(WARP_AFFINE),
static_cast<int>(WARP_HOMOGRAPHY),
191 static_cast<int>(WARP_HOMOGRAPHY_SL3),
static_cast<int>(WARP_SRT),
192 static_cast<int>(WARP_TRANSLATION));
195#ifdef VISP_HAVE_MODULE_TT_MI
197 -t <tracker type=[0,1,2,3,4,5,6,7,8,9]> %d\n\
198 Set the tracker used to track the template. \n\
199 Authorized values are:\n\
201 %d : SSD forward additional\n\
202 %d : SSD forward compositional\n\
203 %d : SSD inverse compositional\n\
204 %d : ZNCC forward additional\n\
205 %d : ZNCC inverse compositional\n\
207 %d : MI forward additional\n\
208 %d : MI forward compositional\n\
209 %d : MI inverse compositional\n",
210 static_cast<int>(tracker_type),
static_cast<int>(TRACKER_SSD_ESM),
static_cast<int>(TRACKER_SSD_FORWARD_ADDITIONAL),
211 static_cast<int>(TRACKER_SSD_FORWARD_COMPOSITIONAL),
static_cast<int>(TRACKER_SSD_INVERSE_COMPOSITIONAL),
212 static_cast<int>(TRACKER_ZNCC_FORWARD_ADDITIONEL),
static_cast<int>(TRACKER_ZNCC_INVERSE_COMPOSITIONAL),
static_cast<int>(TRACKER_MI_ESM),
213 static_cast<int>(TRACKER_MI_FORWARD_ADDITIONAL),
static_cast<int>(TRACKER_MI_FORWARD_COMPOSITIONAL),
214 static_cast<int>(TRACKER_MI_INVERSE_COMPOSITIONAL));
217 -t <tracker type=[0,1,2,3,4,5]> %d\n\
218 Set the tracker used to track the template. \n\
219 Authorized values are:\n\
221 %d : SSD forward additional\n\
222 %d : SSD forward compositional\n\
223 %d : SSD inverse compositional\n\
224 %d : ZNCC forward additional\n\
225 %d : ZNCC inverse compositional\n",
226 static_cast<int>(tracker_type),
static_cast<int>(TRACKER_SSD_ESM),
static_cast<int>(TRACKER_SSD_FORWARD_ADDITIONAL),
227 static_cast<int>(TRACKER_SSD_FORWARD_COMPOSITIONAL),
static_cast<int>(TRACKER_SSD_INVERSE_COMPOSITIONAL),
228 static_cast<int>(TRACKER_ZNCC_FORWARD_ADDITIONEL),
static_cast<int>(TRACKER_ZNCC_INVERSE_COMPOSITIONAL));
233 Enable pyramidal tracking.\n\
236 Disable re-init at frame 10.\n\
238 -s <residual threshold> %g\n\
239 Threshold used to stop optimization when residual difference\n\
240 between two successive optimization iteration becomes lower\n\
241 that this parameter.\n\
247 Print the help.\n\n",
251 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
254bool getOptions(
int argc,
const char **argv, std::string &ipath,
bool &click_allowed,
bool &display,
bool &pyramidal,
255 WarpType &warp_type, TrackerType &tracker_type,
long &last_frame,
bool &reinit,
256 double &threshold_residual,
bool &log)
264 click_allowed =
false;
270 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);
303 if (warp_type >= WARP_LAST) {
304 usage(argv[0],
nullptr, warp_type, tracker_type, last_frame, threshold_residual);
305 std::cerr <<
"ERROR: " << std::endl;
306 std::cerr <<
" Bad argument -w <warp type> with \"warp type\"=" <<
static_cast<int>(warp_type) << std::endl << std::endl;
309 if (tracker_type >= TRACKER_LAST) {
310 usage(argv[0],
nullptr, warp_type, tracker_type, last_frame, threshold_residual);
311 std::cerr <<
"ERROR: " << std::endl;
312 std::cerr <<
" Bad argument -t <tracker type> with \"tracker type\"=" <<
static_cast<int>(tracker_type) << std::endl
316 if ((c == 1) || (c == -1)) {
318 usage(argv[0],
nullptr, warp_type, tracker_type, last_frame, threshold_residual);
319 std::cerr <<
"ERROR: " << std::endl;
320 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
327int main(
int argc,
const char **argv)
329#if defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV)
331 std::string env_ipath;
332 std::string opt_ipath;
334 bool opt_click_allowed =
true;
335 bool opt_display =
true;
336 bool opt_pyramidal =
false;
337 TrackerType opt_tracker_type = TRACKER_SSD_INVERSE_COMPOSITIONAL;
338 WarpType opt_warp_type = WARP_AFFINE;
339 long opt_last_frame = 30;
340 bool opt_reinit =
true;
341 double opt_threshold_residual = 1
e-4;
342 bool opt_log =
false;
346#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
347 std::string opath =
"/tmp";
349 std::string opath =
"C:\\temp";
352#if defined(VISP_HAVE_DATASET)
353#if VISP_HAVE_DATASET_VERSION >= 0x030600
354 std::string ext(
"png");
356 std::string ext(
"pgm");
360 std::string ext(
"png");
364 std::string username;
372 if (!env_ipath.empty())
380 if (!getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display, opt_pyramidal, opt_warp_type,
381 opt_tracker_type, opt_last_frame, opt_reinit, opt_threshold_residual, opt_log)) {
386 if (opt_ipath.empty() && env_ipath.empty()) {
387 usage(argv[0],
nullptr, opt_warp_type, opt_tracker_type, opt_last_frame, opt_threshold_residual);
388 std::cerr << std::endl <<
"ERROR:" << std::endl;
389 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
390 <<
" environment variable to specify the location of the " << std::endl
391 <<
" image path where test images are located." << std::endl
398 if (!opt_ipath.empty())
404 ofs.open(logfilename.c_str());
417 std::cout <<
"Cannot open sequence: " << ipath << std::endl;
425#if defined(VISP_HAVE_X11)
427#elif defined(VISP_HAVE_GDI)
429#elif defined(HAVE_OPENCV_HIGHGUI)
431#elif defined(VISP_HAVE_D3D9)
433#elif defined(VISP_HAVE_GTK)
438#if defined(VISP_HAVE_DISPLAY)
439 display->
init(I, 100, 100,
"Test tracking");
446 switch (opt_warp_type) {
450 case WARP_HOMOGRAPHY:
453 case WARP_HOMOGRAPHY_SL3:
459 case WARP_TRANSLATION:
462#ifdef VISP_HAVE_MODULE_TT_MI
472 switch (opt_tracker_type) {
473 case TRACKER_SSD_ESM:
476 case TRACKER_SSD_FORWARD_ADDITIONAL:
479 case TRACKER_SSD_FORWARD_COMPOSITIONAL:
482 case TRACKER_SSD_INVERSE_COMPOSITIONAL:
485 case TRACKER_ZNCC_FORWARD_ADDITIONEL:
488 case TRACKER_ZNCC_INVERSE_COMPOSITIONAL:
491#ifdef VISP_HAVE_MODULE_TT_MI
495 case TRACKER_MI_FORWARD_ADDITIONAL:
498 case TRACKER_MI_FORWARD_COMPOSITIONAL:
501 case TRACKER_MI_INVERSE_COMPOSITIONAL:
511 tracker->setThresholdGradient(60.);
516 tracker->setThresholdResidualDifference(opt_threshold_residual);
517 bool delaunay =
false;
518 if (opt_display && opt_click_allowed)
519 tracker->initClick(I, delaunay);
521 std::vector<vpImagePoint> v_ip;
536 tracker->initFromPoints(I, v_ip,
false);
542 while (!reader.
end()) {
545 std::cout <<
"Process image number " << reader.
getFrameIndex() << std::endl;
552 ofs <<
tracker->getNbIteration() << std::endl;
557 std::cout <<
"re-init simulation" << std::endl;
558 if (opt_click_allowed)
563 if (opt_display && opt_click_allowed) {
566 tracker->initClick(I, delaunay);
569 std::vector<vpImagePoint> v_ip;
584 tracker->initFromPoints(I, v_ip,
false);
595 warp_->
warpZone(zoneRef_, p_, zoneWarped_);
605 std::cout <<
"Total time: " << t_end - t_init <<
" ms" << std::endl;
606 std::cout <<
"Total mean: " << (t_end - t_init) / niter <<
" ms" << std::endl;
609 std::cout <<
"Log are saved in: " << logfilename << std::endl;
613 if (opt_click_allowed) {
628 std::cout <<
"Catch an exception: " <<
e << std::endl;
634 std::cout <<
"Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
642 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...
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") VP_OVERRIDE
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
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)
Definition of the vpImage class member functions.
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)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void setLastFrameIndex(const long last_frame)
void open(vpImage< vpRGBa > &I) VP_OVERRIDE
void setFileName(const std::string &filename)
void setFirstFrameIndex(const long first_frame)
long getFrameIndex() const
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
VISP_EXPORT double measureTimeMs()