Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-mb-generic-tracker-rgbd-structure-core.cpp
1
2#include <iostream>
3
4#include <visp3/core/vpConfig.h>
5
6#if defined(VISP_HAVE_OCCIPITAL_STRUCTURE) && defined(VISP_HAVE_PUGIXML) && defined(VISP_HAVE_OPENCV) && \
7 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && (defined(HAVE_OPENCV_FEATURES2D) || defined(HAVE_OPENCV_XFEATURES2D))) || \
8 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
9
10#include <visp3/core/vpDisplay.h>
11#include <visp3/core/vpIoTools.h>
12#include <visp3/core/vpXmlParserCamera.h>
13#include <visp3/gui/vpDisplayFactory.h>
14#include <visp3/mbt/vpMbGenericTracker.h>
15#include <visp3/sensor/vpOccipitalStructure.h>
16#include <visp3/vision/vpKeyPoint.h>
17
18#ifdef ENABLE_VISP_NAMESPACE
19using namespace VISP_NAMESPACE_NAME;
20#endif
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23typedef enum DepthType
24{
25 DEPTH_UNUSED = 0,
26 DEPTH_DENSE = 1,
27 DEPTH_NORMAL = 2,
28 DEPTH_COUNT = 3
29}DepthType;
30
31std::string depthTypeToString(const DepthType &type)
32{
33 std::string name;
34 switch (type) {
35 case DEPTH_UNUSED:
36 name = "unused";
37 break;
38 case DEPTH_DENSE:
39 name = "dense";
40 break;
41 case DEPTH_NORMAL:
42 name = "normals";
43 break;
44 case DEPTH_COUNT:
45 default:
46 name = "unknown";
47 break;
48 }
49 return name;
50}
51
52DepthType depthTypeFromString(const std::string &name)
53{
54 DepthType type(DEPTH_COUNT);
55 unsigned int i = 0;
56 bool notFound = true;
57 while ((i < static_cast<unsigned int>(DEPTH_COUNT)) && notFound) {
58 DepthType candidate = static_cast<DepthType>(i);
59 if (vpIoTools::toLowerCase(name) == depthTypeToString(candidate)) {
60 notFound = false;
61 type = candidate;
62 }
63 ++i;
64 }
65 return type;
66}
67
68std::string getDepthTypeList(const std::string &prefix = "<", const std::string &sep = " , ", const std::string &suffix = ">")
69{
70 std::string list(prefix);
71 unsigned int i = 0;
72 while (i < static_cast<unsigned int>(DEPTH_COUNT - 1)) {
73 DepthType type = static_cast<DepthType>(i);
74 std::string name = depthTypeToString(type);
75 list += name + sep;
76 ++i;
77 }
78 DepthType type = static_cast<DepthType>(DEPTH_COUNT - 1);
79 std::string name = depthTypeToString(type);
80 list += name + suffix;
81 return list;
82}
83#endif
84
85int main(int argc, char *argv[])
86{
87 std::string config_color = "", config_depth = "";
88 std::string model_color = "", model_depth = "";
89 std::string init_file = "";
90 bool use_ogre = false;
91 bool use_scanline = false;
92 bool use_edges = true;
93 bool use_klt = true;
94 DepthType use_depth = DEPTH_DENSE;
95 bool learn = false;
96 bool auto_init = false;
97 double proj_error_threshold = 25;
98 std::string learning_data = "learning/data-learned.bin";
99 bool display_projection_error = false;
100
101 for (int i = 1; i < argc; i++) {
102 if (std::string(argv[i]) == "--config_color" && i + 1 < argc) {
103 config_color = std::string(argv[i + 1]);
104 }
105 else if (std::string(argv[i]) == "--config_depth" && i + 1 < argc) {
106 config_depth = std::string(argv[i + 1]);
107 }
108 else if (std::string(argv[i]) == "--model_color" && i + 1 < argc) {
109 model_color = std::string(argv[i + 1]);
110 }
111 else if (std::string(argv[i]) == "--model_depth" && i + 1 < argc) {
112 model_depth = std::string(argv[i + 1]);
113 }
114 else if (std::string(argv[i]) == "--init_file" && i + 1 < argc) {
115 init_file = std::string(argv[i + 1]);
116 }
117 else if (std::string(argv[i]) == "--proj_error_threshold" && i + 1 < argc) {
118 proj_error_threshold = std::atof(argv[i + 1]);
119 }
120 else if (std::string(argv[i]) == "--use_ogre") {
121 use_ogre = true;
122 }
123 else if (std::string(argv[i]) == "--use_scanline") {
124 use_scanline = true;
125 }
126 else if (std::string(argv[i]) == "--use_edges" && i + 1 < argc) {
127 use_edges = (std::atoi(argv[i + 1]) == 0 ? false : true);
128 }
129 else if (std::string(argv[i]) == "--use_klt" && i + 1 < argc) {
130 use_klt = (std::atoi(argv[i + 1]) == 0 ? false : true);
131 }
132 else if (std::string(argv[i]) == "--use_depth" && i + 1 < argc) {
133 use_depth = depthTypeFromString(std::string(argv[i + 1]));
134 }
135 else if (std::string(argv[i]) == "--learn") {
136 learn = true;
137 }
138 else if (std::string(argv[i]) == "--learning_data" && i + 1 < argc) {
139 learning_data = argv[i + 1];
140 }
141 else if (std::string(argv[i]) == "--auto_init") {
142 auto_init = true;
143 }
144 else if (std::string(argv[i]) == "--display_proj_error") {
145 display_projection_error = true;
146 }
147 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
148 std::cout << "Usage: \n"
149 << argv[0]
150 << " [--model_color <object.cao>] [--model_depth <object.cao>]"
151 " [--config_color <object.xml>] [--config_depth <object.xml>]"
152 " [--init_file <object.init>] [--use_ogre] [--use_scanline]"
153 " [--proj_error_threshold <threshold between 0 and 90> (default: "
154 << proj_error_threshold
155 << ")]"
156 " [--use_edges <0|1> (default: 1)] [--use_klt <0|1> (default: 1)] [--use_depth " + getDepthTypeList() + " (default: " + depthTypeToString(use_depth) + ")]"
157 " [--learn] [--auto_init] [--learning_data <path to .bin> (default: learning/data-learned.bin)]"
158 " [--display_proj_error]"
159 << std::endl;
160
161 std::cout << "\n** How to track a 4.2 cm width cube with manual initialization:\n"
162 << argv[0] << " --model_color model/cube/cube.cao --use_edges 1 --use_klt 1 --use_depth 1" << std::endl;
163 std::cout << "\n** How to learn the cube and create a learning database:\n"
164 << argv[0] << " --model_color model/cube/cube.cao --use_edges 1 --use_klt 1 --use_depth 1 --learn"
165 << std::endl;
166 std::cout << "\n** How to track the cube with initialization from learning database:\n"
167 << argv[0] << " --model_color model/cube/cube.cao --use_edges 1 --use_klt 1 --use_depth 1 --auto_init"
168 << std::endl;
169
170 return EXIT_SUCCESS;
171 }
172 }
173
174 if (model_depth.empty()) {
175 model_depth = model_color;
176 }
177 std::string parentname = vpIoTools::getParent(model_color);
178 if (config_color.empty()) {
179 config_color = (parentname.empty() ? "" : (parentname + "/")) + vpIoTools::getNameWE(model_color) + ".xml";
180 }
181 if (config_depth.empty()) {
182 config_depth = (parentname.empty() ? "" : (parentname + "/")) + vpIoTools::getNameWE(model_color) + "_depth.xml";
183 }
184 if (init_file.empty()) {
185 init_file = (parentname.empty() ? "" : (parentname + "/")) + vpIoTools::getNameWE(model_color) + ".init";
186 }
187 std::cout << "Tracked features: " << std::endl;
188 std::cout << " Use edges : " << use_edges << std::endl;
189 std::cout << " Use klt : " << use_klt << std::endl;
190 std::cout << " Use depth : " << depthTypeToString(use_depth) << std::endl;
191 std::cout << "Tracker options: " << std::endl;
192 std::cout << " Use ogre : " << use_ogre << std::endl;
193 std::cout << " Use scanline: " << use_scanline << std::endl;
194 std::cout << " Proj. error : " << proj_error_threshold << std::endl;
195 std::cout << " Display proj. error: " << display_projection_error << std::endl;
196 std::cout << "Config files: " << std::endl;
197 std::cout << " Config color: "
198 << "\"" << config_color << "\"" << std::endl;
199 std::cout << " Config depth: "
200 << "\"" << config_depth << "\"" << std::endl;
201 std::cout << " Model color : "
202 << "\"" << model_color << "\"" << std::endl;
203 std::cout << " Model depth : "
204 << "\"" << model_depth << "\"" << std::endl;
205 std::cout << " Init file : "
206 << "\"" << init_file << "\"" << std::endl;
207 std::cout << "Learning options : " << std::endl;
208 std::cout << " Learn : " << learn << std::endl;
209 std::cout << " Auto init : " << auto_init << std::endl;
210 std::cout << " Learning data: " << learning_data << std::endl;
211
212 if (!use_edges && !use_klt && (use_depth == DEPTH_UNUSED)) {
213 std::cout << "You must choose at least one visual features between edge, KLT and depth." << std::endl;
214 return EXIT_FAILURE;
215 }
216
217 if (config_color.empty() || config_depth.empty() || model_color.empty() || model_depth.empty() || init_file.empty()) {
218 std::cout << "config_color.empty() || config_depth.empty() || model_color.empty() || model_depth.empty() || "
219 "init_file.empty()"
220 << std::endl;
221 return EXIT_FAILURE;
222 }
223
225 ST::CaptureSessionSettings settings;
226 settings.source = ST::CaptureSessionSourceId::StructureCore;
227 settings.structureCore.visibleEnabled = true;
228 settings.applyExpensiveCorrection = true; // Apply a correction and clean filter to the depth before streaming.
229
230 try {
231 sc.open(settings);
232 }
233 catch (const vpException &e) {
234 std::cout << "Catch an exception: " << e.what() << std::endl;
235 std::cout << "Check if the Structure Core camera is connected..." << std::endl;
236 return EXIT_SUCCESS;
237 }
238
242
243 std::cout << "Sensor internal camera parameters for color camera: " << cam_color << std::endl;
244 std::cout << "Sensor internal camera parameters for depth camera: " << cam_depth << std::endl;
245
246 vpImage<vpRGBa> I_color(height, width);
247 vpImage<unsigned char> I_gray(height, width), I_depth(height, width);
248 vpImage<float> I_depth_raw(height, width);
249
250 unsigned int _posx = 100, _posy = 50;
251
252#ifdef VISP_HAVE_DISPLAY
253#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
254 std::shared_ptr<vpDisplay> display1 = vpDisplayFactory::createDisplay();
255 std::shared_ptr<vpDisplay> display2 = vpDisplayFactory::createDisplay();
256#else
259#endif
260 if (use_edges || use_klt) {
261 display1->init(I_gray, _posx, _posy, "Color stream");
262 }
263 if (use_depth != DEPTH_UNUSED) {
264 display2->init(I_depth, _posx + I_gray.getWidth() + 10, _posy, "Depth stream");
265 }
266#endif
267
268 while (true) {
269 sc.acquire((unsigned char *)I_color.bitmap, (unsigned char *)I_depth_raw.bitmap, nullptr, nullptr, nullptr);
270
271 if (use_edges || use_klt) {
272 vpImageConvert::convert(I_color, I_gray);
273 vpDisplay::display(I_gray);
274 vpDisplay::displayText(I_gray, 20, 20, "Click when ready.", vpColor::red);
275 vpDisplay::flush(I_gray);
276
277 if (vpDisplay::getClick(I_gray, false)) {
278 break;
279 }
280 }
281 if (use_depth != DEPTH_UNUSED) {
282 vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
283 vpDisplay::display(I_depth);
284 vpDisplay::displayText(I_depth, 20, 20, "Click when ready.", vpColor::red);
285 vpDisplay::flush(I_depth);
286
287 if (vpDisplay::getClick(I_depth, false)) {
288 break;
289 }
290 }
291 }
292
293 std::vector<int> trackerTypes;
294 if (use_edges && use_klt)
296 else if (use_edges)
297 trackerTypes.push_back(vpMbGenericTracker::EDGE_TRACKER);
298 else if (use_klt)
299 trackerTypes.push_back(vpMbGenericTracker::KLT_TRACKER);
300
301 if (use_depth == DEPTH_DENSE) {
302 trackerTypes.push_back(vpMbGenericTracker::DEPTH_DENSE_TRACKER);
303 }
304 else if (use_depth == DEPTH_NORMAL) {
305 trackerTypes.push_back(vpMbGenericTracker::DEPTH_NORMAL_TRACKER);
306 }
307
309 vpHomogeneousMatrix depth_M_color = color_M_depth.inverse();
310 std::map<std::string, vpHomogeneousMatrix> mapOfCameraTransformations;
311 std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
312 std::map<std::string, std::string> mapOfInitFiles;
313 std::map<std::string, const std::vector<vpColVector> *> mapOfPointclouds;
314 std::map<std::string, unsigned int> mapOfWidths, mapOfHeights;
315 std::map<std::string, vpHomogeneousMatrix> mapOfCameraPoses;
316
317 std::vector<vpColVector> pointcloud;
318
319 vpMbGenericTracker tracker(trackerTypes);
320
321 if ((use_edges || use_klt) && (use_depth != DEPTH_UNUSED)) {
322 tracker.loadConfigFile(config_color, config_depth);
323 tracker.loadModel(model_color, model_depth);
324 std::cout << "Sensor internal depth_M_color: \n" << depth_M_color << std::endl;
325 mapOfCameraTransformations["Camera2"] = depth_M_color;
326 tracker.setCameraTransformationMatrix(mapOfCameraTransformations);
327 mapOfImages["Camera1"] = &I_gray;
328 mapOfImages["Camera2"] = &I_depth;
329 mapOfInitFiles["Camera1"] = init_file;
330 tracker.setCameraParameters(cam_color, cam_depth);
331 }
332 else if (use_edges || use_klt) {
333 tracker.loadConfigFile(config_color);
334 tracker.loadModel(model_color);
335 tracker.setCameraParameters(cam_color);
336 }
337 else if (use_depth != DEPTH_UNUSED) {
338 tracker.loadConfigFile(config_depth);
339 tracker.loadModel(model_depth);
340 tracker.setCameraParameters(cam_depth);
341 }
342
343 tracker.setDisplayFeatures(true);
344 tracker.setOgreVisibilityTest(use_ogre);
345 tracker.setScanLineVisibilityTest(use_scanline);
346 tracker.setProjectionErrorComputation(true);
347 tracker.setProjectionErrorDisplay(display_projection_error);
348
349#if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
350 std::string detectorName = "SIFT";
351 std::string extractorName = "SIFT";
352 std::string matcherName = "BruteForce";
353#elif ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
354 std::string detectorName = "FAST";
355 std::string extractorName = "ORB";
356#endif
357 std::string matcherName = "BruteForce-Hamming";
358
359 vpKeyPoint keypoint;
360 if (learn || auto_init) {
361 keypoint.setDetector(detectorName);
362 keypoint.setExtractor(extractorName);
363 keypoint.setMatcher(matcherName);
364#if ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES))
365 cv::Ptr<cv::ORB> orb_detector = keypoint.getDetector("ORB").dynamicCast<cv::ORB>();
366 if (orb_detector) {
367 orb_detector->setNLevels(1);
368 }
369#endif
370 }
371
372 if (auto_init) {
373 if (!vpIoTools::checkFilename(learning_data)) {
374 std::cout << "Cannot enable auto detection. Learning file \"" << learning_data << "\" doesn't exist" << std::endl;
375 return EXIT_FAILURE;
376 }
377 keypoint.loadLearningData(learning_data, true);
378 }
379 else {
380 if ((use_edges || use_klt) && (use_depth != DEPTH_UNUSED)) {
381 tracker.initClick(mapOfImages, mapOfInitFiles, true);
382 }
383 else if (use_edges || use_klt) {
384 tracker.initClick(I_gray, init_file, true);
385 }
386 else if (use_depth != DEPTH_UNUSED) {
387 tracker.initClick(I_depth, init_file, true);
388 }
389
390 if (learn)
392 }
393
394 bool run_auto_init = false;
395 if (auto_init) {
396 run_auto_init = true;
397 }
398 std::vector<double> times_vec;
399
400 try {
401 // To be able to display keypoints matching with test-detection-rs2
402 int learn_id = 1;
403 bool quit = false;
404 bool learn_position = false;
405 double loop_t = 0;
407
408 while (!quit) {
409 double t = vpTime::measureTimeMs();
410 bool tracking_failed = false;
411
412 // Acquire images and update tracker input data
413 sc.acquire((unsigned char *)I_color.bitmap, (unsigned char *)I_depth_raw.bitmap, &pointcloud);
414
415 if (use_edges || use_klt || run_auto_init) {
416 vpImageConvert::convert(I_color, I_gray);
417 vpDisplay::display(I_gray);
418 }
419 if (use_depth != DEPTH_UNUSED) {
420 vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
421 vpDisplay::display(I_depth);
422 }
423
424 if ((use_edges || use_klt) && (use_depth != DEPTH_UNUSED)) {
425 mapOfImages["Camera1"] = &I_gray;
426 mapOfPointclouds["Camera2"] = &pointcloud;
427 mapOfWidths["Camera2"] = width;
428 mapOfHeights["Camera2"] = height;
429 }
430 else if (use_edges || use_klt) {
431 mapOfImages["Camera"] = &I_gray;
432 }
433 else if (use_depth != DEPTH_UNUSED) {
434 mapOfPointclouds["Camera"] = &pointcloud;
435 mapOfWidths["Camera"] = width;
436 mapOfHeights["Camera"] = height;
437 }
438
439 // Run auto initialization from learned data
440 if (run_auto_init) {
441 if (keypoint.matchPoint(I_gray, cam_color, cMo)) {
442 std::cout << "Auto init succeed" << std::endl;
443 if ((use_edges || use_klt) && (use_depth != DEPTH_UNUSED)) {
444 mapOfCameraPoses["Camera1"] = cMo;
445 mapOfCameraPoses["Camera2"] = depth_M_color * cMo;
446 tracker.initFromPose(mapOfImages, mapOfCameraPoses);
447 }
448 else if (use_edges || use_klt) {
449 tracker.initFromPose(I_gray, cMo);
450 }
451 else if (use_depth != DEPTH_UNUSED) {
452 tracker.initFromPose(I_depth, depth_M_color * cMo);
453 }
454 }
455 else {
456 if (use_edges || use_klt) {
457 vpDisplay::flush(I_gray);
458 }
459 if (use_depth != DEPTH_UNUSED) {
460 vpDisplay::flush(I_depth);
461 }
462 continue;
463 }
464 }
465
466 // Run the tracker
467 try {
468 if (run_auto_init) {
469 // Turn display features off just after auto init to not display wrong moving-edge if the tracker fails
470 tracker.setDisplayFeatures(true);
471
472 run_auto_init = false;
473 }
474 if ((use_edges || use_klt) && (use_depth != DEPTH_UNUSED)) {
475 tracker.track(mapOfImages, mapOfPointclouds, mapOfWidths, mapOfHeights);
476 }
477 else if (use_edges || use_klt) {
478 tracker.track(I_gray);
479 }
480 else if (use_depth != DEPTH_UNUSED) {
481 tracker.track(mapOfImages, mapOfPointclouds, mapOfWidths, mapOfHeights);
482 }
483 }
484 catch (const vpException &e) {
485 std::cout << "Tracker exception: " << e.getStringMessage() << std::endl;
486 tracking_failed = true;
487 if (auto_init) {
488 std::cout << "Tracker needs to restart (tracking exception)" << std::endl;
489 run_auto_init = true;
490 }
491 }
492
493 // Get object pose
494 cMo = tracker.getPose();
495
496 // Check tracking errors
497 double proj_error = 0;
498 if (tracker.getTrackerType() & vpMbGenericTracker::EDGE_TRACKER) {
499 // Check tracking errors
500 proj_error = tracker.getProjectionError();
501 }
502 else {
503 proj_error = tracker.computeCurrentProjectionError(I_gray, cMo, cam_color);
504 }
505
506 if (auto_init && proj_error > proj_error_threshold) {
507 std::cout << "Tracker needs to restart (projection error detected: " << proj_error << ")" << std::endl;
508 run_auto_init = true;
509 tracking_failed = true;
510 }
511
512 // Display tracking results
513 if (!tracking_failed) {
514 // Turn display features on
515 tracker.setDisplayFeatures(true);
516
517 if ((use_edges || use_klt) && (use_depth != DEPTH_UNUSED)) {
518 tracker.display(I_gray, I_depth, cMo, depth_M_color * cMo, cam_color, cam_depth, vpColor::red, 3);
519 vpDisplay::displayFrame(I_gray, cMo, cam_color, 0.05, vpColor::none, 3);
520 vpDisplay::displayFrame(I_depth, depth_M_color * cMo, cam_depth, 0.05, vpColor::none, 3);
521 }
522 else if (use_edges || use_klt) {
523 tracker.display(I_gray, cMo, cam_color, vpColor::red, 3);
524 vpDisplay::displayFrame(I_gray, cMo, cam_color, 0.05, vpColor::none, 3);
525 }
526 else if (use_depth != DEPTH_UNUSED) {
527 tracker.display(I_depth, cMo, cam_depth, vpColor::red, 3);
528 vpDisplay::displayFrame(I_depth, cMo, cam_depth, 0.05, vpColor::none, 3);
529 }
530
531 {
532 std::stringstream ss;
533 ss << "Nb features: " << tracker.getError().size();
534 vpDisplay::displayText(I_gray, I_gray.getHeight() - 50, 20, ss.str(), vpColor::red);
535 }
536 {
537 std::stringstream ss;
538 ss << "Features: edges " << tracker.getNbFeaturesEdge() << ", klt " << tracker.getNbFeaturesKlt()
539 << ", depth " << tracker.getNbFeaturesDepthDense();
540 vpDisplay::displayText(I_gray, I_gray.getHeight() - 30, 20, ss.str(), vpColor::red);
541 }
542 }
543
544 std::stringstream ss;
545 ss << "Loop time: " << loop_t << " ms";
546
548 if (use_edges || use_klt) {
549 vpDisplay::displayText(I_gray, 20, 20, ss.str(), vpColor::red);
550 if (learn)
551 vpDisplay::displayText(I_gray, 35, 20, "Left click: learn Right click: quit", vpColor::red);
552 else if (auto_init)
553 vpDisplay::displayText(I_gray, 35, 20, "Left click: auto_init Right click: quit", vpColor::red);
554 else
555 vpDisplay::displayText(I_gray, 35, 20, "Right click: quit", vpColor::red);
556
557 vpDisplay::flush(I_gray);
558
559 if (vpDisplay::getClick(I_gray, button, false)) {
560 if (button == vpMouseButton::button3) {
561 quit = true;
562 }
563 else if (button == vpMouseButton::button1 && learn) {
564 learn_position = true;
565 }
566 else if (button == vpMouseButton::button1 && auto_init && !learn) {
567 run_auto_init = true;
568 }
569 }
570 }
571 if (use_depth != DEPTH_UNUSED) {
572 vpDisplay::displayText(I_depth, 20, 20, ss.str(), vpColor::red);
573 vpDisplay::displayText(I_depth, 40, 20, "Click to quit", vpColor::red);
574 vpDisplay::flush(I_depth);
575
576 if (vpDisplay::getClick(I_depth, false)) {
577 quit = true;
578 }
579 }
580
581 if (learn_position) {
582 // Detect keypoints on the current image
583 std::vector<cv::KeyPoint> trainKeyPoints;
584 keypoint.detect(I_gray, trainKeyPoints);
585
586 // Keep only keypoints on the cube
587 std::vector<vpPolygon> polygons;
588 std::vector<std::vector<vpPoint> > roisPt;
589 std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces();
590 polygons = pair.first;
591 roisPt = pair.second;
592
593 // Compute the 3D coordinates
594 std::vector<cv::Point3f> points3f;
595 vpKeyPoint::compute3DForPointsInPolygons(cMo, cam_color, trainKeyPoints, polygons, roisPt, points3f);
596
597 // Build the reference keypoints
598 keypoint.buildReference(I_gray, trainKeyPoints, points3f, true, learn_id++);
599
600 // Display learned data
601 for (std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
602 vpDisplay::displayCross(I_gray, static_cast<int>(it->pt.y), static_cast<int>(it->pt.x), 10, vpColor::yellow, 3);
603 }
604 learn_position = false;
605 std::cout << "Data learned" << std::endl;
606 }
607 loop_t = vpTime::measureTimeMs() - t;
608 times_vec.push_back(loop_t);
609 }
610 if (learn) {
611 std::cout << "Save learning file: " << learning_data << std::endl;
612 keypoint.saveLearningData(learning_data, true, true);
613 }
614 }
615 catch (const vpException &e) {
616 std::cout << "Catch an exception: " << e.what() << std::endl;
617 }
618
619#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11) && defined(VISP_HAVE_DISPLAY)
620 if (display1 != nullptr) {
621 delete display1;
622 }
623 if (display2 != nullptr) {
624 delete display2;
625 }
626#endif
627
628 if (!times_vec.empty()) {
629 std::cout << "\nProcessing time, Mean: " << vpMath::getMean(times_vec)
630 << " ms ; Median: " << vpMath::getMedian(times_vec) << " ; Std: " << vpMath::getStdev(times_vec) << " ms"
631 << std::endl;
632 }
633
634 return EXIT_SUCCESS;
635}
636#elif defined(VISP_HAVE_OCCIPITAL_STRUCTURE)
637int main()
638{
639 std::cout << "Install OpenCV 3rd party, configure and build ViSP again to use this example" << std::endl;
640 return EXIT_SUCCESS;
641}
642#else
643int main()
644{
645 std::cout << "Install libStructure 3rd party, configure and build ViSP again to use this example" << std::endl;
646 return EXIT_SUCCESS;
647}
648#endif
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition vpColor.h:198
static const vpColor none
Definition vpColor.h:210
static const vpColor yellow
Definition vpColor.h:206
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 displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
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.
vpHomogeneousMatrix inverse() const
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string toLowerCase(const std::string &input)
Return a lower-case version of the string input . Numbers and special characters stay the same.
static bool checkFilename(const std::string &filename)
static void makeDirectory(const std::string &dirname)
static std::string getNameWE(const std::string &pathname)
static std::string getParent(const std::string &pathname)
Class that allows keypoints 2D features detection (and descriptors extraction) and matching thanks to...
Definition vpKeyPoint.h:274
unsigned int buildReference(const vpImage< unsigned char > &I) VP_OVERRIDE
void setExtractor(const vpFeatureDescriptorType &extractorType)
void loadLearningData(const std::string &filename, bool binaryMode=false, bool append=false)
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
static void compute3DForPointsInPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, std::vector< cv::KeyPoint > &candidates, const std::vector< vpPolygon > &polygons, const std::vector< std::vector< vpPoint > > &roisPt, std::vector< cv::Point3f > &points, cv::Mat *descriptors=nullptr)
void setMatcher(const std::string &matcherName)
void saveLearningData(const std::string &filename, bool binaryMode=false, bool saveTrainingImages=true)
unsigned int matchPoint(const vpImage< unsigned char > &I) VP_OVERRIDE
void setDetector(const vpFeatureDetectorType &detectorType)
cv::Ptr< cv::FeatureDetector > getDetector(const vpFeatureDetectorType &type) const
static double getMedian(const std::vector< double > &v)
Definition vpMath.cpp:343
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
Definition vpMath.cpp:374
static double getMean(const std::vector< double > &v)
Definition vpMath.cpp:323
Real-time 6D object pose tracking using its CAD model.
unsigned int getHeight(vpOccipitalStructureStream stream_type)
vpCameraParameters getCameraParameters(const vpOccipitalStructureStream stream_type, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithoutDistortion)
void acquire(vpImage< unsigned char > &gray, bool undistorted=false, double *ts=nullptr)
unsigned int getWidth(vpOccipitalStructureStream stream_type)
vpHomogeneousMatrix getTransform(const vpOccipitalStructureStream from, const vpOccipitalStructureStream to)
bool open(const ST::CaptureSessionSettings &settings)
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.
VISP_EXPORT double measureTimeMs()