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