Visual Servoing Platform  version 3.5.1 under development (2022-07-03)
tutorial-mb-hybrid-tracker.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>
7 #include <visp3/io/vpVideoReader.h>
8 #include <visp3/mbt/vpMbEdgeKltTracker.h>
9 
10 int main(int argc, char **argv)
11 {
12 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
13 
14  try {
15  std::string videoname = "teabox.mp4";
16 
17  for (int i = 0; i < argc; i++) {
18  if (std::string(argv[i]) == "--name")
19  videoname = std::string(argv[i + 1]);
20  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
21  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help] [-h]\n" << std::endl;
22  return 0;
23  }
24  }
25  std::string parentname = vpIoTools::getParent(videoname);
26  std::string objectname = vpIoTools::getNameWE(videoname);
27 
28  if (!parentname.empty())
29  objectname = parentname + "/" + objectname;
30 
31  std::cout << "Video name: " << videoname << std::endl;
32  std::cout << "Tracker requested config files: " << objectname << ".[init,"
33  << "xml,"
34  << "cao or wrl]" << std::endl;
35  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
36 
40 
41  vpVideoReader g;
42  g.setFileName(videoname);
43  g.open(I);
44 
45 #if defined(VISP_HAVE_X11)
46  vpDisplayX display(I, 100, 100, "Model-based hybrid tracker");
47 #elif defined(VISP_HAVE_GDI)
48  vpDisplayGDI display(I, 100, 100, "Model-based hybrid tracker");
49 #elif defined(VISP_HAVE_OPENCV)
50  vpDisplayOpenCV display(I, 100, 100, "Model-based hybrid tracker");
51 #else
52  std::cout << "No image viewer is available..." << std::endl;
53 #endif
54 
55  vpMbEdgeKltTracker tracker;
56  bool usexml = false;
57  if (vpIoTools::checkFilename(objectname + ".xml")) {
58  tracker.loadConfigFile(objectname + ".xml");
59  usexml = true;
60  }
61  if (!usexml) {
62  vpMe me;
63  me.setMaskSize(5);
64  me.setMaskNumber(180);
65  me.setRange(8);
66  me.setThreshold(10000);
67  me.setMu1(0.5);
68  me.setMu2(0.5);
69  me.setSampleStep(4);
70  tracker.setMovingEdge(me);
71  vpKltOpencv klt_settings;
72  klt_settings.setMaxFeatures(300);
73  klt_settings.setWindowSize(5);
74  klt_settings.setQuality(0.015);
75  klt_settings.setMinDistance(8);
76  klt_settings.setHarrisFreeParameter(0.01);
77  klt_settings.setBlockSize(3);
78  klt_settings.setPyramidLevels(3);
79  tracker.setKltOpencv(klt_settings);
80  tracker.setMaskBorder(5);
81  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
82  tracker.setCameraParameters(cam);
83  tracker.setAngleAppear(vpMath::rad(70));
84  tracker.setAngleDisappear(vpMath::rad(80));
85  tracker.setNearClippingDistance(0.1);
86  tracker.setFarClippingDistance(100.0);
88  }
89  tracker.setOgreVisibilityTest(true);
90  tracker.setOgreShowConfigDialog(false);
91  tracker.loadModel(objectname + ".cao");
92  tracker.setDisplayFeatures(true);
93  tracker.initClick(I, objectname + ".init", true);
94 
95  while (!g.end()) {
96  g.acquire(I);
98  tracker.track(I);
99  tracker.getPose(cMo);
100  tracker.getCameraParameters(cam);
101  tracker.display(I, cMo, cam, vpColor::red, 2);
102  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
103  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
104  vpDisplay::flush(I);
105 
106  if (vpDisplay::getClick(I, false))
107  break;
108  }
110  } catch (const vpException &e) {
111  std::cout << "Catch a ViSP exception: " << e << std::endl;
112  }
113 #ifdef VISP_HAVE_OGRE
114  catch (Ogre::Exception &e) {
115  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
116  }
117 #endif
118 #else
119  (void)argc;
120  (void)argv;
121  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
122 #endif
123 }
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
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:800
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1536
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1620
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)
static double rad(double deg)
Definition: vpMath.h:117
Hybrid tracker based on moving-edges and keypoints tracked using KLT tracker.
virtual void setClipping(const unsigned int &flags)
virtual void setFarClippingDistance(const double &dist)
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
virtual void setNearClippingDistance(const double &dist)
virtual void setCameraParameters(const vpCameraParameters &cam)
virtual void setOgreVisibilityTest(const bool &v)
virtual void track(const vpImage< unsigned char > &I)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
void setMovingEdge(const vpMe &me)
void setMaskBorder(const unsigned int &e)
virtual void setKltOpencv(const vpKltOpencv &t)
virtual void setOgreShowConfigDialog(bool showConfigDialog)
Definition: vpMbTracker.h:647
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:248
virtual void setDisplayFeatures(bool displayF)
Definition: vpMbTracker.h:518
virtual void getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:414
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:481
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:470
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:256
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)