opencv/samples/gpu/surf_keypoint_matcher.cpp

99 lines
2.6 KiB
C++
Raw Normal View History

2012-10-17 07:18:30 +08:00
#include <iostream>
#include "opencv2/opencv_modules.hpp"
2013-03-18 20:14:37 +08:00
#ifdef HAVE_OPENCV_NONFREE
2012-10-17 07:18:30 +08:00
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/cudafeatures2d.hpp"
2013-07-24 16:01:34 +08:00
#include "opencv2/nonfree/cuda.hpp"
2012-10-17 07:18:30 +08:00
using namespace std;
using namespace cv;
2013-08-28 19:45:13 +08:00
using namespace cv::cuda;
2012-10-17 07:18:30 +08:00
static void help()
{
2013-07-24 17:55:18 +08:00
cout << "\nThis program demonstrates using SURF_CUDA features detector, descriptor extractor and BruteForceMatcher_CUDA" << endl;
2012-10-17 07:18:30 +08:00
cout << "\nUsage:\n\tmatcher_simple_gpu --left <image1> --right <image2>" << endl;
}
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")
{
img1.upload(imread(argv[++i], IMREAD_GRAYSCALE));
2012-10-17 07:18:30 +08:00
CV_Assert(!img1.empty());
}
else if (string(argv[i]) == "--right")
{
img2.upload(imread(argv[++i], IMREAD_GRAYSCALE));
2012-10-17 07:18:30 +08:00
CV_Assert(!img2.empty());
}
else if (string(argv[i]) == "--help")
{
help();
return -1;
}
}
2013-08-28 19:45:13 +08:00
cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
2012-10-17 07:18:30 +08:00
2013-07-24 17:55:18 +08:00
SURF_CUDA surf;
2012-10-17 07:18:30 +08:00
// 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
BFMatcher_CUDA matcher(surf.defaultNorm());
2012-10-17 07:18:30 +08:00
GpuMat trainIdx, distance;
matcher.matchSingle(descriptors1GPU, descriptors2GPU, trainIdx, distance);
// downloading results
vector<KeyPoint> keypoints1, keypoints2;
vector<float> descriptors1, descriptors2;
vector<DMatch> matches;
surf.downloadKeypoints(keypoints1GPU, keypoints1);
surf.downloadKeypoints(keypoints2GPU, keypoints2);
surf.downloadDescriptors(descriptors1GPU, descriptors1);
surf.downloadDescriptors(descriptors2GPU, descriptors2);
2013-07-24 17:55:18 +08:00
BFMatcher_CUDA::matchDownload(trainIdx, distance, matches);
2012-10-17 07:18:30 +08:00
// 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()
{
2013-03-18 20:14:37 +08:00
std::cerr << "OpenCV was built without nonfree module" << std::endl;
return 0;
}
#endif