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

SIFT匹配算法分析(Practical OpenCV)

2014-04-13 23:20 393 查看
1、源程序
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/nonfree/features2d.hpp>
#include<opencv2/features2d/features2d.hpp>

using namespace std;
using namespace cv;

int main()
{
Mat train=imread("OpticalFlow0.jpg"),train_g;
cvtColor(train,train_g,CV_RGB2GRAY);

vector<KeyPoint> train_kp;
Mat train_desc;

SiftFeatureDetector featureDetector;
featureDetector.detect(train_g,train_kp);
SiftDescriptorExtractor featureExtractor;
featureExtractor.compute(train_g,train_kp,train_desc);

BFMatcher matcher(NORM_L2);
vector<Mat> train_desc_collection(1,train_desc);//??????????
matcher.add(train_desc_collection);
matcher.train();

Mat test=imread("OpticalFlow1.jpg");//初始化
Mat test_g;

cvtColor(test,test_g,CV_RGB2GRAY);

vector<KeyPoint> test_kp;
Mat  test_desc;
featureDetector.detect(test_g,test_kp);
featureExtractor.compute(test_g,test_kp,test_desc);
vector<vector<DMatch> > matches;

matcher.knnMatch(test_desc,matches,2);

vector<DMatch> good_matches;
for(int i=0;i<matches.size();i++)
{
if(matches[i][0].distance<0.6*matches[i][1].distance)
good_matches.push_back(matches[i][0]);

}

Mat img_show;
drawMatches(test,test_kp,train,train_kp,good_matches,img_show);
imshow("Matches",img_show);

waitKey();

return 1;

}


2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: