1 #include "highgui/highgui.hpp" 2 #include "opencv2/nonfree/nonfree.hpp" 3 #include "opencv2/legacy/legacy.hpp" 4 #include <iostream> 5 6 using namespace cv; 7 using namespace std; 8 9 10 int main() 11 { 12 Mat image01 = imread("C:\2.jpg",1); //右图 13 Mat image02 = imread("C:\1.jpg", 1); //左图 14 namedWindow("p2", 0); 15 namedWindow("p1", 0); 16 imshow("p2", image01); 17 imshow("p1", image02); 18 19 //灰度图转换 20 Mat image1, image2; 21 cvtColor(image01, image1, CV_RGB2GRAY); 22 cvtColor(image02, image2, CV_RGB2GRAY); 23 24 25 //提取特征点 26 SurfFeatureDetector surfDetector(12000); // 海塞矩阵阈值,在这里调整精度,值越大点越少,越精准 27 vector<KeyPoint> keyPoint1, keyPoint2; 28 surfDetector.detect(image1, keyPoint1); 29 surfDetector.detect(image2, keyPoint2); 30 31 //特征点描述,为下边的特征点匹配做准备 32 SurfDescriptorExtractor SurfDescriptor; 33 Mat imageDesc1, imageDesc2; 34 SurfDescriptor.compute(image1, keyPoint1, imageDesc1); 35 SurfDescriptor.compute(image2, keyPoint2, imageDesc2); 36 37 //获得匹配特征点,并提取最优配对 38 FlannBasedMatcher matcher; 39 vector<DMatch> matchePoints; 40 41 matcher.match(imageDesc1, imageDesc2, matchePoints, Mat()); 42 cout << "total match points: " << matchePoints.size() << endl; 43 44 45 Mat img_match; 46 drawMatches(image01, keyPoint1, image02, keyPoint2, matchePoints, img_match); 47 namedWindow("match", 0); 48 imshow("match", img_match); 49 imwrite("match.jpg", img_match); 50 51 waitKey(); 52 return 0; 53 }