surf_keypoint_matcher.cpp 2.62 KB
Newer Older
wester committed
1 2 3 4
#include <iostream>

#include "opencv2/opencv_modules.hpp"

wester committed
5
#ifdef HAVE_OPENCV_NONFREE
wester committed
6

a  
Kai Westerkamp committed
7 8 9
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
wester committed
10 11
#include "opencv2/gpu/gpu.hpp"
#include "opencv2/nonfree/gpu.hpp"
wester committed
12 13 14

using namespace std;
using namespace cv;
wester committed
15
using namespace cv::gpu;
wester committed
16 17 18

static void help()
{
wester committed
19
    cout << "\nThis program demonstrates using SURF_GPU features detector, descriptor extractor and BruteForceMatcher_GPU" << endl;
a  
Kai Westerkamp committed
20
    cout << "\nUsage:\n\tmatcher_simple_gpu --left <image1> --right <image2>" << endl;
wester committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
}

int main(int argc, char* argv[])
{
    if (argc != 5)
    {
        help();
        return -1;
    }

    GpuMat img1, img2;
    for (int i = 1; i < argc; ++i)
    {
        if (string(argv[i]) == "--left")
        {
wester committed
36
            img1.upload(imread(argv[++i], CV_LOAD_IMAGE_GRAYSCALE));
wester committed
37 38 39 40
            CV_Assert(!img1.empty());
        }
        else if (string(argv[i]) == "--right")
        {
wester committed
41
            img2.upload(imread(argv[++i], CV_LOAD_IMAGE_GRAYSCALE));
wester committed
42 43 44 45 46 47 48 49 50
            CV_Assert(!img2.empty());
        }
        else if (string(argv[i]) == "--help")
        {
            help();
            return -1;
        }
    }

wester committed
51
    cv::gpu::printShortCudaDeviceInfo(cv::gpu::getDevice());
wester committed
52

wester committed
53
    SURF_GPU surf;
wester committed
54 55 56 57 58 59 60 61 62 63 64

    // detecting keypoints & computing descriptors
    GpuMat keypoints1GPU, keypoints2GPU;
    GpuMat descriptors1GPU, descriptors2GPU;
    surf(img1, GpuMat(), keypoints1GPU, descriptors1GPU);
    surf(img2, GpuMat(), keypoints2GPU, descriptors2GPU);

    cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl;
    cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl;

    // matching descriptors
wester committed
65 66 67
    BFMatcher_GPU matcher(NORM_L2);
    GpuMat trainIdx, distance;
    matcher.matchSingle(descriptors1GPU, descriptors2GPU, trainIdx, distance);
wester committed
68 69 70 71

    // downloading results
    vector<KeyPoint> keypoints1, keypoints2;
    vector<float> descriptors1, descriptors2;
wester committed
72
    vector<DMatch> matches;
wester committed
73 74 75 76
    surf.downloadKeypoints(keypoints1GPU, keypoints1);
    surf.downloadKeypoints(keypoints2GPU, keypoints2);
    surf.downloadDescriptors(descriptors1GPU, descriptors1);
    surf.downloadDescriptors(descriptors2GPU, descriptors2);
wester committed
77
    BFMatcher_GPU::matchDownload(trainIdx, distance, matches);
wester committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    // drawing the results
    Mat img_matches;
    drawMatches(Mat(img1), keypoints1, Mat(img2), keypoints2, matches, img_matches);

    namedWindow("matches", 0);
    imshow("matches", img_matches);
    waitKey(0);

    return 0;
}

#else

int main()
{
wester committed
94
    std::cerr << "OpenCV was built without nonfree module" << std::endl;
wester committed
95 96 97 98
    return 0;
}

#endif