这一篇是接上篇的文章接着往下写的,这里着重介绍点的输出,其他的地方不做过多的赘述
#include "opencv2/opencv.hpp"
#include <opencv2/nonfree/nonfree.hpp>//SIFT
#include <opencv2/legacy/legacy.hpp>//BFMatch暴力匹配
#include <vector>
#include<iostream>
using namespace std;
using namespace cv;
void main()
{
Mat srcImg1 = imread("D:\\0.jpg");
Mat srcImg2 = imread("D:\\1.jpg");
resize(srcImg1,srcImg1,Size(srcImg1.cols*3,srcImg1.rows*3));
resize(srcImg2,srcImg2,Size(srcImg2.cols/3,srcImg2.rows/3));
//定义SIFT特征检测类对象
SiftFeatureDetector siftDetector(1000);
//定义KeyPoint变量
vector<KeyPoint>keyPoints1;
vector<KeyPoint>keyPoints2;
//特征点检测
siftDetector.detect(srcImg1, keyPoints1);
siftDetector.detect(srcImg2, keyPoints2);
//绘制特征点(关键点)
Mat feature_pic1, feature_pic2;
drawKeypoints(srcImg1, keyPoints1, feature_pic1, Scalar::all(-1));
drawKeypoints(srcImg2, keyPoints2, feature_pic2, Scalar::all(-1));
//显示原图
imshow("src1", srcImg1);
imshow("src2", srcImg2);
//显示结果
imshow("feature1", feature_pic1);
imshow("feature2", feature_pic2);
//计算特征点描述符 / 特征向量提取
SiftDescriptorExtractor descriptor;
Mat description1;
descriptor.compute(srcImg1, keyPoints1, description1);
Mat description2;
descriptor.compute(srcImg2, keyPoints2, description2);
cout<<description1.cols<<endl;
cout<<description1.rows<<endl;
//进行BFMatch暴力匹配
BruteForceMatcher<L1<float>>matcher; //实例化暴力匹配器
vector<DMatch>matches; //定义匹配结果变量
matcher.match(description1, description2, matches); //实现描述符之间的匹配
//匹配结果筛选
nth_element(matches.begin(), matches.begin()+0, matches.end()); //提取出前x个最佳匹配结果
matches.erase(matches.begin()+1, matches.end()); //剔除掉其余的匹配结果
int index1,index2;
for(size_t i=0;i<matches.size();i++)//这里是对筛选后坐标对进行输出
{
index1=matches.at(i).queryIdx;//这个是访问特征图片的属性。具体是啥我查看源码也没太搞明白
index2=matches.at(i).trainIdx;//这个是访问待匹配图片的属性
cout<<"这是个啥:"<<keyPoints2.at(index2).pt<<endl;//我这里是输出待匹配特征图片的坐标,keyPoints2这个是特征向量提取后的集合
cout<<"这是个啥:"<<keyPoints2.at(index2).pt.x<<endl//这里是输出待匹配特征图片的x点
cout<<"这是个啥:"<<keyPoints2.at(index2).pt.y<<endl//这里是输出待匹配特征图片的y点
}
Mat result;
drawMatches(srcImg1, keyPoints1, srcImg2, keyPoints2, matches, result, Scalar(0, 255, 0), Scalar::all(-1));//匹配特征点绿色,单一特征点颜色随机
imshow("Match_Result", result);
waitKey(0);
}