Visual Servoing Platform version 3.5.0
tutorial-mb-generic-tracker-stereo-mono.cpp
1
2#include <cstdlib>
3#include <visp3/core/vpConfig.h>
4#include <visp3/core/vpIoTools.h>
5#include <visp3/gui/vpDisplayGDI.h>
6#include <visp3/gui/vpDisplayOpenCV.h>
7#include <visp3/gui/vpDisplayX.h>
8#include <visp3/io/vpImageIo.h>
10#include <visp3/mbt/vpMbGenericTracker.h>
12#include <visp3/io/vpVideoReader.h>
13
14int main(int argc, char **argv)
15{
16#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020300)
17 try {
18 std::string opt_videoname = "teabox.mp4";
19 int opt_tracker = vpMbGenericTracker::EDGE_TRACKER;
20
21 for (int i = 0; i < argc; i++) {
22 if (std::string(argv[i]) == "--name" && i + 1 < argc)
23 opt_videoname = std::string(argv[i + 1]);
24 else if (std::string(argv[i]) == "--tracker" && i + 1 < argc)
25 opt_tracker = atoi(argv[i + 1]);
26 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
27 std::cout << "\nUsage: " << argv[0]
28 << " [--name <video name>] [--tracker <1=egde|2=keypoint|3=hybrid>]"
29 << " [--help] [-h]\n"
30 << std::endl;
31 return EXIT_SUCCESS;
32 }
33 }
34
35 if (opt_tracker < 1 || opt_tracker > 3) {
36 std::cerr << "Wrong tracker type. Correct values are: "
37 "1=egde|2=keypoint|3=hybrid."
38 << std::endl;
39 return EXIT_SUCCESS;
40 }
41
42 std::string parentname = vpIoTools::getParent(opt_videoname);
43 std::string objectname = vpIoTools::getNameWE(opt_videoname);
44
45 if (!parentname.empty()) {
46 objectname = parentname + "/" + objectname;
47 }
48
49 std::cout << "Video name: " << opt_videoname << std::endl;
50 std::cout << "Tracker requested config files: " << objectname << ".[init, cao]" << std::endl;
51 std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
52
56
58 g.setFileName(opt_videoname);
59 g.open(I);
60
61#if defined(VISP_HAVE_X11)
62 vpDisplayX display;
63#elif defined(VISP_HAVE_GDI)
64 vpDisplayGDI display;
65#else
66 vpDisplayOpenCV display;
67#endif
68 display.init(I, 100, 100, "Model-based tracker");
69
71 vpMbGenericTracker tracker(1, opt_tracker);
73
74#if !defined(VISP_HAVE_MODULE_KLT)
75 if (opt_tracker >= 2) {
76 std::cout << "KLT and hybrid model-based tracker are not available "
77 "since visp_klt module is missing"
78 << std::endl;
79 return EXIT_SUCCESS;
80 }
81#endif
82
85 tracker.loadConfigFile(objectname + ".xml");
87#if 0
88 // Corresponding parameters manually set to have an example code
89 if (opt_tracker == 1 || opt_tracker == 3) {
90 vpMe me;
91 me.setMaskSize(5);
92 me.setMaskNumber(180);
93 me.setRange(8);
94 me.setThreshold(10000);
95 me.setMu1(0.5);
96 me.setMu2(0.5);
97 me.setSampleStep(4);
98 tracker.setMovingEdge(me);
99 }
100
101#ifdef VISP_HAVE_MODULE_KLT
102 if (opt_tracker == 2 || opt_tracker == 3) {
103 vpKltOpencv klt_settings;
104 tracker.setKltMaskBorder(5);
105 klt_settings.setMaxFeatures(300);
106 klt_settings.setWindowSize(5);
107 klt_settings.setQuality(0.015);
108 klt_settings.setMinDistance(8);
109 klt_settings.setHarrisFreeParameter(0.01);
110 klt_settings.setBlockSize(3);
111 klt_settings.setPyramidLevels(3);
112 tracker.setKltOpencv(klt_settings);
113 }
114#endif
115
116 {
119 cam.initPersProjWithoutDistortion(839.21470, 839.44555, 325.66776, 243.69727);
120 tracker.setCameraParameters(cam);
122 }
123#endif
125
127 tracker.loadModel(objectname + ".cao");
130 tracker.setDisplayFeatures(true);
133 tracker.initClick(I, objectname + ".init", true);
135
136 while (!g.end()) {
137 g.acquire(I);
140 tracker.track(I);
144 tracker.getPose(cMo);
148 tracker.getCameraParameters(cam);
149 tracker.display(I, cMo, cam, vpColor::red, 2);
151 vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
152 vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
154
155 if (vpDisplay::getClick(I, false)) {
156 break;
157 }
158 }
160 } catch (const vpException &e) {
161 std::cerr << "Catch a ViSP exception: " << e.what() << std::endl;
162 }
163
164 return EXIT_SUCCESS;
165#else
166 (void)argc;
167 (void)argv;
168 std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
169 return EXIT_SUCCESS;
170#endif
171}
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
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...
Definition: vpDisplayX.h:135
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 displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0))
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emited by ViSP classes.
Definition: vpException.h:72
const char * what() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1532
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1606
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:79
void setBlockSize(int blockSize)
void setQuality(double qualityLevel)
void setHarrisFreeParameter(double harris_k)
void setMaxFeatures(int maxCount)
void setMinDistance(double minDistance)
void setWindowSize(int winSize)
void setPyramidLevels(int pyrMaxLevel)
Real-time 6D object pose tracking using its CAD model.
Definition: vpMe.h:61
void setMu1(const double &mu_1)
Definition: vpMe.h:241
void setSampleStep(const double &s)
Definition: vpMe.h:278
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:459
void setMu2(const double &mu_2)
Definition: vpMe.h:248
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:452
void setThreshold(const double &t)
Definition: vpMe.h:300
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 open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)