Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
tutorial-mb-generic-tracker.cpp
1 #include <visp3/core/vpIoTools.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/io/vpImageIo.h>
8 #include <visp3/mbt/vpMbGenericTracker.h>
10 #include <visp3/io/vpVideoReader.h>
11 
12 int main(int argc, char **argv)
13 {
14 #if defined(VISP_HAVE_OPENCV)
15  try {
16  std::string opt_videoname = "model/teabox/teabox.mp4";
17  std::string opt_modelname = "model/teabox/teabox.cao";
18  int opt_tracker = 2; // Hybrid tracker
19 
20  for (int i = 0; i < argc; i++) {
21  if (std::string(argv[i]) == "--video")
22  opt_videoname = std::string(argv[i + 1]);
23  else if (std::string(argv[i]) == "--model")
24  opt_modelname = std::string(argv[i + 1]);
25  else if (std::string(argv[i]) == "--tracker")
26  opt_tracker = atoi(argv[i + 1]);
27  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
28  std::cout << "\nUsage: " << argv[0]
29  << " [--video <video name>] [--model <model name>]"
30  " [--tracker <0=egde|1=keypoint|2=hybrid>] [--help] [-h]\n"
31  << std::endl;
32  return EXIT_SUCCESS;
33  }
34  }
35  std::string parentname = vpIoTools::getParent(opt_modelname);
36  std::string objectname = vpIoTools::getNameWE(opt_modelname);
37 
38  if (!parentname.empty())
39  objectname = parentname + "/" + objectname;
40 
41  std::cout << "Video name: " << opt_videoname << std::endl;
42  std::cout << "Tracker requested config files: " << objectname << ".[init, cao]" << std::endl;
43  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
44 
52 
53  vpVideoReader g;
54  g.setFileName(opt_videoname);
55  g.open(I);
56 
57  vpDisplay *display = nullptr;
58 #if defined(VISP_HAVE_X11)
59  display = new vpDisplayX;
60 #elif defined(VISP_HAVE_GDI)
61  display = new vpDisplayGDI;
62 #elif defined(HAVE_OPENCV_HIGHGUI)
64 #endif
65  display->init(I, 100, 100, "Model-based tracker");
66 
68  vpMbGenericTracker tracker;
69  if (opt_tracker == 0)
71 #if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
72  else if (opt_tracker == 1)
74  else
76 #else
77  else {
78  std::cout << "klt and hybrid model-based tracker are not available since visp_klt module is not available. "
79  "In CMakeGUI turn visp_klt module ON, configure and build ViSP again."
80  << std::endl;
81  return EXIT_FAILURE;
82  }
83 #endif
85 
87  if (opt_tracker == 0 || opt_tracker == 2) {
88  vpMe me;
89  me.setMaskSize(5);
90  me.setMaskNumber(180);
91  me.setRange(8);
93  me.setThreshold(20);
94  me.setMu1(0.5);
95  me.setMu2(0.5);
96  me.setSampleStep(4);
97  tracker.setMovingEdge(me);
98  // Print moving-edges settings
99  me = tracker.getMovingEdge();
100  me.print();
101  }
102 
103 #if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
104  if (opt_tracker == 1 || opt_tracker == 2) {
105  vpKltOpencv klt_settings;
106  klt_settings.setMaxFeatures(300);
107  klt_settings.setWindowSize(5);
108  klt_settings.setQuality(0.015);
109  klt_settings.setMinDistance(8);
110  klt_settings.setHarrisFreeParameter(0.01);
111  klt_settings.setBlockSize(3);
112  klt_settings.setPyramidLevels(3);
113  tracker.setKltOpencv(klt_settings);
114  tracker.setKltMaskBorder(5);
115  }
116 #endif
117 
119  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
121  tracker.setCameraParameters(cam);
123 
125  tracker.loadModel(objectname + ".cao");
128  tracker.setDisplayFeatures(true);
131  tracker.initClick(I, objectname + ".init", true);
133 
134 
135 
136  while (!g.end()) {
137  g.acquire(I);
140  tracker.track(I);
143  tracker.getPose(cMo);
146  tracker.getCameraParameters(cam);
147  tracker.display(I, cMo, cam, vpColor::red, 2);
149  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
150  vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
151  vpDisplay::flush(I);
152 
153  if (vpDisplay::getClick(I, false))
154  break;
155  }
158  delete display;
160  }
161  catch (const vpException &e) {
162  std::cout << "Catch a ViSP exception: " << e << std::endl;
163  return EXIT_FAILURE;
164  }
165 #else
166  (void)argc;
167  (void)argv;
168  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
169 #endif
170  return EXIT_SUCCESS;
171 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:211
static const vpColor none
Definition: vpColor.h:223
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
Class that defines generic functionalities for display.
Definition: vpDisplay.h:173
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:59
Implementation of an homogeneous matrix and operations on such kind of matrices.
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:2003
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:2086
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:73
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:266
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:355
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:274
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:314
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:323
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:376
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:342
Real-time 6D object pose tracking using its CAD model.
virtual void setKltMaskBorder(const unsigned int &e)
virtual void getPose(vpHomogeneousMatrix &cMo) const vp_override
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false) vp_override
virtual void getCameraParameters(vpCameraParameters &camera) const vp_override
virtual void setMovingEdge(const vpMe &me)
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix()) vp_override
virtual void setKltOpencv(const vpKltOpencv &t)
virtual void setCameraParameters(const vpCameraParameters &camera) vp_override
virtual void setTrackerType(int type)
virtual void initClick(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, const std::string &initFile1, const std::string &initFile2, bool displayHelp=false, const vpHomogeneousMatrix &T1=vpHomogeneousMatrix(), const vpHomogeneousMatrix &T2=vpHomogeneousMatrix())
virtual vpMe getMovingEdge() const
virtual void setDisplayFeatures(bool displayF) vp_override
virtual void track(const vpImage< unsigned char > &I) vp_override
Definition: vpMe.h:124
void setMu1(const double &mu_1)
Definition: vpMe.h:399
void print()
Definition: vpMe.cpp:375
void setRange(const unsigned int &range)
Definition: vpMe.h:429
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:519
void setMaskNumber(const unsigned int &mask_number)
Definition: vpMe.cpp:488
void setThreshold(const double &threshold)
Definition: vpMe.h:480
void setSampleStep(const double &sample_step)
Definition: vpMe.h:436
void setMaskSize(const unsigned int &mask_size)
Definition: vpMe.cpp:496
void setMu2(const double &mu_2)
Definition: vpMe.h:406
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:135
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)
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.