Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-mb-klt-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/vpMbKltTracker.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_PUGIXML
34  << "xml,"
35 #endif
36  << "cao or wrl]" << std::endl;
37  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
41 
42  vpVideoReader g;
43  g.setFileName(videoname);
44  g.open(I);
45 
46 #if defined(VISP_HAVE_X11)
47  vpDisplayX display;
48 #elif defined(VISP_HAVE_GDI)
49  vpDisplayGDI display;
50 #elif defined(VISP_HAVE_OPENCV)
51  vpDisplayOpenCV display;
52 #else
53  std::cout << "No image viewer is available..." << std::endl;
54  return 0;
55 #endif
56 
57  display.init(I, 100, 100, "Model-based keypoint tracker");
58 
59  vpMbKltTracker tracker;
60  bool usexml = false;
62 #ifdef VISP_HAVE_PUGIXML
63  if (vpIoTools::checkFilename(objectname + ".xml")) {
64  tracker.loadConfigFile(objectname + ".xml");
65  usexml = true;
66  }
67 #endif
68  if (!usexml) {
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.setKltMaskBorder(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);
89  }
90  tracker.setOgreVisibilityTest(true);
91  tracker.setOgreShowConfigDialog(false);
92  tracker.loadModel(objectname + "-triangle.cao");
93  tracker.setDisplayFeatures(true);
94  tracker.initClick(I, objectname + ".init", true);
95 
96  while (!g.end()) {
97  g.acquire(I);
99  tracker.track(I);
100  tracker.getPose(cMo);
101  tracker.getCameraParameters(cam);
102  tracker.display(I, cMo, cam, vpColor::red, 2);
103  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
104  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
105  vpDisplay::flush(I);
106 
107  if (vpDisplay::getClick(I, false))
108  break;
109  }
111 
112 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION >= 2)
113  SoDB::finish();
114 #endif
115  } catch (const vpException &e) {
116  std::cout << "Catch a ViSP exception: " << e << std::endl;
117  }
118 #ifdef VISP_HAVE_OGRE
119  catch (Ogre::Exception &e) {
120  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
121  }
122 #endif
123 #else
124  (void)argc;
125  (void)argv;
126  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
127 #endif
128 }
virtual void setKltOpencv(const vpKltOpencv &t)
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:252
virtual void track(const vpImage< unsigned char > &I)
void setMaxFeatures(int maxCount)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setHarrisFreeParameter(double harris_k)
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:476
Implementation of an homogeneous matrix and operations on such kind of matrices.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
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:191
virtual void setOgreShowConfigDialog(bool showConfigDialog)
Definition: vpMbTracker.h:639
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
void setKltMaskBorder(const unsigned int &e)
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1473
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:179
virtual void loadConfigFile(const std::string &configFile)
void setQuality(double qualityLevel)
void open(vpImage< vpRGBa > &I)
virtual void getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:410
void setPyramidLevels(int pyrMaxLevel)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void acquire(vpImage< vpRGBa > &I)
void setWindowSize(int winSize)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:465
Model based tracker using only KLT.
static double rad(double deg)
Definition: vpMath.h:108
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
void setCameraParameters(const vpCameraParameters &cam)
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))
virtual void setOgreVisibilityTest(const bool &v)
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1460
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:78
void setFileName(const std::string &filename)
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:730
virtual void setClipping(const unsigned int &flags)
virtual void setFarClippingDistance(const double &dist)
void setBlockSize(int blockSize)
virtual void setDisplayFeatures(bool displayF)
Definition: vpMbTracker.h:513
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:244
virtual void setNearClippingDistance(const double &dist)