surf_keypoint_matcher.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <iostream>
  2. #include "opencv2/opencv_modules.hpp"
  3. #ifdef HAVE_OPENCV_XFEATURES2D
  4. #include "opencv2/core.hpp"
  5. #include "opencv2/features2d.hpp"
  6. #include "opencv2/highgui.hpp"
  7. #include "opencv2/cudafeatures2d.hpp"
  8. #include "opencv2/xfeatures2d/cuda.hpp"
  9. using namespace std;
  10. using namespace cv;
  11. using namespace cv::cuda;
  12. static void help()
  13. {
  14. cout << "\nThis program demonstrates using SURF_CUDA features detector, descriptor extractor and BruteForceMatcher_CUDA" << endl;
  15. cout << "\nUsage:\n\tsurf_keypoint_matcher --left <image1> --right <image2>" << endl;
  16. }
  17. int main(int argc, char* argv[])
  18. {
  19. if (argc != 5)
  20. {
  21. help();
  22. return -1;
  23. }
  24. GpuMat img1, img2;
  25. for (int i = 1; i < argc; ++i)
  26. {
  27. if (string(argv[i]) == "--left")
  28. {
  29. img1.upload(imread(argv[++i], IMREAD_GRAYSCALE));
  30. CV_Assert(!img1.empty());
  31. }
  32. else if (string(argv[i]) == "--right")
  33. {
  34. img2.upload(imread(argv[++i], IMREAD_GRAYSCALE));
  35. CV_Assert(!img2.empty());
  36. }
  37. else if (string(argv[i]) == "--help")
  38. {
  39. help();
  40. return -1;
  41. }
  42. }
  43. cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
  44. SURF_CUDA surf;
  45. // detecting keypoints & computing descriptors
  46. GpuMat keypoints1GPU, keypoints2GPU;
  47. GpuMat descriptors1GPU, descriptors2GPU;
  48. surf(img1, GpuMat(), keypoints1GPU, descriptors1GPU);
  49. surf(img2, GpuMat(), keypoints2GPU, descriptors2GPU);
  50. cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl;
  51. cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl;
  52. // matching descriptors
  53. Ptr<cv::cuda::DescriptorMatcher> matcher = cv::cuda::DescriptorMatcher::createBFMatcher(surf.defaultNorm());
  54. vector<DMatch> matches;
  55. matcher->match(descriptors1GPU, descriptors2GPU, matches);
  56. // downloading results
  57. vector<KeyPoint> keypoints1, keypoints2;
  58. vector<float> descriptors1, descriptors2;
  59. surf.downloadKeypoints(keypoints1GPU, keypoints1);
  60. surf.downloadKeypoints(keypoints2GPU, keypoints2);
  61. surf.downloadDescriptors(descriptors1GPU, descriptors1);
  62. surf.downloadDescriptors(descriptors2GPU, descriptors2);
  63. // drawing the results
  64. Mat img_matches;
  65. drawMatches(Mat(img1), keypoints1, Mat(img2), keypoints2, matches, img_matches);
  66. namedWindow("matches", 0);
  67. imshow("matches", img_matches);
  68. waitKey(0);
  69. return 0;
  70. }
  71. #else
  72. int main()
  73. {
  74. std::cerr << "OpenCV was built without xfeatures2d module" << std::endl;
  75. return 0;
  76. }
  77. #endif