gms_matcher.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <iostream>
  2. #include <opencv2/core.hpp>
  3. #include <opencv2/videoio.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <opencv2/imgproc.hpp>
  6. #include <opencv2/features2d.hpp>
  7. #include <opencv2/flann.hpp>
  8. #include <opencv2/xfeatures2d.hpp>
  9. using namespace cv;
  10. using namespace cv::xfeatures2d;
  11. ////////////////////////////////////////////////////
  12. // This program demonstrates the GMS matching strategy.
  13. int main(int argc, char* argv[])
  14. {
  15. const char* keys =
  16. "{ h help | | print help message }"
  17. "{ l left | | specify left (reference) image }"
  18. "{ r right | | specify right (query) image }"
  19. "{ camera | 0 | specify the camera device number }"
  20. "{ nfeatures | 10000 | specify the maximum number of ORB features }"
  21. "{ fastThreshold | 20 | specify the FAST threshold }"
  22. "{ drawSimple | true | do not draw not matched keypoints }"
  23. "{ withRotation | false | take rotation into account }"
  24. "{ withScale | false | take scale into account }";
  25. CommandLineParser cmd(argc, argv, keys);
  26. if (cmd.has("help"))
  27. {
  28. std::cout << "Usage: gms_matcher [options]" << std::endl;
  29. std::cout << "Available options:" << std::endl;
  30. cmd.printMessage();
  31. return EXIT_SUCCESS;
  32. }
  33. Ptr<Feature2D> orb = ORB::create(cmd.get<int>("nfeatures"));
  34. orb.dynamicCast<cv::ORB>()->setFastThreshold(cmd.get<int>("fastThreshold"));
  35. Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
  36. if (!cmd.get<String>("left").empty() && !cmd.get<String>("right").empty())
  37. {
  38. Mat imgL = imread(cmd.get<String>("left"));
  39. Mat imgR = imread(cmd.get<String>("right"));
  40. std::vector<KeyPoint> kpRef, kpCur;
  41. Mat descRef, descCur;
  42. orb->detectAndCompute(imgL, noArray(), kpRef, descRef);
  43. orb->detectAndCompute(imgR, noArray(), kpCur, descCur);
  44. std::vector<DMatch> matchesAll, matchesGMS;
  45. matcher->match(descCur, descRef, matchesAll);
  46. matchGMS(imgR.size(), imgL.size(), kpCur, kpRef, matchesAll, matchesGMS, cmd.get<bool>("withRotation"), cmd.get<bool>("withScale"));
  47. std::cout << "matchesGMS: " << matchesGMS.size() << std::endl;
  48. Mat frameMatches;
  49. if (cmd.get<bool>("drawSimple"))
  50. drawMatches(imgR, kpCur, imgL, kpRef, matchesGMS, frameMatches, Scalar::all(-1), Scalar::all(-1),
  51. std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
  52. else
  53. drawMatches(imgR, kpCur, imgL, kpRef, matchesGMS, frameMatches);
  54. imshow("Matches GMS", frameMatches);
  55. waitKey();
  56. }
  57. else
  58. {
  59. std::vector<KeyPoint> kpRef;
  60. Mat descRef;
  61. VideoCapture capture(cmd.get<int>("camera"));
  62. //Camera warm-up
  63. for (int i = 0; i < 10; i++)
  64. {
  65. Mat frame;
  66. capture >> frame;
  67. }
  68. Mat frameRef;
  69. for (;;)
  70. {
  71. Mat frame;
  72. capture >> frame;
  73. if (frameRef.empty())
  74. {
  75. frame.copyTo(frameRef);
  76. orb->detectAndCompute(frameRef, noArray(), kpRef, descRef);
  77. }
  78. TickMeter tm;
  79. tm.start();
  80. std::vector<KeyPoint> kp;
  81. Mat desc;
  82. orb->detectAndCompute(frame, noArray(), kp, desc);
  83. tm.stop();
  84. double t_orb = tm.getTimeMilli();
  85. tm.reset();
  86. tm.start();
  87. std::vector<DMatch> matchesAll, matchesGMS;
  88. matcher->match(desc, descRef, matchesAll);
  89. tm.stop();
  90. double t_match = tm.getTimeMilli();
  91. matchGMS(frame.size(), frameRef.size(), kp, kpRef, matchesAll, matchesGMS, cmd.get<bool>("withRotation"), cmd.get<bool>("withScale"));
  92. tm.stop();
  93. Mat frameMatches;
  94. if (cmd.get<bool>("drawSimple"))
  95. drawMatches(frame, kp, frameRef, kpRef, matchesGMS, frameMatches, Scalar::all(-1), Scalar::all(-1),
  96. std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
  97. else
  98. drawMatches(frame, kp, frameRef, kpRef, matchesGMS, frameMatches);
  99. String label = format("ORB: %.2f ms", t_orb);
  100. putText(frameMatches, label, Point(20, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,255));
  101. label = format("Matching: %.2f ms", t_match);
  102. putText(frameMatches, label, Point(20, 40), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,255));
  103. label = format("GMS matching: %.2f ms", tm.getTimeMilli());
  104. putText(frameMatches, label, Point(20, 60), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,255));
  105. putText(frameMatches, "Press r to reinitialize the reference image.", Point(frameMatches.cols-380, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,255));
  106. putText(frameMatches, "Press esc to quit.", Point(frameMatches.cols-180, 40), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,255));
  107. imshow("Matches GMS", frameMatches);
  108. int c = waitKey(30);
  109. if (c == 27)
  110. break;
  111. else if (c == 'r')
  112. {
  113. frame.copyTo(frameRef);
  114. orb->detectAndCompute(frameRef, noArray(), kpRef, descRef);
  115. }
  116. }
  117. }
  118. return EXIT_SUCCESS;
  119. }