Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-mb-hybrid-tracker.cpp
1
2#include <visp3/core/vpConfig.h>
3#include <visp3/core/vpIoTools.h>
4#include <visp3/gui/vpDisplayFactory.h>
5#include <visp3/io/vpImageIo.h>
6#include <visp3/io/vpVideoReader.h>
7#include <visp3/mbt/vpMbEdgeKltTracker.h>
8
9int main(int argc, char **argv)
10{
11#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
12#ifdef ENABLE_VISP_NAMESPACE
13 using namespace VISP_NAMESPACE_NAME;
14#endif
15
16#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
17 std::shared_ptr<vpDisplay> display;
18#else
19 vpDisplay *display = nullptr;
20#endif
21
22 try {
23 std::string videoname = "teabox.mp4";
24
25 for (int i = 1; i < argc; i++) {
26 if (std::string(argv[i]) == "--name" && i + 1 < argc) {
27 videoname = std::string(argv[++i]);
28 }
29 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
30 std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help] [-h]\n" << std::endl;
31 return EXIT_SUCCESS;
32 }
33 }
34 std::string parentname = vpIoTools::getParent(videoname);
35 std::string objectname = vpIoTools::getNameWE(videoname);
36
37 if (!parentname.empty())
38 objectname = parentname + "/" + objectname;
39
40 std::cout << "Video name: " << videoname << std::endl;
41 std::cout << "Tracker requested config files: " << objectname << ".[init,"
42 << "xml,"
43 << "cao or wrl]" << std::endl;
44 std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
45
49
51 g.setFileName(videoname);
52 g.open(I);
53
54#if defined(VISP_HAVE_DISPLAY)
55#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
56 display = vpDisplayFactory::createDisplay(I, 100, 100, "Model-based hybrid tracker");
57#else
58 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Model-based hybrid tracker");
59#endif
60#else
61 std::cout << "No image viewer is available..." << std::endl;
62#endif
63
65 bool usexml = false;
66#if defined(VISP_HAVE_PUGIXML)
67 if (vpIoTools::checkFilename(objectname + ".xml")) {
68 tracker.loadConfigFile(objectname + ".xml");
69 usexml = true;
70 }
71#endif
72 if (!usexml) {
73 vpMe me;
74 me.setMaskSize(5);
75 me.setMaskNumber(180);
76 me.setRange(8);
78 me.setThreshold(20);
79 me.setMu1(0.5);
80 me.setMu2(0.5);
81 me.setSampleStep(4);
82 tracker.setMovingEdge(me);
83 vpKltOpencv klt_settings;
84 klt_settings.setMaxFeatures(300);
85 klt_settings.setWindowSize(5);
86 klt_settings.setQuality(0.015);
87 klt_settings.setMinDistance(8);
88 klt_settings.setHarrisFreeParameter(0.01);
89 klt_settings.setBlockSize(3);
90 klt_settings.setPyramidLevels(3);
91 tracker.setKltOpencv(klt_settings);
92 tracker.setMaskBorder(5);
93 cam.initPersProjWithoutDistortion(839, 839, 325, 243);
94 tracker.setCameraParameters(cam);
95 tracker.setAngleAppear(vpMath::rad(70));
96 tracker.setAngleDisappear(vpMath::rad(80));
97 tracker.setNearClippingDistance(0.1);
98 tracker.setFarClippingDistance(100.0);
99 tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
100 }
101 tracker.setOgreVisibilityTest(true);
102 tracker.setOgreShowConfigDialog(false);
103 tracker.loadModel(objectname + ".cao");
104 tracker.setDisplayFeatures(true);
105 tracker.initClick(I, objectname + ".init", true);
106
107 while (!g.end()) {
108 g.acquire(I);
110 tracker.track(I);
111 tracker.getPose(cMo);
112 tracker.getCameraParameters(cam);
113 tracker.display(I, cMo, cam, vpColor::red, 2);
114 vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
115 vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
117
118 if (vpDisplay::getClick(I, false))
119 break;
120 }
122 }
123 catch (const vpException &e) {
124 std::cout << "Catch a ViSP exception: " << e << std::endl;
125 }
126#ifdef VISP_HAVE_OGRE
127 catch (Ogre::Exception &e) {
128 std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
129 }
130#endif
131#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
132 if (display != nullptr) {
133 delete display;
134 }
135#endif
136#else
137 (void)argc;
138 (void)argv;
139 std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
140#endif
141}
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition vpColor.h:198
static const vpColor none
Definition vpColor.h:210
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition vpImage.h:131
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:83
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:129
Hybrid tracker based on moving-edges and keypoints tracked using KLT tracker.
Definition vpMe.h:143
void setMu1(const double &mu_1)
Definition vpMe.h:408
void setRange(const unsigned int &range)
Definition vpMe.h:438
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition vpMe.h:531
void setMaskNumber(const unsigned int &mask_number)
Definition vpMe.cpp:555
void setThreshold(const double &threshold)
Definition vpMe.h:489
void setSampleStep(const double &sample_step)
Definition vpMe.h:445
void setMaskSize(const unsigned int &mask_size)
Definition vpMe.cpp:563
void setMu2(const double &mu_2)
Definition vpMe.h:415
@ NORMALIZED_THRESHOLD
Definition vpMe.h:154
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void open(vpImage< vpRGBa > &I) VP_OVERRIDE
void setFileName(const std::string &filename)
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.