Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tutorial-mb-tracker-full.cpp
1 #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>
8 #include <visp3/mbt/vpMbEdgeTracker.h>
9 #include <visp3/mbt/vpMbEdgeKltTracker.h>
11 #include <visp3/io/vpVideoReader.h>
12 
13 int main(int argc, char** argv)
14 {
15 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
16 
17  try {
18  std::string opt_videoname = "teabox.mpg";
19  int opt_tracker = 0;
20 
21  for (int i=0; i<argc; i++) {
22  if (std::string(argv[i]) == "--name")
23  opt_videoname = std::string(argv[i+1]);
24  if (std::string(argv[i]) == "--tracker")
25  opt_tracker = atoi(argv[i+1]);
26  else if (std::string(argv[i]) == "--help") {
27  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--tracker <0=egde|1=keypoint|2=hybrid>] [--help]\n" << std::endl;
28  return 0;
29  }
30  }
31  std::string parentname = vpIoTools::getParent(opt_videoname);
32  std::string objectname = vpIoTools::getNameWE(opt_videoname);
33 
34  if(! parentname.empty())
35  objectname = parentname + "/" + objectname;
36 
37  std::cout << "Video name: " << opt_videoname << std::endl;
38  std::cout << "Tracker requested config files: " << objectname
39  << ".[init,"
40 #ifdef VISP_HAVE_XML2
41  << "xml,"
42 #endif
43  << "cao or wrl]" << std::endl;
44  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
45 
53 
54  vpVideoReader g;
55  g.setFileName(opt_videoname);
56  g.open(I);
57 
58  vpDisplay *display = NULL;
59 #if defined(VISP_HAVE_X11)
60  display = new vpDisplayX;
61 #elif defined(VISP_HAVE_GDI)
62  display = new vpDisplayGDI;
63 #else
64  display = new vpDisplayOpenCV;
65 #endif
66  display->init(I, 100, 100, "Model-based tracker");
67 
69  vpMbTracker *tracker;
70  if (opt_tracker == 0)
71  tracker = new vpMbEdgeTracker;
72 #ifdef VISP_HAVE_MODULE_KLT
73  else if (opt_tracker == 1)
74  tracker = new vpMbKltTracker;
75  else
76  tracker = new vpMbEdgeKltTracker;
77 #else
78  else {
79  std::cout << "klt and hybrid model-based tracker are not available since visp_klt module is missing" << std::endl;
80  return 0;
81  }
82 #endif
83 
85  bool usexml = false;
87 #ifdef VISP_HAVE_XML2
88  if(vpIoTools::checkFilename(objectname + ".xml")) {
89  tracker->loadConfigFile(objectname + ".xml");
90  usexml = true;
91  }
92 #endif
93 
95  if (! usexml) {
97  if (opt_tracker == 0 || opt_tracker == 2) {
99  vpMe me;
100  me.setMaskSize(5);
101  me.setMaskNumber(180);
102  me.setRange(8);
103  me.setThreshold(10000);
104  me.setMu1(0.5);
105  me.setMu2(0.5);
106  me.setSampleStep(4);
107  dynamic_cast<vpMbEdgeTracker*>(tracker)->setMovingEdge(me);
109  }
110 
111 #ifdef VISP_HAVE_MODULE_KLT
112  if (opt_tracker == 1 || opt_tracker == 2) {
114  vpKltOpencv klt_settings;
115  klt_settings.setMaxFeatures(300);
116  klt_settings.setWindowSize(5);
117  klt_settings.setQuality(0.015);
118  klt_settings.setMinDistance(8);
119  klt_settings.setHarrisFreeParameter(0.01);
120  klt_settings.setBlockSize(3);
121  klt_settings.setPyramidLevels(3);
122  dynamic_cast<vpMbKltTracker*>(tracker)->setKltOpencv(klt_settings);
123  dynamic_cast<vpMbKltTracker*>(tracker)->setMaskBorder(5);
125  }
126 #endif
127 
129  cam.initPersProjWithoutDistortion(839.21470, 839.44555, 325.66776, 243.69727);
130  tracker->setCameraParameters(cam);
132 
134  tracker->setAngleAppear( vpMath::rad(70) );
135  tracker->setAngleDisappear( vpMath::rad(80) );
138  tracker->setNearClippingDistance(0.1);
139  tracker->setFarClippingDistance(100.0);
142  tracker->setClipping(tracker->getClipping() | vpMbtPolygon::FOV_CLIPPING);
144 
146  tracker->setOgreVisibilityTest(false);
147  tracker->setOgreShowConfigDialog(false);
149 
151  }
152 
154  if(vpIoTools::checkFilename(objectname + ".cao"))
155  tracker->loadModel(objectname + ".cao");
158  else if(vpIoTools::checkFilename(objectname + ".wrl"))
159  tracker->loadModel(objectname + ".wrl");
162  tracker->setDisplayFeatures(true);
165  tracker->initClick(I, objectname + ".init", true);
167 
168  while(! g.end()){
169  g.acquire(I);
172  tracker->track(I);
175  tracker->getPose(cMo);
178  tracker->getCameraParameters(cam);
179  tracker->display(I, cMo, cam, vpColor::red, 2, true);
181  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
182  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
183  vpDisplay::flush(I);
184 
185  if (vpDisplay::getClick(I, false))
186  break;
187  }
190 #ifdef VISP_HAVE_XML2
192 #endif
193 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION == 3)
194  SoDB::finish();
195 #endif
196  delete display;
197  delete tracker;
199  }
200  catch(vpException &e) {
201  std::cout << "Catch a ViSP exception: " << e << std::endl;
202  }
203 #ifdef VISP_HAVE_OGRE
204  catch(Ogre::Exception &e) {
205  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
206  }
207 #endif
208 #else
209  (void)argc;
210  (void)argv;
211  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
212 #endif
213 }
virtual void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:411
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:169
virtual void track(const vpImage< unsigned char > &I)=0
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.
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:488
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:263
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
Definition: vpMe.h:59
Make the complete tracking of an object by using its CAD model.
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1306
static void flush(const vpImage< unsigned char > &I)
void setMu1(const double &mu_1)
Definition: vpMe.h:226
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:479
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:508
void open(vpImage< vpRGBa > &I)
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)=0
virtual void setCameraParameters(const vpCameraParameters &camera)
Definition: vpMbTracker.h:389
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:496
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.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:103
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 loadConfigFile(const std::string &configFile)=0
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
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))
static void cleanup()
Definition: vpXmlParser.h:308
void setMu2(const double &mu_2)
Definition: vpMe.h:233
void setWindowSize(const int winSize)
virtual void setOgreVisibilityTest(const bool &v)
virtual void loadModel(const char *modelFile, const bool verbose=false)
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)
void setThreshold(const double &t)
Definition: vpMe.h:284
virtual void setClipping(const unsigned int &flags)
void setRange(const unsigned int &r)
Definition: vpMe.h:256
virtual void setFarClippingDistance(const double &dist)
virtual void setNearClippingDistance(const double &dist)