1. 程式人生 > >opencv3.2 SURF實現特徵點匹配

opencv3.2 SURF實現特徵點匹配

opencv3.2中SurfFeatureDetector、SurfDescriptorExtractor、BruteForceMatcher這三個的使用方法已經和原先2.4版本前不一樣了。

使用方法示例如下:

 Ptr<SURF> detector = SURF::create(minHessian);
 detector->detect(img_1, keypoints_1);

 Ptr<SURF> extractor = SURF::create();
 extractor->compute(img_1, keypoints_1, descriptors_1);

Ptr<
DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce");//這裡填寫使用的匹配方式 matcher->match(descriptors_1, descriptors_2, matches);

debug版本detect函式執行時會報錯,記憶體訪問錯誤什麼的,,,,,
具體原因還不知道,網上查詢資料修改圖片type為CV_8U,和給vector手動分配空間,實測沒有用,但是改為release版本可以使用

程式碼:

#include<opencv2/features2d/features2d.hpp>
#include<opencv2/opencv.hpp> #include<opencv2/highgui/highgui.hpp> #include<opencv2/xfeatures2d/nonfree.hpp> #include<opencv2/core/core.hpp> #include<iostream> using namespace std; using namespace cv; using namespace cv::xfeatures2d; int main() { Mat srcImage1 = imread("3.jpg"
, 1); Mat srcImage2 = imread("4.jpg",1); if (!srcImage1.data || !srcImage2.data) { cout << "讀取圖片出錯" << endl; return false; } imshow("原始圖1",srcImage1); imshow("原始圖2", srcImage2); int minHessian = 100; Ptr<SurfFeatureDetector> detector = SurfFeatureDetector::create(minHessian); vector<cv::KeyPoint> key_points_1, key_points_2; Mat dstImage1, dstImage2; detector->detectAndCompute(srcImage1,Mat(), key_points_1,dstImage1); detector->detectAndCompute(srcImage2,Mat(), key_points_2,dstImage2);//可以分成detect和compute Mat img_keypoints_1, img_keypoints_2; drawKeypoints(srcImage1,key_points_1,img_keypoints_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT); drawKeypoints(srcImage2, key_points_2, img_keypoints_2, Scalar::all(-1),DrawMatchesFlags::DEFAULT); Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased"); vector<DMatch>mach; matcher->match(dstImage1,dstImage2,mach); double Max_dist = 0; double Min_dist = 100; for (int i = 0; i < dstImage1.rows; i++) { double dist = mach[i].distance; if (dist < Min_dist)Min_dist = dist; if (dist > Max_dist)Max_dist = dist; } cout << "最短距離" << Min_dist << endl; cout << "最長距離" << Max_dist << endl; vector<DMatch>goodmaches; for (int i = 0; i < dstImage1.rows; i++) { if (mach[i].distance < 2 * Min_dist) goodmaches.push_back(mach[i]); } Mat img_maches; drawMatches(srcImage1,key_points_1,srcImage2,key_points_2,goodmaches,img_maches); for (int i = 0; i < goodmaches.size(); i++) { cout << "符合條件的匹配:" << goodmaches[i].queryIdx << "--" << goodmaches[i].trainIdx << endl; } imshow("效果圖1", img_keypoints_1); imshow("效果圖2", img_keypoints_2); imshow("匹配效果",img_maches); waitKey(0); return 0; }

特徵檢測
匹配效果
這裡寫圖片描述