Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-mb-generic-tracker-stereo.cpp
1
2#include <cstdlib>
3#include <visp3/core/vpConfig.h>
4#include <visp3/core/vpIoTools.h>
5#include <visp3/gui/vpDisplayFactory.h>
6#include <visp3/io/vpImageIo.h>
8#include <visp3/mbt/vpMbGenericTracker.h>
10#include <visp3/io/vpVideoReader.h>
11
12int main(int argc, char **argv)
13{
14#if defined(VISP_HAVE_OPENCV) && defined(VISP_HAVE_PUGIXML) && defined(VISP_HAVE_DISPLAY)
15#ifdef ENABLE_VISP_NAMESPACE
16 using namespace VISP_NAMESPACE_NAME;
17#endif
18
19#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
20 std::shared_ptr<vpDisplay> display_left;
21 std::shared_ptr<vpDisplay> display_right;
22#else
23 vpDisplay *display_left = nullptr;
24 vpDisplay *display_right = nullptr;
25#endif
26
27 try {
28 std::string opt_videoname_left = "teabox_left.mp4";
29 std::string opt_videoname_right = "teabox_right.mp4";
30 int opt_tracker1 = vpMbGenericTracker::EDGE_TRACKER;
31 int opt_tracker2 = vpMbGenericTracker::EDGE_TRACKER;
32
33 for (int i = 1; i < argc; i++) {
34 if (std::string(argv[i]) == "--name" && i + 2 < argc) {
35 opt_videoname_left = std::string(argv[++i]);
36 opt_videoname_right = std::string(argv[++i]);
37 }
38 else if (std::string(argv[i]) == "--tracker" && i + 2 < argc) {
39 opt_tracker1 = atoi(argv[++i]);
40 opt_tracker2 = atoi(argv[++i]);
41 }
42 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
43 std::cout << "\nUsage: " << argv[0]
44 << " [--name <video name left> <video name right>]"
45 << " [--tracker <1=egde|2=klt|3=hybrid> <1=egde|2=klt|3=hybrid>]"
46 << " [--help,-h]\n"
47 << std::endl;
48 return EXIT_SUCCESS;
49 }
50 }
51
52 if ((opt_tracker1 < 1 || opt_tracker1 > 3) && (opt_tracker2 < 1 || opt_tracker2 > 3)) {
53 std::cerr << "Wrong tracker type. Correct values are: "
54 "1=egde|2=keypoint|3=hybrid."
55 << std::endl;
56 return EXIT_SUCCESS;
57 }
58
59 std::string parentname = vpIoTools::getParent(opt_videoname_left);
60 std::string objectname_left = vpIoTools::getNameWE(opt_videoname_left);
61 std::string objectname_right = vpIoTools::getNameWE(opt_videoname_right);
62
63 if (!parentname.empty()) {
64 objectname_left = parentname + "/" + objectname_left;
65 }
66
67 std::cout << "Video name: " << opt_videoname_left << " ; " << opt_videoname_right << std::endl;
68 std::cout << "Tracker requested config files: " << objectname_left << ".[init, cao]"
69 << " and " << objectname_right << ".[init, cao]" << std::endl;
70 std::cout << "Tracker optional config files: " << opt_videoname_left << ".ppm"
71 << " and " << opt_videoname_right << ".ppm" << std::endl;
72
74 vpImage<unsigned char> I_left, I_right;
76
77 vpVideoReader g_left, g_right;
78 g_left.setFileName(opt_videoname_left);
79 g_left.open(I_left);
80 g_right.setFileName(opt_videoname_right);
81 g_right.open(I_right);
82
83#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
84 display_left = vpDisplayFactory::createDisplay();
85 display_right = vpDisplayFactory::createDisplay();
86#else
87 display_left = vpDisplayFactory::allocateDisplay();
88 display_right = vpDisplayFactory::allocateDisplay();
89#endif
90 display_left->setDownScalingFactor(vpDisplay::SCALE_AUTO);
91 display_right->setDownScalingFactor(vpDisplay::SCALE_AUTO);
92 display_left->init(I_left, 100, 100, "Model-based tracker (Left)");
93 display_right->init(I_right, 110 + static_cast<int>(I_left.getWidth()), 100, "Model-based tracker (Right)");
94
96 std::vector<int> trackerTypes(2);
97 trackerTypes[0] = opt_tracker1;
98 trackerTypes[1] = opt_tracker2;
99 vpMbGenericTracker tracker(trackerTypes);
101
102#if !defined(VISP_HAVE_MODULE_KLT)
103 unsigned int nbTracker = trackerTypes.size();
104 for (unsigned int i = 0; i < nbTracker; ++i) {
105 if (trackerTypes[i] >= 2) {
106 std::cout << "klt and hybrid model-based tracker are not available "
107 "since visp_klt module is missing"
108 << std::endl;
109 return EXIT_SUCCESS;
110 }
111 }
112#endif
113
115 tracker.loadConfigFile(objectname_left + ".xml", objectname_right + ".xml");
117
119 tracker.loadModel(objectname_left + ".cao", objectname_right + ".cao");
122 tracker.setDisplayFeatures(true);
124
126 vpHomogeneousMatrix cRightMcLeft;
127 std::ifstream file_cRightMcLeft("cRightMcLeft.txt");
128 cRightMcLeft.load(file_cRightMcLeft);
129
130 std::map<std::string, vpHomogeneousMatrix> mapOfCameraTransformationMatrix;
131 mapOfCameraTransformationMatrix["Camera1"] = vpHomogeneousMatrix();
132 mapOfCameraTransformationMatrix["Camera2"] = cRightMcLeft;
133
134 tracker.setCameraTransformationMatrix(mapOfCameraTransformationMatrix);
136
138 tracker.initClick(I_left, I_right, objectname_left + ".init", objectname_right + ".init", true);
140
141 while (!g_left.end() && !g_right.end()) {
142 g_left.acquire(I_left);
143 g_right.acquire(I_right);
144
145 vpDisplay::display(I_left);
146 vpDisplay::display(I_right);
147
149 tracker.track(I_left, I_right);
151
153 vpHomogeneousMatrix cLeftMo, cRightMo;
154 tracker.getPose(cLeftMo, cRightMo);
156
158 vpCameraParameters cam_left, cam_right;
159 tracker.getCameraParameters(cam_left, cam_right);
160 tracker.display(I_left, I_right, cLeftMo, cRightMo, cam_left, cam_right, vpColor::red, 2);
162
163 vpDisplay::displayFrame(I_left, cLeftMo, cam_left, 0.025, vpColor::none, 3);
164 vpDisplay::displayFrame(I_right, cRightMo, cam_right, 0.025, vpColor::none, 3);
165 vpDisplay::displayText(I_left, 10, 10, "A click to exit...", vpColor::red);
166
167 vpDisplay::flush(I_left);
168 vpDisplay::flush(I_right);
169
170 if (vpDisplay::getClick(I_left, false)) {
171 break;
172 }
173 }
174 vpDisplay::getClick(I_left);
175 }
176 catch (const vpException &e) {
177 std::cerr << "Catch a ViSP exception: " << e.what() << std::endl;
178 }
179#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
180 if (display_left != nullptr) {
181 delete display_left;
182 }
183 if (display_right != nullptr) {
184 delete display_right;
185 }
186#endif
187#else
188 (void)argc;
189 (void)argv;
190 std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
191#endif
192}
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition vpColor.h:198
static const vpColor none
Definition vpColor.h:210
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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.
void load(std::ifstream &f)
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned int getWidth() const
Definition vpImage.h:242
static std::string getNameWE(const std::string &pathname)
static std::string getParent(const std::string &pathname)
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 open(vpImage< vpRGBa > &I) VP_OVERRIDE
void setFileName(const std::string &filename)
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.