Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
tutorial-mb-tracker-full.cpp
1
2#include <visp3/core/vpIoTools.h>
3#include <visp3/gui/vpDisplayGDI.h>
4#include <visp3/gui/vpDisplayOpenCV.h>
5#include <visp3/gui/vpDisplayX.h>
6#include <visp3/io/vpImageIo.h>
8#include <visp3/mbt/vpMbEdgeKltTracker.h>
9#include <visp3/mbt/vpMbEdgeTracker.h>
11#include <visp3/io/vpVideoReader.h>
12
13int main(int argc, char **argv)
14{
15#if defined(VISP_HAVE_OPENCV)
16
17 try {
18 std::string opt_videoname = "teabox.mp4";
19 std::string opt_modelname = "teabox.cao";
20 int opt_tracker = 0;
21
22 for (int i = 0; i < argc; i++) {
23 if (std::string(argv[i]) == "--video")
24 opt_videoname = std::string(argv[i + 1]);
25 else if (std::string(argv[i]) == "--model")
26 opt_modelname = std::string(argv[i + 1]);
27 else if (std::string(argv[i]) == "--tracker")
28 opt_tracker = atoi(argv[i + 1]);
29 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
30 std::cout << "\nUsage: " << argv[0]
31 << " [--video <video name>] [--model <model name>] "
32 "[--tracker <0=egde|1=keypoint|2=hybrid>] [--help] [-h]\n"
33 << std::endl;
34 return EXIT_SUCCESS;
35 }
36 }
37 std::string parentname = vpIoTools::getParent(opt_modelname);
38 std::string objectname = vpIoTools::getNameWE(opt_modelname);
39
40 if (!parentname.empty())
41 objectname = parentname + "/" + objectname;
42
43 std::cout << "Video name: " << opt_videoname << std::endl;
44 std::cout << "Tracker requested config files: " << objectname << ".[init,"
45 << "xml,"
46 << "cao or wrl]" << std::endl;
47 std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
48
56
58 g.setFileName(opt_videoname);
59 g.open(I);
60
61 vpDisplay *display = NULL;
62#if defined(VISP_HAVE_X11)
63 display = new vpDisplayX;
64#elif defined(VISP_HAVE_GDI)
65 display = new vpDisplayGDI;
66#elif defined(HAVE_OPENCV_HIGHGUI)
67 display = new vpDisplayOpenCV;
68#endif
69 display->init(I, 100, 100, "Model-based tracker");
70
72 vpMbTracker *tracker;
73 if (opt_tracker == 0)
74 tracker = new vpMbEdgeTracker;
75#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
76 else if (opt_tracker == 1)
77 tracker = new vpMbKltTracker;
78 else
79 tracker = new vpMbEdgeKltTracker;
80#else
81 else {
82 std::cout << "klt and hybrid model-based tracker are not available "
83 "since visp_klt module is missing"
84 << std::endl;
85 return EXIT_FAILURE;
86 }
87#endif
89
90 bool usexml = false;
92 if (vpIoTools::checkFilename(objectname + ".xml")) {
93 tracker->loadConfigFile(objectname + ".xml");
94 usexml = true;
95 }
97
98 if (!usexml) {
100 if (opt_tracker == 0 || opt_tracker == 2) {
102 vpMe me;
103 me.setMaskSize(5);
104 me.setMaskNumber(180);
105 me.setRange(8);
107 me.setThreshold(20);
108 me.setMu1(0.5);
109 me.setMu2(0.5);
110 me.setSampleStep(4);
111 dynamic_cast<vpMbEdgeTracker *>(tracker)->setMovingEdge(me);
113 }
114
115#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
116 if (opt_tracker == 1 || opt_tracker == 2) {
118 vpKltOpencv klt_settings;
119 klt_settings.setMaxFeatures(300);
120 klt_settings.setWindowSize(5);
121 klt_settings.setQuality(0.015);
122 klt_settings.setMinDistance(8);
123 klt_settings.setHarrisFreeParameter(0.01);
124 klt_settings.setBlockSize(3);
125 klt_settings.setPyramidLevels(3);
126 dynamic_cast<vpMbKltTracker *>(tracker)->setKltOpencv(klt_settings);
127 dynamic_cast<vpMbKltTracker *>(tracker)->setKltMaskBorder(5);
129 }
130#endif
131
133 cam.initPersProjWithoutDistortion(839.21470, 839.44555, 325.66776, 243.69727);
134 tracker->setCameraParameters(cam);
136
138 tracker->setAngleAppear(vpMath::rad(70));
139 tracker->setAngleDisappear(vpMath::rad(80));
142 tracker->setNearClippingDistance(0.1);
143 tracker->setFarClippingDistance(100.0);
149 }
152 tracker->setOgreVisibilityTest(false);
153 tracker->setOgreShowConfigDialog(false);
156 tracker->setScanLineVisibilityTest(true);
159
161 if (vpIoTools::checkFilename(objectname + ".cao"))
162 tracker->loadModel(objectname + ".cao");
165 else if (vpIoTools::checkFilename(objectname + ".wrl"))
166 tracker->loadModel(objectname + ".wrl");
169 tracker->setDisplayFeatures(true);
172 tracker->initClick(I, objectname + ".init", true);
174
175 while (!g.end()) {
176 g.acquire(I);
179 tracker->track(I);
182 tracker->getPose(cMo);
185 tracker->getCameraParameters(cam);
186 tracker->display(I, cMo, cam, vpColor::red, 2);
188 vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
189 vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
191
192 if (vpDisplay::getClick(I, false))
193 break;
194 }
197 delete display;
198 delete tracker;
200 }
201 catch (const vpException &e) {
202 std::cout << "Catch a ViSP exception: " << e << std::endl;
203 }
204#ifdef VISP_HAVE_OGRE
205 catch (Ogre::Exception &e) {
206 std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
207 }
208#endif
209#else
210 (void)argc;
211 (void)argv;
212 std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
213#endif
214}
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
Class that defines generic functionalities for display.
Definition vpDisplay.h:173
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
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition vpImage.h:135
static bool checkFilename(const std::string &filename)
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)
static double rad(double deg)
Definition vpMath.h:116
Hybrid tracker based on moving-edges and keypoints tracked using KLT tracker.
Make the complete tracking of an object by using its CAD model.
Model based tracker using only KLT.
Main methods for a model-based tracker.
virtual void track(const vpImage< unsigned char > &I)=0
virtual void setOgreShowConfigDialog(bool showConfigDialog)
virtual void getCameraParameters(vpCameraParameters &cam) const
virtual void setDisplayFeatures(bool displayF)
virtual void getPose(vpHomogeneousMatrix &cMo) const
virtual void setCameraParameters(const vpCameraParameters &cam)
virtual void setAngleDisappear(const double &a)
virtual void setScanLineVisibilityTest(const bool &v)
virtual void setOgreVisibilityTest(const bool &v)
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setNearClippingDistance(const double &dist)
virtual void setFarClippingDistance(const double &dist)
virtual void setClipping(const unsigned int &flags)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)=0
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setAngleAppear(const double &a)
virtual unsigned int getClipping() const
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
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)