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