来感觉了,再更一篇,马上科研。闲言少叙,开始操作。
搞计算机视觉的monkey可能最不陌生的就是LOWE大神的SIFT算法了,无论科研还是工作,可能会有获取匹配度最高的前N个匹配点对这种需求,OK,码上来!
#include "highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/legacy/legacy.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat input1 = imread("img2.png", 1);
Mat input2 = imread("img1.png", 1);
Mat imgGray1, imgGray2;
//转换灰度图
cvtColor(input1, imgGray1, CV_BGR2GRAY);
cvtColor(input2, imgGray2, CV_BGR2GRAY);
SiftFeatureDetector detector;
vector<KeyPoint> keypoint1, keypoint2;
detector.detect(imgGray1, keypoint1);
detector.detect(imgGray2, keypoint2);
SiftDescriptorExtractor extractor;
Mat descriptor1, descriptor2;
BruteForceMatcher<L2<float>> matcher;
vector<DMatch> matches;
extractor.compute(imgGray1, keypoint1, descriptor1);
extractor.compute(imgGray2, keypoint2, descriptor2);
matcher.match(descriptor1, descriptor2, matches);
//特征点排序
sort(matches.begin(), matches.end());
vector<KeyPoint> goodImagePoints1, goodImagePoints2;
vector<DMatch> matchesVoted;
//获取排名前10个的匹配度高的匹配点集
for (int i = 0; i<10; i++)
{
DMatch dmatch;
dmatch.queryIdx = i;
dmatch.trainIdx = i;
matchesVoted.push_back(dmatch);
goodImagePoints1.push_back(keypoint1[matches[i].queryIdx]);
goodImagePoints2.push_back(keypoint2[matches[i].trainIdx]);
}
Mat img_matches;
std::vector< DMatch > emptyVec;
drawMatches(input1, goodImagePoints1, input2, goodImagePoints2, matchesVoted, img_matches, DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("SIFT_Match_Image", img_matches);
namedWindow("SIFT_Match_Image",WINDOW_NORMAL);
waitKey();
return 0;
}