Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
tutorial-mb-hybrid-tracker.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpIoTools.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/io/vpImageIo.h>
8 #include <visp3/io/vpVideoReader.h>
9 #include <visp3/mbt/vpMbEdgeKltTracker.h>
10 
11 int main(int argc, char **argv)
12 {
13 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
14 #ifdef ENABLE_VISP_NAMESPACE
15  using namespace VISP_NAMESPACE_NAME;
16 #endif
17 
18  try {
19  std::string videoname = "teabox.mp4";
20 
21  for (int i = 0; i < argc; i++) {
22  if (std::string(argv[i]) == "--name")
23  videoname = std::string(argv[i + 1]);
24  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
25  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help] [-h]\n" << std::endl;
26  return EXIT_SUCCESS;
27  }
28  }
29  std::string parentname = vpIoTools::getParent(videoname);
30  std::string objectname = vpIoTools::getNameWE(videoname);
31 
32  if (!parentname.empty())
33  objectname = parentname + "/" + objectname;
34 
35  std::cout << "Video name: " << videoname << std::endl;
36  std::cout << "Tracker requested config files: " << objectname << ".[init,"
37  << "xml,"
38  << "cao or wrl]" << std::endl;
39  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
40 
44 
45  vpVideoReader g;
46  g.setFileName(videoname);
47  g.open(I);
48 
49 #if defined(VISP_HAVE_X11)
50  vpDisplayX display(I, 100, 100, "Model-based hybrid tracker");
51 #elif defined(VISP_HAVE_GDI)
52  vpDisplayGDI display(I, 100, 100, "Model-based hybrid tracker");
53 #elif defined(HAVE_OPENCV_HIGHGUI)
54  vpDisplayOpenCV display(I, 100, 100, "Model-based hybrid tracker");
55 #else
56  std::cout << "No image viewer is available..." << std::endl;
57 #endif
58 
59  vpMbEdgeKltTracker tracker;
60  bool usexml = false;
61 #if defined(VISP_HAVE_PUGIXML)
62  if (vpIoTools::checkFilename(objectname + ".xml")) {
63  tracker.loadConfigFile(objectname + ".xml");
64  usexml = true;
65  }
66 #endif
67  if (!usexml) {
68  vpMe me;
69  me.setMaskSize(5);
70  me.setMaskNumber(180);
71  me.setRange(8);
73  me.setThreshold(20);
74  me.setMu1(0.5);
75  me.setMu2(0.5);
76  me.setSampleStep(4);
77  tracker.setMovingEdge(me);
78  vpKltOpencv klt_settings;
79  klt_settings.setMaxFeatures(300);
80  klt_settings.setWindowSize(5);
81  klt_settings.setQuality(0.015);
82  klt_settings.setMinDistance(8);
83  klt_settings.setHarrisFreeParameter(0.01);
84  klt_settings.setBlockSize(3);
85  klt_settings.setPyramidLevels(3);
86  tracker.setKltOpencv(klt_settings);
87  tracker.setMaskBorder(5);
88  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
89  tracker.setCameraParameters(cam);
90  tracker.setAngleAppear(vpMath::rad(70));
91  tracker.setAngleDisappear(vpMath::rad(80));
92  tracker.setNearClippingDistance(0.1);
93  tracker.setFarClippingDistance(100.0);
94  tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
95  }
96  tracker.setOgreVisibilityTest(true);
97  tracker.setOgreShowConfigDialog(false);
98  tracker.loadModel(objectname + ".cao");
99  tracker.setDisplayFeatures(true);
100  tracker.initClick(I, objectname + ".init", true);
101 
102  while (!g.end()) {
103  g.acquire(I);
105  tracker.track(I);
106  tracker.getPose(cMo);
107  tracker.getCameraParameters(cam);
108  tracker.display(I, cMo, cam, vpColor::red, 2);
109  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
110  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
111  vpDisplay::flush(I);
112 
113  if (vpDisplay::getClick(I, false))
114  break;
115  }
117  }
118  catch (const vpException &e) {
119  std::cout << "Catch a ViSP exception: " << e << std::endl;
120  }
121 #ifdef VISP_HAVE_OGRE
122  catch (Ogre::Exception &e) {
123  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
124  }
125 #endif
126 #else
127  (void)argc;
128  (void)argv;
129  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
130 #endif
131 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
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), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:786
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1227
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1314
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:74
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:267
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:356
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:275
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:315
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:324
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:377
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:343
static double rad(double deg)
Definition: vpMath.h:129
Definition: vpMe.h:134
void setMu1(const double &mu_1)
Definition: vpMe.h:385
void setRange(const unsigned int &range)
Definition: vpMe.h:415
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:505
void setMaskNumber(const unsigned int &mask_number)
Definition: vpMe.cpp:552
void setThreshold(const double &threshold)
Definition: vpMe.h:466
void setSampleStep(const double &sample_step)
Definition: vpMe.h:422
void setMaskSize(const unsigned int &mask_size)
Definition: vpMe.cpp:560
void setMu2(const double &mu_2)
Definition: vpMe.h:392
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:145
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)