Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created December 9, 2011 13:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yoggy/1451463 to your computer and use it in GitHub Desktop.
Save yoggy/1451463 to your computer and use it in GitHub Desktop.
OpenCV cv::gpu::SURF_GPU & cv::gpu::BruteForceMatcher_GPU sample
#include <SDKDDKVer.h>
#include <Windows.h>
#pragma warning(disable:4819)
#pragma warning(disable:4996)
// for OpenCV2
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/gpu/gpu.hpp"
#include "opencv2/highgui/highgui.hpp"
#ifdef _DEBUG
#pragma comment(lib, "opencv_core231d.lib")
#pragma comment(lib, "opencv_imgproc231d.lib")
#pragma comment(lib, "opencv_objdetect231d.lib")
#pragma comment(lib, "opencv_gpu231d.lib")
#pragma comment(lib, "opencv_features2d231d.lib")
#pragma comment(lib, "opencv_highgui231d.lib")
#else
#pragma comment(lib, "opencv_core231.lib")
#pragma comment(lib, "opencv_imgproc231.lib")
#pragma comment(lib, "opencv_objdetect231.lib")
#pragma comment(lib, "opencv_objdetect231d.lib")
#pragma comment(lib, "opencv_gpu231.lib")
#pragma comment(lib, "opencv_features2d231.lib")
#pragma comment(lib, "opencv_highgui231.lib")
#endif
int main(int argc, char* argv[])
{
cv::Mat src_img, template_img;
cv::Mat result_mat;
cv::Mat debug_img;
template_img = cv::imread("..\\img\\lena_face.png", CV_LOAD_IMAGE_GRAYSCALE);
if (template_img.data == NULL) {
printf("cv::imread() failed...\n");
return -1;
}
src_img = cv::imread("..\\img\\lena.png", CV_LOAD_IMAGE_GRAYSCALE);
if (src_img.data == NULL) {
printf("cv::imread() failed...\n");
return -1;
}
cv::gpu::GpuMat src_gpu, template_gpu;
cv::gpu::GpuMat src_keypoints_gpu, src_descriptors_gpu;
cv::gpu::GpuMat template_keypoints_gpu, template_descriptors_gpu;
cv::gpu::SURF_GPU surf;
std::vector<cv::KeyPoint> src_keypoints, template_keypoints;
std::vector<float> src_descriptors, template_descriptors;
std::vector<std::vector<cv::DMatch>> matches;
while(true) {
// GPUへ画像をアップロード
src_gpu.upload(src_img);
template_gpu.upload(template_img);
// 特徴点の抽出
surf(src_gpu, cv::gpu::GpuMat(), src_keypoints_gpu, src_descriptors_gpu, false);
surf(template_gpu, cv::gpu::GpuMat(), template_keypoints_gpu, template_descriptors_gpu, false);
// GPUから結果を取り出す
surf.downloadKeypoints(src_keypoints_gpu, src_keypoints);
surf.downloadKeypoints(template_keypoints_gpu, template_keypoints);
surf.downloadDescriptors(src_descriptors_gpu, src_descriptors);
surf.downloadDescriptors(template_descriptors_gpu, template_descriptors);
// 2つの特徴点のうち、一致する特徴点を探す
cv::gpu::BruteForceMatcher_GPU<cv::L2<float>> matcher;
cv::gpu::GpuMat trainIdx, distance;
matcher.radiusMatch(src_descriptors_gpu, template_descriptors_gpu, matches, 0.1f);
// 描画
cv::drawMatches(src_img, src_keypoints, template_img, template_keypoints, matches, debug_img);
cv::imshow("debug_img", debug_img);
int c = cv::waitKey(1);
if (c == 27) break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment