Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-mb-klt-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/vpMbKltTracker.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") {
25  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help]\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;
43 
44  vpVideoReader g;
45  g.setFileName(videoname);
46  g.open(I);
47 
48 #if defined(VISP_HAVE_X11)
49  vpDisplayX display;
50 #elif defined(VISP_HAVE_GDI)
51  vpDisplayGDI display;
52 #elif defined(HAVE_OPENCV_HIGHGUI)
53  vpDisplayOpenCV display;
54 #else
55  std::cout << "No image viewer is available..." << std::endl;
56  return EXIT_FAILURE;
57 #endif
58 
59  display.init(I, 100, 100, "Model-based keypoint tracker");
60 
61  vpMbKltTracker tracker;
62  bool usexml = false;
64 #if defined(VISP_HAVE_PUGIXML)
65  if (vpIoTools::checkFilename(objectname + ".xml")) {
66  tracker.loadConfigFile(objectname + ".xml");
67  usexml = true;
68  }
69 #endif
71  if (!usexml) {
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  tracker.setKltMaskBorder(5);
83  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
84  tracker.setCameraParameters(cam);
85  tracker.setAngleAppear(vpMath::rad(70));
86  tracker.setAngleDisappear(vpMath::rad(80));
87  tracker.setNearClippingDistance(0.1);
88  tracker.setFarClippingDistance(100.0);
89  tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
91  }
92  tracker.setOgreVisibilityTest(true);
93  tracker.setOgreShowConfigDialog(false);
94  tracker.loadModel(objectname + "-triangle.cao");
95  tracker.setDisplayFeatures(true);
96  tracker.initClick(I, objectname + ".init", true);
97 
98  while (!g.end()) {
99  g.acquire(I);
101  tracker.track(I);
102  tracker.getPose(cMo);
103  tracker.getCameraParameters(cam);
104  tracker.display(I, cMo, cam, vpColor::red, 2);
105  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
106  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
107  vpDisplay::flush(I);
108 
109  if (vpDisplay::getClick(I, false))
110  break;
111  }
113  }
114  catch (const vpException &e) {
115  std::cout << "Catch a ViSP exception: " << e << std::endl;
116  }
117 #ifdef VISP_HAVE_OGRE
118  catch (Ogre::Exception &e) {
119  std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
120 }
121 #endif
122 #else
123  (void)argc;
124  (void)argv;
125  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
126 #endif
127 }
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
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)