运行环境 VS2015+opencv2.4.9
#include "stdafx.h"
#include<vector>
#include<opencv2\opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2\nonfree\nonfree.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
initModule_nonfree();
Mat imgL = imread("5.jpg");
Mat imgR = imread("6.jpg");
Ptr<FeatureDetector> fast = FeatureDetector::create("FAST");
vector<KeyPoint> keypointL,keypointR;
fast->detect(imgL, keypointL);
fast->detect(imgR, keypointR);
Mat scrimgL,srcimgR;
drawKeypoints(imgL, keypointL, scrimgL, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
drawKeypoints(imgR, keypointR, srcimgR, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
imshow("结果图左", scrimgL);
imshow("结果图右", srcimgR);
waitKey(0);
Mat descriptorsL, descriptorsR;
Ptr<DescriptorExtractor> de = DescriptorExtractor::create("SURF");
de->compute(imgL, keypointL, descriptorsL);
de->compute(imgR, keypointR, descriptorsR);
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
vector< DMatch > matches;
matcher->match(descriptorsL, descriptorsR, matches);
double max_dist = 0; double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for (int i = 0; i < descriptorsL.rows; i++)
{
double dist = matches[i].distance;
if (dist < min_dist) min_dist = dist;
if (dist > max_dist) max_dist = dist;
}
//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist )
//-- PS.- radiusMatch can also be used here.
vector< DMatch > good_matches;
for (int i = 0; i < descriptorsL.rows; i++)
{
if (matches[i].distance < 2 * min_dist)
{
good_matches.push_back(matches[i]);
}
}
//-- Draw only "good" matches
Mat img_matches;
drawMatches(imgL, keypointL, imgR, keypointR,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//-- Show detected matches
imshow("Good Matches", img_matches);
waitKey(0);
return 0;
}