Visual Servoing Platform  version 3.0.0
tutorial-mb-hybrid-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/vpMbEdgeKltTracker.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;
39 
43 
44  vpVideoReader g;
45  g.setFileName(videoname);
46  g.open(I);
47 
48 #if defined(VISP_HAVE_X11)
49  vpDisplayX display(I,100,100,"Model-based hybrid tracker");;
50 #elif defined(VISP_HAVE_GDI)
51  vpDisplayGDI display(I,100,100,"Model-based hybrid tracker");;
52 #elif defined(VISP_HAVE_OPENCV)
53  vpDisplayOpenCV display(I,100,100,"Model-based hybrid tracker");;
54 #else
55  std::cout << "No image viewer is available..." << std::endl;
56 #endif
57 
58  vpMbEdgeKltTracker tracker;
59  bool usexml = false;
60 #ifdef VISP_HAVE_XML2
61  if(vpIoTools::checkFilename(objectname + ".xml")) {
62  tracker.loadConfigFile(objectname + ".xml");
63  usexml = true;
64  }
65 #endif
66  if (! usexml) {
67  vpMe me;
68  me.setMaskSize(5);
69  me.setMaskNumber(180);
70  me.setRange(8);
71  me.setThreshold(10000);
72  me.setMu1(0.5);
73  me.setMu2(0.5);
74  me.setSampleStep(4);
75  tracker.setMovingEdge(me);
76  vpKltOpencv klt_settings;
77  klt_settings.setMaxFeatures(300);
78  klt_settings.setWindowSize(5);
79  klt_settings.setQuality(0.015);
80  klt_settings.setMinDistance(8);
81  klt_settings.setHarrisFreeParameter(0.01);
82  klt_settings.setBlockSize(3);
83  klt_settings.setPyramidLevels(3);
84  tracker.setKltOpencv(klt_settings);
85  tracker.setMaskBorder(5);
86  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
87  tracker.setCameraParameters(cam);
88  tracker.setAngleAppear( vpMath::rad(70) );
89  tracker.setAngleDisappear( vpMath::rad(80) );
90  tracker.setNearClippingDistance(0.1);
91  tracker.setFarClippingDistance(100.0);
93  }
94  tracker.setOgreVisibilityTest(true);
95  tracker.setOgreShowConfigDialog(false);
96  tracker.loadModel(objectname + ".cao");
97  tracker.setDisplayFeatures(true);
98  tracker.initClick(I, objectname + ".init", true);
99 
100  while(! g.end()){
101  g.acquire(I);
103  tracker.track(I);
104  tracker.getPose(cMo);
105  tracker.getCameraParameters(cam);
106  tracker.display(I, cMo, cam, vpColor::red, 2, true);
107  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
108  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
109  vpDisplay::flush(I);
110 
111  if (vpDisplay::getClick(I, false))
112  break;
113  }
115 
116 #ifdef VISP_HAVE_XML2
118 #endif
119 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION == 3)
120  SoDB::finish();
121 #endif
122  }
123  catch(vpException e) {
124  std::cout << "Catch a ViSP exception: " << e << std::endl;
125  }
126 #ifdef VISP_HAVE_OGRE
127  catch(Ogre::Exception e) {
128  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
129  }
130 #endif
131 #else
132  (void)argc;
133  (void)argv;
134  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
135 #endif
136 }
void setKltOpencv(const vpKltOpencv &t)
void setMovingEdge(const vpMe &me)
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.
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:460
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setMaxFeatures(const int maxCount)
void setSampleStep(const double &s)
Definition: vpMe.h:260
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
Definition: vpMe.h:59
virtual void setClipping(const unsigned int &flags)
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1240
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
virtual void setFarClippingDistance(const double &dist)
void setMu1(const double &mu_1)
Definition: vpMe.h:160
static const vpColor red
Definition: vpColor.h:163
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)
virtual void setOgreVisibilityTest(const bool &v)
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:468
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 track(const vpImage< unsigned char > &I)
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:225
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:419
void loadConfigFile(const char *configFile)
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
virtual void setCameraParameters(const vpCameraParameters &cam)
Hybrid tracker based on moving-edges and keypoints tracked using KLT tracker.
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 setMu2(const double &mu_2)
Definition: vpMe.h:174
void setWindowSize(const int winSize)
void setMaskBorder(const unsigned int &e)
virtual void loadModel(const char *modelFile, const bool verbose=false)
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)
void setThreshold(const double &t)
Definition: vpMe.h:288
virtual bool getClick(bool blocking=true)=0
void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:459
void setRange(const unsigned int &r)
Definition: vpMe.h:218
virtual void setNearClippingDistance(const double &dist)