Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
tutorial-mb-tracker-stereo.cpp
1 #include <iostream>
3 #include <visp3/core/vpConfig.h>
4 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
5 
6 #include <visp3/core/vpIoTools.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpDisplayOpenCV.h>
9 #include <visp3/gui/vpDisplayX.h>
10 #include <visp3/io/vpImageIo.h>
12 #include <visp3/mbt/vpMbEdgeKltMultiTracker.h>
13 #include <visp3/mbt/vpMbEdgeMultiTracker.h>
15 #include <visp3/io/vpVideoReader.h>
16 
17 int main(int argc, char **argv)
18 {
19 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
20  try {
21  std::string opt_videoname_left = "teabox_left.mpg";
22  std::string opt_videoname_right = "teabox_right.mpg";
23  int opt_tracker = 0;
24 
25  for (int i = 0; i < argc; i++) {
26  if (std::string(argv[i]) == "--name" && i + 2 < argc) {
27  opt_videoname_left = std::string(argv[i + 1]);
28  opt_videoname_right = std::string(argv[i + 2]);
29  } else if (std::string(argv[i]) == "--tracker" && i + 1 < argc) {
30  opt_tracker = atoi(argv[i + 1]);
31  } else if (std::string(argv[i]) == "--help") {
32  std::cout << "\nUsage: " << argv[0]
33  << " [--name <video name left> <video name right>] "
34  "[--tracker <0=egde|1=klt|2=hybrid>] [--help]\n"
35  << std::endl;
36  return 0;
37  }
38  }
39  std::string parentname = vpIoTools::getParent(opt_videoname_left);
40  std::string objectname_left = vpIoTools::getNameWE(opt_videoname_left);
41  std::string objectname_right = vpIoTools::getNameWE(opt_videoname_right);
42 
43  if (!parentname.empty()) {
44  objectname_left = parentname + "/" + objectname_left;
45  }
46 
47  std::cout << "Video name: " << opt_videoname_left << " ; " << opt_videoname_right << std::endl;
48  std::cout << "Tracker requested config files: " << objectname_left << ".[init, cao]"
49  << " and " << objectname_right << ".[init, cao]" << std::endl;
50  std::cout << "Tracker optional config files: " << opt_videoname_left << ".ppm"
51  << " and " << opt_videoname_right << ".ppm" << std::endl;
52 
54  vpImage<unsigned char> I_left, I_right;
56 
57  vpVideoReader g_left, g_right;
58  g_left.setFileName(opt_videoname_left);
59  g_left.open(I_left);
60  g_right.setFileName(opt_videoname_right);
61  g_right.open(I_right);
62 
63  vpDisplay *display_left = NULL, *display_right = NULL;
64 #if defined(VISP_HAVE_X11)
65  display_left = new vpDisplayX;
66  display_right = new vpDisplayX;
67 #elif defined(VISP_HAVE_GDI)
68  display_left = new vpDisplayGDI;
69  display_right = new vpDisplayGDI;
70 #else
71  display_left = new vpDisplayOpenCV;
72  display_right = new vpDisplayOpenCV;
73 #endif
75  display_right->setDownScalingFactor(vpDisplay::SCALE_AUTO);
76  display_left->init(I_left, 100, 100, "Model-based tracker (Left)");
77  display_right->init(I_right, 110 + (int)I_left.getWidth(), 100, "Model-based tracker (Right)");
78 
80  vpMbTracker *tracker;
81  if (opt_tracker == 0)
82  tracker = new vpMbEdgeMultiTracker(2);
83 #ifdef VISP_HAVE_MODULE_KLT
84  else if (opt_tracker == 1)
85  tracker = new vpMbKltMultiTracker(2);
86  else
87  tracker = new vpMbEdgeKltMultiTracker(2);
88 #else
89  else {
90  std::cout << "klt and hybrid model-based tracker are not available "
91  "since visp_klt module is missing"
92  << std::endl;
93  return 0;
94  }
95 #endif
96 
99  if (opt_tracker == 0)
100  dynamic_cast<vpMbEdgeMultiTracker *>(tracker)->loadConfigFile(objectname_left + ".xml",
101  objectname_right + ".xml");
102 #if defined(VISP_HAVE_MODULE_KLT)
103  else if (opt_tracker == 1)
104  dynamic_cast<vpMbKltMultiTracker *>(tracker)->loadConfigFile(objectname_left + ".xml", objectname_right + ".xml");
105  else
106  dynamic_cast<vpMbEdgeKltMultiTracker *>(tracker)->loadConfigFile(objectname_left + ".xml",
107  objectname_right + ".xml");
108 #endif
109 
112  vpCameraParameters cam_left, cam_right;
113  if (opt_tracker == 0)
114  dynamic_cast<vpMbEdgeMultiTracker *>(tracker)->getCameraParameters(cam_left, cam_right);
115 #if defined(VISP_HAVE_MODULE_KLT)
116  else if (opt_tracker == 1)
117  dynamic_cast<vpMbKltMultiTracker *>(tracker)->getCameraParameters(cam_left, cam_right);
118  else
119  dynamic_cast<vpMbEdgeKltMultiTracker *>(tracker)->getCameraParameters(cam_left, cam_right);
120 #endif
121 
124  tracker->loadModel("teabox.cao");
127  tracker->setDisplayFeatures(true);
129 
130  vpHomogeneousMatrix cRightMcLeft;
131  std::ifstream file_cRightMcLeft("cRightMcLeft.txt");
132  cRightMcLeft.load(file_cRightMcLeft);
133 
135  std::map<std::string, vpHomogeneousMatrix> mapOfCameraTransformationMatrix;
136  mapOfCameraTransformationMatrix["Camera1"] = vpHomogeneousMatrix();
137  mapOfCameraTransformationMatrix["Camera2"] = cRightMcLeft;
138 
139  if (opt_tracker == 0)
140  dynamic_cast<vpMbEdgeMultiTracker *>(tracker)->setCameraTransformationMatrix(mapOfCameraTransformationMatrix);
141 #if defined(VISP_HAVE_MODULE_KLT)
142  else if (opt_tracker == 1)
143  dynamic_cast<vpMbKltMultiTracker *>(tracker)->setCameraTransformationMatrix(mapOfCameraTransformationMatrix);
144  else
145  dynamic_cast<vpMbEdgeKltMultiTracker *>(tracker)->setCameraTransformationMatrix(mapOfCameraTransformationMatrix);
146 #endif
147 
149 #ifndef VISP_HAVE_XML2
150  std::cout << "\n**********************************************************\n"
151  << "Warning: we are not able to load the tracker settings from\n"
152  << "the xml config files since ViSP is not build with libxml2\n"
153  << "3rd party. As a consequence, the tracking may fail !"
154  << "\n**********************************************************\n"
155  << std::endl;
156 #endif
157 
159  if (opt_tracker == 0)
160  dynamic_cast<vpMbEdgeMultiTracker *>(tracker)->initClick(I_left, I_right, objectname_left + ".init",
161  objectname_right + ".init", true);
162 #if defined(VISP_HAVE_MODULE_KLT)
163  else if (opt_tracker == 1)
164  dynamic_cast<vpMbKltMultiTracker *>(tracker)->initClick(I_left, I_right, objectname_left + ".init",
165  objectname_right + ".init", true);
166  else
167  dynamic_cast<vpMbEdgeKltMultiTracker *>(tracker)->initClick(I_left, I_right, objectname_left + ".init",
168  objectname_right + ".init", true);
169 #endif
170 
173  vpHomogeneousMatrix cLeftMo, cRightMo;
175  while (!g_left.end() && !g_right.end()) {
176  g_left.acquire(I_left);
177  g_right.acquire(I_right);
178 
179  vpDisplay::display(I_left);
180  vpDisplay::display(I_right);
181 
183  if (opt_tracker == 0)
184  dynamic_cast<vpMbEdgeMultiTracker *>(tracker)->track(I_left, I_right);
185 #if defined(VISP_HAVE_MODULE_KLT)
186  else if (opt_tracker == 1)
187  dynamic_cast<vpMbKltMultiTracker *>(tracker)->track(I_left, I_right);
188  else
189  dynamic_cast<vpMbEdgeKltMultiTracker *>(tracker)->track(I_left, I_right);
190 #endif
191 
194  if (opt_tracker == 0)
195  dynamic_cast<vpMbEdgeMultiTracker *>(tracker)->getPose(cLeftMo, cRightMo);
196 #if defined(VISP_HAVE_MODULE_KLT)
197  else if (opt_tracker == 1)
198  dynamic_cast<vpMbKltMultiTracker *>(tracker)->getPose(cLeftMo, cRightMo);
199  else
200  dynamic_cast<vpMbEdgeKltMultiTracker *>(tracker)->getPose(cLeftMo, cRightMo);
201 #endif
202 
205  if (opt_tracker == 0)
206  dynamic_cast<vpMbEdgeMultiTracker *>(tracker)->display(I_left, I_right, cLeftMo, cRightMo, cam_left, cam_right,
207  vpColor::red, 2);
208 #if defined(VISP_HAVE_MODULE_KLT)
209  else if (opt_tracker == 1)
210  dynamic_cast<vpMbKltMultiTracker *>(tracker)->display(I_left, I_right, cLeftMo, cRightMo, cam_left, cam_right,
211  vpColor::red, 2);
212  else
213  dynamic_cast<vpMbEdgeKltMultiTracker *>(tracker)->display(I_left, I_right, cLeftMo, cRightMo, cam_left,
214  cam_right, vpColor::red, 2);
215 #endif
216 
218  vpDisplay::displayFrame(I_left, cLeftMo, cam_left, 0.025, vpColor::none, 3);
219  vpDisplay::displayFrame(I_right, cRightMo, cam_right, 0.025, vpColor::none, 3);
220  vpDisplay::displayText(I_left, 10, 10, "A click to exit...", vpColor::red);
221 
222  vpDisplay::flush(I_left);
223  vpDisplay::flush(I_right);
224 
225  if (vpDisplay::getClick(I_left, false)) {
226  break;
227  }
228  }
229  vpDisplay::getClick(I_left);
230 
232  delete display_left;
233  delete display_right;
234  delete tracker;
236  } catch (const vpException &e) {
237  std::cerr << "Catch a ViSP exception: " << e.getMessage() << std::endl;
238  }
239 #else
240  (void)argc;
241  (void)argv;
242  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
243 #endif
244 }
245 #else
246 int main()
247 {
248  std::cout << "Nothing to run, deprecated tutorial." << std::endl;
249  return 0;
250 }
251 #endif //#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
virtual void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:503
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
Definition: vpImage.h:239
Implementation of an homogeneous matrix and operations on such kind of matrices.
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:232
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
virtual void loadModel(const std::string &modelFile, const bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
static const vpColor none
Definition: vpColor.h:192
error that can be emited by ViSP classes.
Definition: vpException.h:71
Hybrid stereo (or more) tracker based on moving-edges and keypoints tracked using KLT tracker...
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1477
static void flush(const vpImage< unsigned char > &I)
void load(std::ifstream &f)
static const vpColor red
Definition: vpColor.h:180
void open(vpImage< vpRGBa > &I)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:110
void acquire(vpImage< vpRGBa > &I)
const char * getMessage(void) const
Definition: vpException.cpp:90
void setFileName(const char *filename)
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))
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1464
Make the complete stereo (or more) tracking of an object by using its CAD model.
Model based stereo (or more) tracker using only KLT.