Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 }
virtual void setKltOpencv(const vpKltOpencv &t)
virtual void track(const vpImage< unsigned char > &I)
virtual void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:411
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setHarrisFreeParameter(double harris_k)
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:208
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:382
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)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
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
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1306
static void flush(const vpImage< unsigned char > &I)
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:479
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:508
void open(vpImage< vpRGBa > &I)
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 getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:201
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:371
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))
static void cleanup()
Definition: vpXmlParser.h:308
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:1293
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:76
virtual void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:333
void setBlockSize(const int blockSize)
virtual void setClipping(const unsigned int &flags)
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)