Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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 #ifdef ENABLE_VISP_NAMESPACE
18  using namespace VISP_NAMESPACE_NAME;
19 #endif
20 
21  try {
22  std::string opt_videoname_left = "teabox_left.mp4";
23  std::string opt_videoname_right = "teabox_right.mp4";
24  int opt_tracker1 = vpMbGenericTracker::EDGE_TRACKER;
25  int opt_tracker2 = vpMbGenericTracker::EDGE_TRACKER;
26 
27  for (int i = 0; i < argc; i++) {
28  if (std::string(argv[i]) == "--name" && i + 2 < argc) {
29  opt_videoname_left = std::string(argv[i + 1]);
30  opt_videoname_right = std::string(argv[i + 2]);
31  }
32  else if (std::string(argv[i]) == "--tracker" && i + 2 < argc) {
33  opt_tracker1 = atoi(argv[i + 1]);
34  opt_tracker2 = atoi(argv[i + 2]);
35  }
36  else if (std::string(argv[i]) == "--help") {
37  std::cout << "\nUsage: " << argv[0]
38  << " [--name <video name left> <video name right>]"
39  " [--tracker <1=egde|2=klt|3=hybrid> <1=egde|2=klt|3=hybrid>]"
40  " [--help]\n"
41  << std::endl;
42  return EXIT_SUCCESS;
43  }
44  }
45 
46  if ((opt_tracker1 < 1 || opt_tracker1 > 3) && (opt_tracker2 < 1 || opt_tracker2 > 3)) {
47  std::cerr << "Wrong tracker type. Correct values are: "
48  "1=egde|2=keypoint|3=hybrid."
49  << std::endl;
50  return EXIT_SUCCESS;
51  }
52 
53  std::string parentname = vpIoTools::getParent(opt_videoname_left);
54  std::string objectname_left = vpIoTools::getNameWE(opt_videoname_left);
55  std::string objectname_right = vpIoTools::getNameWE(opt_videoname_right);
56 
57  if (!parentname.empty()) {
58  objectname_left = parentname + "/" + objectname_left;
59  }
60 
61  std::cout << "Video name: " << opt_videoname_left << " ; " << opt_videoname_right << std::endl;
62  std::cout << "Tracker requested config files: " << objectname_left << ".[init, cao]"
63  << " and " << objectname_right << ".[init, cao]" << std::endl;
64  std::cout << "Tracker optional config files: " << opt_videoname_left << ".ppm"
65  << " and " << opt_videoname_right << ".ppm" << std::endl;
66 
68  vpImage<unsigned char> I_left, I_right;
70 
71  vpVideoReader g_left, g_right;
72  g_left.setFileName(opt_videoname_left);
73  g_left.open(I_left);
74  g_right.setFileName(opt_videoname_right);
75  g_right.open(I_right);
76 
77 #if defined(VISP_HAVE_X11)
78  vpDisplayX display_left;
79  vpDisplayX display_right;
80 #elif defined(VISP_HAVE_GDI)
81  vpDisplayGDI display_left;
82  vpDisplayGDI display_right;
83 #elif defined(HAVE_OPENCV_HIGHGUI)
84  vpDisplayOpenCV display_left;
85  vpDisplayOpenCV display_right;
86 #endif
88  display_right.setDownScalingFactor(vpDisplay::SCALE_AUTO);
89  display_left.init(I_left, 100, 100, "Model-based tracker (Left)");
90  display_right.init(I_right, 110 + (int)I_left.getWidth(), 100, "Model-based tracker (Right)");
91 
93  std::vector<int> trackerTypes(2);
94  trackerTypes[0] = opt_tracker1;
95  trackerTypes[1] = opt_tracker2;
96  vpMbGenericTracker tracker(trackerTypes);
98 
99 #if !defined(VISP_HAVE_MODULE_KLT)
100  unsigned int nbTracker = trackerTypes.size();
101  for (unsigned int i = 0; i < nbTracker; ++i) {
102  if (trackerTypes[i] >= 2) {
103  std::cout << "klt and hybrid model-based tracker are not available "
104  "since visp_klt module is missing"
105  << std::endl;
106  return EXIT_SUCCESS;
107  }
108  }
109 #endif
110 
112  tracker.loadConfigFile(objectname_left + ".xml", objectname_right + ".xml");
114 
116  tracker.loadModel(objectname_left + ".cao", objectname_right + ".cao");
119  tracker.setDisplayFeatures(true);
121 
123  vpHomogeneousMatrix cRightMcLeft;
124  std::ifstream file_cRightMcLeft("cRightMcLeft.txt");
125  cRightMcLeft.load(file_cRightMcLeft);
126 
127  std::map<std::string, vpHomogeneousMatrix> mapOfCameraTransformationMatrix;
128  mapOfCameraTransformationMatrix["Camera1"] = vpHomogeneousMatrix();
129  mapOfCameraTransformationMatrix["Camera2"] = cRightMcLeft;
130 
131  tracker.setCameraTransformationMatrix(mapOfCameraTransformationMatrix);
133 
135  tracker.initClick(I_left, I_right, objectname_left + ".init", objectname_right + ".init", true);
137 
138  while (!g_left.end() && !g_right.end()) {
139  g_left.acquire(I_left);
140  g_right.acquire(I_right);
141 
142  vpDisplay::display(I_left);
143  vpDisplay::display(I_right);
144 
146  tracker.track(I_left, I_right);
148 
150  vpHomogeneousMatrix cLeftMo, cRightMo;
151  tracker.getPose(cLeftMo, cRightMo);
153 
155  vpCameraParameters cam_left, cam_right;
156  tracker.getCameraParameters(cam_left, cam_right);
157  tracker.display(I_left, I_right, cLeftMo, cRightMo, cam_left, cam_right, vpColor::red, 2);
159 
160  vpDisplay::displayFrame(I_left, cLeftMo, cam_left, 0.025, vpColor::none, 3);
161  vpDisplay::displayFrame(I_right, cRightMo, cam_right, 0.025, vpColor::none, 3);
162  vpDisplay::displayText(I_left, 10, 10, "A click to exit...", vpColor::red);
163 
164  vpDisplay::flush(I_left);
165  vpDisplay::flush(I_right);
166 
167  if (vpDisplay::getClick(I_left, false)) {
168  break;
169  }
170  }
171  vpDisplay::getClick(I_left);
172  }
173  catch (const vpException &e) {
174  std::cerr << "Catch a ViSP exception: " << e.what() << std::endl;
175  }
176 #else
177  (void)argc;
178  (void)argv;
179  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
180 #endif
181 }
Generic class defining intrinsic camera parameters.
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)
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:233
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:184
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
const char * what() const
Definition: vpException.cpp:71
Implementation of an homogeneous matrix and operations on such kind of matrices.
void load(std::ifstream &f)
unsigned int getWidth() const
Definition: vpImage.h:242
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1227
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1314
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)