Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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.mpg";
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") {
21  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help]\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 #ifdef VISP_HAVE_XML2
34  << "xml,"
35 #endif
36  << "cao or wrl]" << std::endl;
37  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
38 
42 
43  vpVideoReader g;
44  g.setFileName(videoname);
45  g.open(I);
46 
47 #if defined(VISP_HAVE_X11)
48  vpDisplayX display(I, 100, 100, "Model-based hybrid tracker");
49  ;
50 #elif defined(VISP_HAVE_GDI)
51  vpDisplayGDI display(I, 100, 100, "Model-based hybrid tracker");
52  ;
53 #elif defined(VISP_HAVE_OPENCV)
54  vpDisplayOpenCV display(I, 100, 100, "Model-based hybrid tracker");
55  ;
56 #else
57  std::cout << "No image viewer is available..." << std::endl;
58 #endif
59 
60  vpMbEdgeKltTracker tracker;
61  bool usexml = false;
62 #ifdef VISP_HAVE_XML2
63  if (vpIoTools::checkFilename(objectname + ".xml")) {
64  tracker.loadConfigFile(objectname + ".xml");
65  usexml = true;
66  }
67 #endif
68  if (!usexml) {
69  vpMe me;
70  me.setMaskSize(5);
71  me.setMaskNumber(180);
72  me.setRange(8);
73  me.setThreshold(10000);
74  me.setMu1(0.5);
75  me.setMu2(0.5);
76  me.setSampleStep(4);
77  tracker.setMovingEdge(me);
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.setMaskBorder(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);
95  }
96  tracker.setOgreVisibilityTest(true);
97  tracker.setOgreShowConfigDialog(false);
98  tracker.loadModel(objectname + ".cao");
99  tracker.setDisplayFeatures(true);
100  tracker.initClick(I, objectname + ".init", true);
101 
102  while (!g.end()) {
103  g.acquire(I);
105  tracker.track(I);
106  tracker.getPose(cMo);
107  tracker.getCameraParameters(cam);
108  tracker.display(I, cMo, cam, vpColor::red, 2, true);
109  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
110  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
111  vpDisplay::flush(I);
112 
113  if (vpDisplay::getClick(I, false))
114  break;
115  }
117 
118 #ifdef VISP_HAVE_XML2
120 #endif
121 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION >= 2)
122  SoDB::finish();
123 #endif
124  } catch (const vpException &e) {
125  std::cout << "Catch a ViSP exception: " << e << std::endl;
126  }
127 #ifdef VISP_HAVE_OGRE
128  catch (Ogre::Exception &e) {
129  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
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 }
virtual void setKltOpencv(const vpKltOpencv &t)
virtual void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:503
virtual void setOgreShowConfigDialog(const bool showConfigDialog)
Definition: vpMbTracker.h:629
void setMovingEdge(const vpMe &me)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setHarrisFreeParameter(double harris_k)
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:256
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:466
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:454
virtual void loadConfigFile(const std::string &configFile)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
virtual void loadModel(const std::string &modelFile, const bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
void setMaxFeatures(const int maxCount)
void setSampleStep(const double &s)
Definition: vpMe.h:278
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
static const vpColor none
Definition: vpColor.h:192
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:71
Definition: vpMe.h:60
virtual void setClipping(const unsigned int &flags)
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1477
static void flush(const vpImage< unsigned char > &I)
virtual void setFarClippingDistance(const double &dist)
void setMu1(const double &mu_1)
Definition: vpMe.h:241
static const vpColor red
Definition: vpColor.h:180
void setQuality(double qualityLevel)
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:675
void open(vpImage< vpRGBa > &I)
virtual void setOgreVisibilityTest(const bool &v)
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:461
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
virtual void track(const vpImage< unsigned char > &I)
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:248
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, const unsigned int thickness=1, const bool displayFullModel=false)
Generic class defining intrinsic camera parameters.
void acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:455
void setPyramidLevels(const int pyrMaxLevel)
static double rad(double deg)
Definition: vpMath.h:102
virtual void setCameraParameters(const vpCameraParameters &cam)
Hybrid tracker based on moving-edges and keypoints tracked using KLT tracker.
static void cleanup()
Definition: vpXmlParser.h:310
void setMu2(const double &mu_2)
Definition: vpMe.h:248
void setWindowSize(const int winSize)
void setMaskBorder(const unsigned int &e)
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 std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1464
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:78
virtual void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:414
void setBlockSize(const int blockSize)
void setThreshold(const double &t)
Definition: vpMe.h:300
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, const bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
void setRange(const unsigned int &r)
Definition: vpMe.h:271
virtual void setNearClippingDistance(const double &dist)