Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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)
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] << " [--name <video name>] [--tracker <1=egde|2=keypoint|3=hybrid>]"
28 << " [--help] [-h]\n"
29 << std::endl;
30 return EXIT_SUCCESS;
31 }
32 }
33
34 if (opt_tracker < 1 || opt_tracker > 3) {
35 std::cerr << "Wrong tracker type. Correct values are: "
36 "1=egde|2=keypoint|3=hybrid."
37 << std::endl;
38 return EXIT_SUCCESS;
39 }
40
41 std::string parentname = vpIoTools::getParent(opt_videoname);
42 std::string objectname = vpIoTools::getNameWE(opt_videoname);
43
44 if (!parentname.empty()) {
45 objectname = parentname + "/" + objectname;
46 }
47
48 std::cout << "Video name: " << opt_videoname << std::endl;
49 std::cout << "Tracker requested config files: " << objectname << ".[init, cao]" << std::endl;
50 std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
51
55
57 g.setFileName(opt_videoname);
58 g.open(I);
59
60#if defined(VISP_HAVE_X11)
61 vpDisplayX display;
62#elif defined(VISP_HAVE_GDI)
63 vpDisplayGDI display;
64#elif defined(HAVE_OPENCV_HIGHGUI)
65 vpDisplayOpenCV display;
66#endif
67 display.init(I, 100, 100, "Model-based tracker");
68
70 vpMbGenericTracker tracker(1, opt_tracker);
72
73#if !defined(VISP_HAVE_MODULE_KLT)
74 if (opt_tracker >= 2) {
75 std::cout << "KLT and hybrid model-based tracker are not available since visp_klt module is missing"
76 << std::endl;
77 return EXIT_SUCCESS;
78 }
79#endif
80
83 tracker.loadConfigFile(objectname + ".xml");
85#if 0
86 // Corresponding parameters manually set to have an example code
87 if (opt_tracker == 1 || opt_tracker == 3) {
88 vpMe me;
89 me.setMaskSize(5);
90 me.setMaskNumber(180);
91 me.setRange(8);
93 me.setThreshold(20);
94 me.setMu1(0.5);
95 me.setMu2(0.5);
96 me.setSampleStep(4);
97 tracker.setMovingEdge(me);
98 }
99
100#if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
101 if (opt_tracker == 2 || opt_tracker == 3) {
102 vpKltOpencv klt_settings;
103 tracker.setKltMaskBorder(5);
104 klt_settings.setMaxFeatures(300);
105 klt_settings.setWindowSize(5);
106 klt_settings.setQuality(0.015);
107 klt_settings.setMinDistance(8);
108 klt_settings.setHarrisFreeParameter(0.01);
109 klt_settings.setBlockSize(3);
110 klt_settings.setPyramidLevels(3);
111 tracker.setKltOpencv(klt_settings);
112 }
113#endif
114
115 {
118 cam.initPersProjWithoutDistortion(839.21470, 839.44555, 325.66776, 243.69727);
119 tracker.setCameraParameters(cam);
121 }
122#endif
124
126 tracker.loadModel(objectname + ".cao");
129 tracker.setDisplayFeatures(true);
132 tracker.initClick(I, objectname + ".init", true);
134
135 while (!g.end()) {
136 g.acquire(I);
139 tracker.track(I);
143 tracker.getPose(cMo);
147 tracker.getCameraParameters(cam);
148 tracker.display(I, cMo, cam, vpColor::red, 2);
150 vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
151 vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
153
154 if (vpDisplay::getClick(I, false)) {
155 break;
156 }
157 }
159 }
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:211
static const vpColor none
Definition vpColor.h:223
Display for windows using GDI (available on any windows 32 platform).
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:132
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(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), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
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.
Definition vpException.h:59
const char * what() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition vpImage.h:135
static std::string getNameWE(const std::string &pathname)
static std::string getParent(const std::string &pathname)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition vpKltOpencv.h:73
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:122
void setMu1(const double &mu_1)
Definition vpMe.h:353
void setSampleStep(const double &s)
Definition vpMe.h:390
void setRange(const unsigned int &r)
Definition vpMe.h:383
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition vpMe.h:445
void setMaskSize(const unsigned int &a)
Definition vpMe.cpp:452
void setMu2(const double &mu_2)
Definition vpMe.h:360
@ NORMALIZED_THRESHOLD
Easy-to-use normalized likelihood threshold corresponding to the minimal luminance contrast to consid...
Definition vpMe.h:132
void setMaskNumber(const unsigned int &a)
Definition vpMe.cpp:445
void setThreshold(const double &t)
Definition vpMe.h:435
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)