Visual Servoing Platform  version 3.0.0
tutorial-mb-klt-tracker.cpp
1 
2 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayX.h>
5 #include <visp3/io/vpImageIo.h>
6 #include <visp3/core/vpIoTools.h>
7 #include <visp3/mbt/vpMbKltTracker.h>
8 #include <visp3/io/vpVideoReader.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
33  << ".[init,"
34 #ifdef VISP_HAVE_XML2
35  << "xml,"
36 #endif
37  << "cao or wrl]" << std::endl;
38  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
42 
43  vpVideoReader g;
44  g.setFileName(videoname);
45  g.open(I);
46 
47 #if defined(VISP_HAVE_X11)
48  vpDisplayX display;
49 #elif defined(VISP_HAVE_GDI)
50  vpDisplayGDI display;
51 #elif defined(VISP_HAVE_OPENCV)
52  vpDisplayOpenCV display;
53 #else
54  std::cout << "No image viewer is available..." << std::endl;
55  return 0;
56 #endif
57 
58  display.init(I, 100, 100,"Model-based keypoint tracker");
59 
60  vpMbKltTracker tracker;
61  bool usexml = false;
63 #ifdef VISP_HAVE_XML2
64  if(vpIoTools::checkFilename(objectname + ".xml")) {
65  tracker.loadConfigFile(objectname + ".xml");
66  usexml = true;
67  }
68 #endif
69  if (! usexml) {
72  tracker.setMaskBorder(5);
73  vpKltOpencv klt_settings;
74  klt_settings.setMaxFeatures(300);
75  klt_settings.setWindowSize(5);
76  klt_settings.setQuality(0.015);
77  klt_settings.setMinDistance(8);
78  klt_settings.setHarrisFreeParameter(0.01);
79  klt_settings.setBlockSize(3);
80  klt_settings.setPyramidLevels(3);
81  tracker.setKltOpencv(klt_settings);
82  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
83  tracker.setCameraParameters(cam);
84  tracker.setAngleAppear( vpMath::rad(70) );
85  tracker.setAngleDisappear( vpMath::rad(80) );
86  tracker.setNearClippingDistance(0.1);
87  tracker.setFarClippingDistance(100.0);
90  }
91  tracker.setOgreVisibilityTest(true);
92  tracker.setOgreShowConfigDialog(false);
93  tracker.loadModel(objectname + "-triangle.cao");
94  tracker.setDisplayFeatures(true);
95  tracker.initClick(I, objectname + ".init", true);
96 
97  while(! g.end()){
98  g.acquire(I);
100  tracker.track(I);
101  tracker.getPose(cMo);
102  tracker.getCameraParameters(cam);
103  tracker.display(I, cMo, cam, vpColor::red, 2, true);
104  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
105  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
106  vpDisplay::flush(I);
107 
108  if (vpDisplay::getClick(I, false))
109  break;
110  }
112 
113 #ifdef VISP_HAVE_XML2
115 #endif
116 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION == 3)
117  SoDB::finish();
118 #endif
119  }
120  catch(vpException e) {
121  std::cout << "Catch a ViSP exception: " << e << std::endl;
122  }
123 #ifdef VISP_HAVE_OGRE
124  catch(Ogre::Exception e) {
125  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
126  }
127 #endif
128 #else
129  (void)argc;
130  (void)argv;
131  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
132 #endif
133 }
void setKltOpencv(const vpKltOpencv &t)
virtual void track(const vpImage< unsigned char > &I)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setHarrisFreeParameter(double harris_k)
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:232
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:430
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
void setMaxFeatures(const int maxCount)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:888
Define the X11 console to display images.
Definition: vpDisplayX.h:148
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:175
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:73
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1240
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static const vpColor red
Definition: vpColor.h:163
virtual void loadConfigFile(const std::string &configFile)
void setQuality(double qualityLevel)
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
void setOgreShowConfigDialog(const bool showConfigDialog)
Definition: vpMbTracker.h:523
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:485
void open(vpImage< vpRGBa > &I)
void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:356
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:225
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:419
Model based tracker using only KLT.
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, const bool displayHelp=false)
void setPyramidLevels(const int pyrMaxLevel)
static double rad(double deg)
Definition: vpMath.h:104
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, vpImagePoint offset=vpImagePoint(0, 0))
Definition: vpDisplay.cpp:373
static void cleanup()
Definition: vpXmlParser.h:216
void setWindowSize(const int winSize)
void setMaskBorder(const unsigned int &e)
virtual void loadModel(const char *modelFile, const bool verbose=false)
void setCameraParameters(const vpCameraParameters &cam)
virtual void setOgreVisibilityTest(const bool &v)
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1227
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
Definition: vpKltOpencv.h:75
void setBlockSize(const int blockSize)
virtual void setClipping(const unsigned int &flags)
virtual bool getClick(bool blocking=true)=0
void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:459
virtual void setFarClippingDistance(const double &dist)
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)
virtual void setNearClippingDistance(const double &dist)