#include <opencv2/opencv.hpp>
#include <istream>
using namespace std;
using namespace cv;
int main()
{
//read the two input images
Mat image1 = imread("image1.jpg");
Mat image2 = imread("image2.jpg");
//if failed
if(image1.empty()||image2.empty())
{
cout<<"error,the image is not exist"<<endl;
return -1;
}
//difine a sift detector
SiftFeatureDetector siftDetector;
//store key points
vector<KeyPoint> keypoint1,keypoint2;
//detect image with SIFT,get key points
siftDetector.detect(image1,keypoint1);
Mat outImage1;
//draw key points at the out image and show to the user
drawKeypoints(image1,keypoint1,outImage1,Scalar(255,0,0));
imshow("original_image1",image1);
imshow("sift_image1",outImage1);
Mat outImage2;
siftDetector.detect(image2,keypoint2);
drawKeypoints(image2,keypoint2,outImage2,Scalar(255,0,0));
imshow("sift_image2.jpg",outImage2);
//imwrite("sift_result2.jpg",outImage2);
//store 10 keypoints in order to watch the effect clearly
vector<KeyPoint> keypoint3,keypoint4;
for(int i=0;i<10;i++)
{
keypoint3.push_back(keypoint1[i]);
keypoint4.push_back(keypoint2[i]);
}
// difine a sift descriptor extractor
SiftDescriptorExtractor extractor;
//store the descriptor of each image
Mat descriptor1,descriptor2;
BruteForceMatcher<L2<float>> matcher;
vector<DMatch> matches;
Mat img_matches;
//compute the descriptor of each image
extractor.compute(image1,keypoint3,descriptor1);
extractor.compute(image2,keypoint4,descriptor2);
//match
matcher.match(descriptor1,descriptor2,matches);
//show the result
drawMatches(image1,keypoint3,image2,keypoint4,matches,img_matches,Scalar(255,0,0));
imshow("matches",img_matches);
//store the match_image
//imwrite("matches.jpg",img_matches);
waitKey(0);
return 0;
}
|
<span style="font-family:Microsoft YaHei;"> struct DMatch
{
//三个构造函数
DMatch(): queryIdx(-1), trainIdx(-1),imgIdx(-1),distance(std::numeric_limits<float>::max()) {}
DMatch(int _queryIdx, int _trainIdx, float _distance ) :
queryIdx( _queryIdx),trainIdx( _trainIdx), imgIdx(-1),distance( _distance) {}
DMatch(int _queryIdx, int _trainIdx, int _imgIdx, float _distance ) :
queryIdx(_queryIdx), trainIdx( _trainIdx), imgIdx( _imgIdx),distance( _distance) {}
intqueryIdx; //此匹配对应的查询图像的特征描述子索引
inttrainIdx; //此匹配对应的训练(模板)图像的特征描述子索引
intimgIdx; //训练图像的索引(若有多个)
float distance; //两个特征向量之间的欧氏距离,越小表明匹配度越高。
booloperator < (const DMatch &m) const;
};</span>版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/chentravelling/article/details/49761631