1 #include "stdafx.h" 2 #include <stdio.h> 3 #include <iostream> 4 #include <opencv2/opencv.hpp> 5 #include <opencv2features2dfeatures2d.hpp> 6 #include <opencv2 onfree onfree.hpp> 7 #include <opencv2legacylegacy.hpp> 8 #include <opencv2/core/core.hpp>//因为在属性中已经配置了opencv等目录,所以把其当成了本地目录一样 9 #include <opencv2/features2d/features2d.hpp> 10 #include <opencv2/highgui/highgui.hpp> 11 12 using namespace cv; 13 using namespace std; 14 15 void readme(); 16 17 int main(int argc,char* argv[]) 18 { 19 Mat img_1=imread("C:\Users\Bite07\Desktop\1.3.jpg",CV_LOAD_IMAGE_GRAYSCALE);//宏定义时CV_LOAD_IMAGE_GRAYSCALE=0,也就是读取灰度图像 20 Mat img_2=imread("C:\Users\Bite07\Desktop\4.jpg",CV_LOAD_IMAGE_GRAYSCALE);//一定要记得这里路径的斜线方向,这与Matlab里面是相反的 21 22 if(!img_1.data || !img_2.data)//如果数据为空 23 { 24 cout<<"opencv error"<<endl; 25 return -1; 26 } 27 cout<<"open right"<<endl; 28 29 //第一步,用SIFT算子检测关键点 30 SiftFeatureDetector detector;//构造函数采用内部默认的 31 std::vector<KeyPoint> keypoints_1,keypoints_2;//构造2个专门由点组成的点向量用来存储特征点 32 33 detector.detect(img_1,keypoints_1);//将img_1图像中检测到的特征点存储起来放在keypoints_1中 34 detector.detect(img_2,keypoints_2);//同理 35 36 //在图像中画出特征点 37 Mat img_keypoints_1,img_keypoints_2; 38 39 drawKeypoints(img_1,keypoints_1,img_keypoints_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT);//在内存中画出特征点 40 drawKeypoints(img_2,keypoints_2,img_keypoints_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT); 41 42 imshow("sift_keypoints_1",img_keypoints_1);//显示特征点 43 imshow("sift_keypoints_2",img_keypoints_2); 44 45 //计算特征向量 46 SiftDescriptorExtractor extractor;//定义描述子对象 47 48 Mat descriptors_1,descriptors_2;//存放特征向量的矩阵 49 50 extractor.compute(img_1,keypoints_1,descriptors_1);//计算特征向量 51 extractor.compute(img_2,keypoints_2,descriptors_2); 52 53 //用burte force进行匹配特征向量 54 BruteForceMatcher<L2<float>>matcher;//定义一个burte force matcher对象 55 vector<DMatch>matches; 56 matcher.match(descriptors_1,descriptors_2,matches); 57 58 //绘制匹配线段 59 Mat img_matches; 60 drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches);//将匹配出来的结果放入内存img_matches中 61 62 //显示匹配线段 63 imshow("sift_Matches",img_matches);//显示的标题为Matches 64 waitKey(0); 65 return 0; 66 }
参考至:http://www.cnblogs.com/tornadomeet/archive/2012/03/08/2384843.html