您的位置:首页 > 运维架构

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

2014-05-22 10:23 603 查看
参考了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);//显示的标题为Matches
waitKey(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);//显示的标题为Matches
waitKey(6000);

}

main:
int main()
{
string file1 = "test.png";
string file2 = "test_t.png";

Sift(file1,file2);
Surf(file1,file2);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: