Path: blob/master/samples/gpu/surf_keypoint_matcher.cpp
16337 views
#include <iostream>12#include "opencv2/opencv_modules.hpp"34#ifdef HAVE_OPENCV_XFEATURES2D56#include "opencv2/core.hpp"7#include "opencv2/features2d.hpp"8#include "opencv2/highgui.hpp"9#include "opencv2/cudafeatures2d.hpp"10#include "opencv2/xfeatures2d/cuda.hpp"1112using namespace std;13using namespace cv;14using namespace cv::cuda;1516static void help()17{18cout << "\nThis program demonstrates using SURF_CUDA features detector, descriptor extractor and BruteForceMatcher_CUDA" << endl;19cout << "\nUsage:\n\tsurf_keypoint_matcher --left <image1> --right <image2>" << endl;20}2122int main(int argc, char* argv[])23{24if (argc != 5)25{26help();27return -1;28}2930GpuMat img1, img2;31for (int i = 1; i < argc; ++i)32{33if (string(argv[i]) == "--left")34{35img1.upload(imread(argv[++i], IMREAD_GRAYSCALE));36CV_Assert(!img1.empty());37}38else if (string(argv[i]) == "--right")39{40img2.upload(imread(argv[++i], IMREAD_GRAYSCALE));41CV_Assert(!img2.empty());42}43else if (string(argv[i]) == "--help")44{45help();46return -1;47}48}4950cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());5152SURF_CUDA surf;5354// detecting keypoints & computing descriptors55GpuMat keypoints1GPU, keypoints2GPU;56GpuMat descriptors1GPU, descriptors2GPU;57surf(img1, GpuMat(), keypoints1GPU, descriptors1GPU);58surf(img2, GpuMat(), keypoints2GPU, descriptors2GPU);5960cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl;61cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl;6263// matching descriptors64Ptr<cv::cuda::DescriptorMatcher> matcher = cv::cuda::DescriptorMatcher::createBFMatcher(surf.defaultNorm());65vector<DMatch> matches;66matcher->match(descriptors1GPU, descriptors2GPU, matches);6768// downloading results69vector<KeyPoint> keypoints1, keypoints2;70vector<float> descriptors1, descriptors2;71surf.downloadKeypoints(keypoints1GPU, keypoints1);72surf.downloadKeypoints(keypoints2GPU, keypoints2);73surf.downloadDescriptors(descriptors1GPU, descriptors1);74surf.downloadDescriptors(descriptors2GPU, descriptors2);7576// drawing the results77Mat img_matches;78drawMatches(Mat(img1), keypoints1, Mat(img2), keypoints2, matches, img_matches);7980namedWindow("matches", 0);81imshow("matches", img_matches);82waitKey(0);8384return 0;85}8687#else8889int main()90{91std::cerr << "OpenCV was built without xfeatures2d module" << std::endl;92return 0;93}9495#endif969798