Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
tutorial-mb-generic-tracker-stereo.cpp
1 #include <cstdlib>
3 #include <visp3/core/vpConfig.h>
4 #include <visp3/core/vpIoTools.h>
5 #include <visp3/gui/vpDisplayGDI.h>
6 #include <visp3/gui/vpDisplayOpenCV.h>
7 #include <visp3/gui/vpDisplayX.h>
8 #include <visp3/io/vpImageIo.h>
10 #include <visp3/mbt/vpMbGenericTracker.h>
12 #include <visp3/io/vpVideoReader.h>
13 
14 int main(int argc, char **argv)
15 {
16 #if defined(VISP_HAVE_OPENCV) && defined(VISP_HAVE_PUGIXML)
17  try {
18  std::string opt_videoname_left = "teabox_left.mp4";
19  std::string opt_videoname_right = "teabox_right.mp4";
20  int opt_tracker1 = vpMbGenericTracker::EDGE_TRACKER;
21  int opt_tracker2 = vpMbGenericTracker::EDGE_TRACKER;
22 
23  for (int i = 0; i < argc; i++) {
24  if (std::string(argv[i]) == "--name" && i + 2 < argc) {
25  opt_videoname_left = std::string(argv[i + 1]);
26  opt_videoname_right = std::string(argv[i + 2]);
27  }
28  else if (std::string(argv[i]) == "--tracker" && i + 2 < argc) {
29  opt_tracker1 = atoi(argv[i + 1]);
30  opt_tracker2 = atoi(argv[i + 2]);
31  }
32  else if (std::string(argv[i]) == "--help") {
33  std::cout << "\nUsage: " << argv[0]
34  << " [--name <video name left> <video name right>]"
35  " [--tracker <1=egde|2=klt|3=hybrid> <1=egde|2=klt|3=hybrid>]"
36  " [--help]\n"
37  << std::endl;
38  return EXIT_SUCCESS;
39  }
40  }
41 
42  if ((opt_tracker1 < 1 || opt_tracker1 > 3) && (opt_tracker2 < 1 || opt_tracker2 > 3)) {
43  std::cerr << "Wrong tracker type. Correct values are: "
44  "1=egde|2=keypoint|3=hybrid."
45  << std::endl;
46  return EXIT_SUCCESS;
47  }
48 
49  std::string parentname = vpIoTools::getParent(opt_videoname_left);
50  std::string objectname_left = vpIoTools::getNameWE(opt_videoname_left);
51  std::string objectname_right = vpIoTools::getNameWE(opt_videoname_right);
52 
53  if (!parentname.empty()) {
54  objectname_left = parentname + "/" + objectname_left;
55  }
56 
57  std::cout << "Video name: " << opt_videoname_left << " ; " << opt_videoname_right << std::endl;
58  std::cout << "Tracker requested config files: " << objectname_left << ".[init, cao]"
59  << " and " << objectname_right << ".[init, cao]" << std::endl;
60  std::cout << "Tracker optional config files: " << opt_videoname_left << ".ppm"
61  << " and " << opt_videoname_right << ".ppm" << std::endl;
62 
64  vpImage<unsigned char> I_left, I_right;
66 
67  vpVideoReader g_left, g_right;
68  g_left.setFileName(opt_videoname_left);
69  g_left.open(I_left);
70  g_right.setFileName(opt_videoname_right);
71  g_right.open(I_right);
72 
73 #if defined(VISP_HAVE_X11)
74  vpDisplayX display_left;
75  vpDisplayX display_right;
76 #elif defined(VISP_HAVE_GDI)
77  vpDisplayGDI display_left;
78  vpDisplayGDI display_right;
79 #elif defined(HAVE_OPENCV_HIGHGUI)
80  vpDisplayOpenCV display_left;
81  vpDisplayOpenCV display_right;
82 #endif
85  display_left.init(I_left, 100, 100, "Model-based tracker (Left)");
86  display_right.init(I_right, 110 + (int)I_left.getWidth(), 100, "Model-based tracker (Right)");
87 
89  std::vector<int> trackerTypes(2);
90  trackerTypes[0] = opt_tracker1;
91  trackerTypes[1] = opt_tracker2;
92  vpMbGenericTracker tracker(trackerTypes);
94 
95 #if !defined(VISP_HAVE_MODULE_KLT)
96  if (opt_tracker >= 2) {
97  std::cout << "klt and hybrid model-based tracker are not available "
98  "since visp_klt module is missing"
99  << std::endl;
100  return EXIT_SUCCESS;
101  }
102 #endif
103 
105  tracker.loadConfigFile(objectname_left + ".xml", objectname_right + ".xml");
107 
109  tracker.loadModel(objectname_left + ".cao", objectname_right + ".cao");
112  tracker.setDisplayFeatures(true);
114 
116  vpHomogeneousMatrix cRightMcLeft;
117  std::ifstream file_cRightMcLeft("cRightMcLeft.txt");
118  cRightMcLeft.load(file_cRightMcLeft);
119 
120  std::map<std::string, vpHomogeneousMatrix> mapOfCameraTransformationMatrix;
121  mapOfCameraTransformationMatrix["Camera1"] = vpHomogeneousMatrix();
122  mapOfCameraTransformationMatrix["Camera2"] = cRightMcLeft;
123 
124  tracker.setCameraTransformationMatrix(mapOfCameraTransformationMatrix);
126 
128  tracker.initClick(I_left, I_right, objectname_left + ".init", objectname_right + ".init", true);
130 
131  while (!g_left.end() && !g_right.end()) {
132  g_left.acquire(I_left);
133  g_right.acquire(I_right);
134 
135  vpDisplay::display(I_left);
136  vpDisplay::display(I_right);
137 
139  tracker.track(I_left, I_right);
141 
143  vpHomogeneousMatrix cLeftMo, cRightMo;
144  tracker.getPose(cLeftMo, cRightMo);
146 
148  vpCameraParameters cam_left, cam_right;
149  tracker.getCameraParameters(cam_left, cam_right);
150  tracker.display(I_left, I_right, cLeftMo, cRightMo, cam_left, cam_right, vpColor::red, 2);
152 
153  vpDisplay::displayFrame(I_left, cLeftMo, cam_left, 0.025, vpColor::none, 3);
154  vpDisplay::displayFrame(I_right, cRightMo, cam_right, 0.025, vpColor::none, 3);
155  vpDisplay::displayText(I_left, 10, 10, "A click to exit...", vpColor::red);
156 
157  vpDisplay::flush(I_left);
158  vpDisplay::flush(I_right);
159 
160  if (vpDisplay::getClick(I_left, false)) {
161  break;
162  }
163  }
164  vpDisplay::getClick(I_left);
165  }
166  catch (const vpException &e) {
167  std::cerr << "Catch a ViSP exception: " << e.what() << std::endl;
168  }
169 #else
170  (void)argc;
171  (void)argv;
172  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
173 #endif
174 }
Generic class defining intrinsic camera parameters.
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
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") vp_override
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:227
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)
@ SCALE_AUTO
Definition: vpDisplay.h:179
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
const char * what() const
Definition: vpException.cpp:70
Implementation of an homogeneous matrix and operations on such kind of matrices.
void load(std::ifstream &f)
unsigned int getWidth() const
Definition: vpImage.h:245
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:2003
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:2086
Real-time 6D object pose tracking using its CAD model.
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)