使用Opencv进行图像特征点检查与匹配

来源:互联网 发布:同花顺最新软件下载 编辑:程序博客网 时间:2024/05/06 10:53

参考了http://www.cnblogs.com/tornadomeet/archive/2012/03/08/2384843.html

因为使用的OpenCV2.4.9,所以有些头文件不对。

SiftFeatureDetector,SiftDescriptorExtractor,SurfFeatureDetector,SurfDescriptorExtractor这四个类应该是在<opencv2/nonfree/features2d.hpp>中,BruteForceMatcher应该是在<opencv2/legacy/legacy.hpp>

头文件:

#include<iostream>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/features2d/features2d.hpp>#include <opencv2/nonfree/features2d.hpp>#include <vector>#include <opencv2/legacy/legacy.hpp>using namespace cv;using namespace std;



Sift特征检测与匹配:

int Sift(const string &file1,const string &file2){Mat img1 = imread(file1,CV_LOAD_IMAGE_GRAYSCALE);Mat img2 = imread(file2,CV_LOAD_IMAGE_GRAYSCALE);if(!img1.data || !img2.data){cout<<"opencv error"<<endl;return -1;}cout <<"Open pictures successfully"<<endl;SiftFeatureDetector detector;vector<KeyPoint> keyPoints_1,keyPoints_2;detector.detect(img1,keyPoints_1);detector.detect(img2,keyPoints_2);Mat img_keypoints_1,img_keypoints_2;drawKeypoints(img1,keyPoints_1,img_keypoints_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT);drawKeypoints(img2,keyPoints_2,img_keypoints_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT);cvNamedWindow("sift_keypoints_1");cvNamedWindow("sift_keypoints_2");imshow("sift_keypoints_1",img_keypoints_1);//显示特征点imshow("sift_keypoints_2",img_keypoints_2);SiftDescriptorExtractor extractor;//定义描述子对象Mat descriptors_1,descriptors_2;//存放特征向量的矩阵extractor.compute(img1,keyPoints_1,descriptors_1);//计算特征向量extractor.compute(img2,keyPoints_2,descriptors_2);//用burte force进行匹配特征向量BruteForceMatcher<L2<float>> matcher;//定义一个burte force matcher对象vector<DMatch>matches;matcher.match(descriptors_1,descriptors_2,matches);//绘制匹配线段Mat img_matches;drawMatches(img1,keyPoints_1,img2,keyPoints_2,matches,img_matches);//将匹配出来的结果放入内存img_matches中//显示匹配线段cvNamedWindow("sift_Matches");imshow("sift_Matches",img_matches);//显示的标题为MatcheswaitKey(6000);}
Surf 特征检测与匹配:

int Surf(const string &file1,const string &file2){Mat img1 = imread(file1,CV_LOAD_IMAGE_GRAYSCALE);Mat img2 = imread(file2,CV_LOAD_IMAGE_GRAYSCALE);if(!img1.data || !img2.data){cout<<"opencv error"<<endl;return -1;}cout <<"Open pictures successfully"<<endl;SurfFeatureDetector detector;vector<KeyPoint> keyPoints_1,keyPoints_2;detector.detect(img1,keyPoints_1);detector.detect(img2,keyPoints_2);Mat img_keypoints_1,img_keypoints_2;drawKeypoints(img1,keyPoints_1,img_keypoints_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT);drawKeypoints(img2,keyPoints_2,img_keypoints_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT);cvNamedWindow("surf_keypoints_1");cvNamedWindow("surf_keypoints_2");imshow("surf_keypoints_1",img_keypoints_1);//显示特征点imshow("surf_keypoints_2",img_keypoints_2);SurfDescriptorExtractor extractor;//定义描述子对象Mat descriptors_1,descriptors_2;//存放特征向量的矩阵extractor.compute(img1,keyPoints_1,descriptors_1);//计算特征向量extractor.compute(img2,keyPoints_2,descriptors_2);//用burte force进行匹配特征向量BruteForceMatcher<L2<float>> matcher;//定义一个burte force matcher对象vector<DMatch>matches;matcher.match(descriptors_1,descriptors_2,matches);//绘制匹配线段Mat img_matches;drawMatches(img1,keyPoints_1,img2,keyPoints_2,matches,img_matches);//将匹配出来的结果放入内存img_matches中//显示匹配线段cvNamedWindow("surf_Matches");imshow("surf_Matches",img_matches);//显示的标题为MatcheswaitKey(6000);}

main:

int main(){string file1 = "test.png";string file2 = "test_t.png";Sift(file1,file2);Surf(file1,file2);return 0;}


0 0
原创粉丝点击