cvv_demo.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // system includes
  2. #include <iostream>
  3. // library includes
  4. #include <opencv2/imgproc.hpp>
  5. #include <opencv2/features2d.hpp>
  6. #include <opencv2/imgproc/types_c.h>
  7. #include <opencv2/videoio.hpp>
  8. #include <opencv2/videoio/videoio_c.h>
  9. #define CVVISUAL_DEBUGMODE
  10. #include <opencv2/cvv/debug_mode.hpp>
  11. #include <opencv2/cvv/show_image.hpp>
  12. #include <opencv2/cvv/filter.hpp>
  13. #include <opencv2/cvv/dmatch.hpp>
  14. #include <opencv2/cvv/final_show.hpp>
  15. using namespace std;
  16. using namespace cv;
  17. template<class T> std::string toString(const T& p_arg)
  18. {
  19. std::stringstream ss;
  20. ss << p_arg;
  21. return ss.str();
  22. }
  23. int
  24. main(int argc, char** argv)
  25. {
  26. cv::Size* resolution = nullptr;
  27. // parser keys
  28. const char *keys =
  29. "{ help h usage ? | | show this message }"
  30. "{ width W | 0| camera resolution width. leave at 0 to use defaults }"
  31. "{ height H | 0| camera resolution height. leave at 0 to use defaults }";
  32. CommandLineParser parser(argc, argv, keys);
  33. if (parser.has("help")) {
  34. parser.printMessage();
  35. return 0;
  36. }
  37. int res_w = parser.get<int>("width");
  38. int res_h = parser.get<int>("height");
  39. // setup video capture
  40. cv::VideoCapture capture(0);
  41. if (!capture.isOpened()) {
  42. std::cout << "Could not open VideoCapture" << std::endl;
  43. return 1;
  44. }
  45. if (res_w>0 && res_h>0) {
  46. printf("Setting resolution to %dx%d\n", res_w, res_h);
  47. capture.set(CV_CAP_PROP_FRAME_WIDTH, res_w);
  48. capture.set(CV_CAP_PROP_FRAME_HEIGHT, res_h);
  49. }
  50. cv::Mat prevImgGray;
  51. std::vector<cv::KeyPoint> prevKeypoints;
  52. cv::Mat prevDescriptors;
  53. int maxFeatureCount = 500;
  54. Ptr<ORB> detector = ORB::create(maxFeatureCount);
  55. cv::BFMatcher matcher(cv::NORM_HAMMING);
  56. for (int imgId = 0; imgId < 10; imgId++) {
  57. // capture a frame
  58. cv::Mat imgRead;
  59. capture >> imgRead;
  60. printf("%d: image captured\n", imgId);
  61. std::string imgIdString{"imgRead"};
  62. imgIdString += toString(imgId);
  63. cvv::showImage(imgRead, CVVISUAL_LOCATION, imgIdString.c_str());
  64. // convert to grayscale
  65. cv::Mat imgGray;
  66. cv::cvtColor(imgRead, imgGray, COLOR_BGR2GRAY);
  67. cvv::debugFilter(imgRead, imgGray, CVVISUAL_LOCATION, "to gray");
  68. // detect ORB features
  69. std::vector<cv::KeyPoint> keypoints;
  70. cv::Mat descriptors;
  71. detector->detectAndCompute(imgGray, cv::noArray(), keypoints, descriptors);
  72. printf("%d: detected %zd keypoints\n", imgId, keypoints.size());
  73. // match them to previous image (if available)
  74. if (!prevImgGray.empty()) {
  75. std::vector<cv::DMatch> matches;
  76. matcher.match(prevDescriptors, descriptors, matches);
  77. printf("%d: all matches size=%zd\n", imgId, matches.size());
  78. std::string allMatchIdString{"all matches "};
  79. allMatchIdString += toString(imgId-1) + "<->" + toString(imgId);
  80. cvv::debugDMatch(prevImgGray, prevKeypoints, imgGray, keypoints, matches, CVVISUAL_LOCATION, allMatchIdString.c_str());
  81. // remove worst (as defined by match distance) bestRatio quantile
  82. double bestRatio = 0.8;
  83. std::sort(matches.begin(), matches.end());
  84. matches.resize(int(bestRatio * matches.size()));
  85. printf("%d: best matches size=%zd\n", imgId, matches.size());
  86. std::string bestMatchIdString{"best " + toString(bestRatio) + " matches "};
  87. bestMatchIdString += toString(imgId-1) + "<->" + toString(imgId);
  88. cvv::debugDMatch(prevImgGray, prevKeypoints, imgGray, keypoints, matches, CVVISUAL_LOCATION, bestMatchIdString.c_str());
  89. }
  90. prevImgGray = imgGray;
  91. prevKeypoints = keypoints;
  92. prevDescriptors = descriptors;
  93. }
  94. cvv::finalShow();
  95. return 0;
  96. }