Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-mb-klt-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/vpMbKltTracker.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") {
30 std::cout << "\nUsage: " << argv[0]
31 << " [--name <video name>]"
32 << " [--help]\n" << std::endl;
33 return EXIT_SUCCESS;
34 }
35 }
36 std::string parentname = vpIoTools::getParent(videoname);
37 std::string objectname = vpIoTools::getNameWE(videoname);
38
39 if (!parentname.empty())
40 objectname = parentname + "/" + objectname;
41
42 std::cout << "Video name: " << videoname << std::endl;
43 std::cout << "Tracker requested config files: " << objectname << ".[init,"
44 << "xml,"
45 << "cao or wrl]" << std::endl;
46 std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
50
52 g.setFileName(videoname);
53 g.open(I);
54
55#if defined(VISP_HAVE_DISPLAY)
56#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
57 display = vpDisplayFactory::createDisplay(I, 100, 100, "Model-based keypoint tracker");
58#else
59 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Model-based keypoint tracker");
60#endif
61#else
62 std::cout << "No image viewer is available..." << std::endl;
63 return EXIT_FAILURE;
64#endif
65
67 bool usexml = false;
69#if defined(VISP_HAVE_PUGIXML)
70 if (vpIoTools::checkFilename(objectname + ".xml")) {
71 tracker.loadConfigFile(objectname + ".xml");
72 usexml = true;
73 }
74#endif
76 if (!usexml) {
78 vpKltOpencv klt_settings;
79 klt_settings.setMaxFeatures(300);
80 klt_settings.setWindowSize(5);
81 klt_settings.setQuality(0.015);
82 klt_settings.setMinDistance(8);
83 klt_settings.setHarrisFreeParameter(0.01);
84 klt_settings.setBlockSize(3);
85 klt_settings.setPyramidLevels(3);
86 tracker.setKltOpencv(klt_settings);
87 tracker.setKltMaskBorder(5);
88 cam.initPersProjWithoutDistortion(839, 839, 325, 243);
89 tracker.setCameraParameters(cam);
90 tracker.setAngleAppear(vpMath::rad(70));
91 tracker.setAngleDisappear(vpMath::rad(80));
92 tracker.setNearClippingDistance(0.1);
93 tracker.setFarClippingDistance(100.0);
94 tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
96 }
97 tracker.setOgreVisibilityTest(true);
98 tracker.setOgreShowConfigDialog(false);
99 tracker.loadModel(objectname + "-triangle.cao");
100 tracker.setDisplayFeatures(true);
101 tracker.initClick(I, objectname + ".init", true);
102
103 while (!g.end()) {
104 g.acquire(I);
106 tracker.track(I);
107 tracker.getPose(cMo);
108 tracker.getCameraParameters(cam);
109 tracker.display(I, cMo, cam, vpColor::red, 2);
110 vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
111 vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
113
114 if (vpDisplay::getClick(I, false))
115 break;
116 }
118 }
119 catch (const vpException &e) {
120 std::cout << "Catch a ViSP exception: " << e << std::endl;
121 }
122#ifdef VISP_HAVE_OGRE
123 catch (Ogre::Exception &e) {
124 std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
125 }
126#endif
127#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
128 if (display != nullptr) {
129 delete display;
130 }
131#endif
132#else
133 (void)argc;
134 (void)argv;
135 std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
136#endif
137}
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
Model based tracker using only KLT.
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.