opencv SIFT特征匹配,可自动选择最佳的20个匹配结果

#include <opencv2\highgui\highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\nonfree\nonfree.hpp>
#include <opencv2/legacy/legacy.hpp> 
#include <iostream>


using namespace std;
using namespace cv;


int main()
{
Mat im1 = imread("1.jpg");
Mat im2 = imread("11.jpg");
Mat image1, image2;
resize(im1, image1, Size(int(im1.cols*0.25),int(im1.rows*0.25)));
resize(im2, image2, Size(int(im2.cols*0.25),int(im2.rows*0.25)));
Mat image3 = image1.clone();
Mat image4 = image2.clone();
//Sift检测器
SiftFeatureDetector detector;


//保存两幅图像得到的特征点
vector<KeyPoint> image1KeyPoint, image2KeyPoint;


//检测特征点
detector.detect(image1, image1KeyPoint);
detector.detect(image2, image2KeyPoint);


//把特征点画在两幅新图上
Mat imageOutput1;
Mat imageOutput2;


drawKeypoints(image1, image1KeyPoint, imageOutput1, Scalar(0, 255, 0));
drawKeypoints(image2, image2KeyPoint, imageOutput2, Scalar(0, 255, 0));


//Sift特征描述提取
SiftDescriptorExtractor extractor;
Mat descriptor1, descriptor2;
//得到Sift特征描述符
extractor.compute(imageOutput1, image1KeyPoint, descriptor1);
extractor.compute(imageOutput2, image2KeyPoint, descriptor2);


//暴力匹配
BruteForceMatcher<L2<float>> matcher;
vector<DMatch> matches;
matcher.match(descriptor1, descriptor2, matches);


//挑选匹配的最好的前20个
nth_element(matches.begin(), matches.begin() + 19, matches.end());
matches.erase(matches.begin() + 19, matches.end());






//画出匹配线
Mat image_matched;




drawMatches(image3, image1KeyPoint, image4, image2KeyPoint, matches, image_matched, Scalar::all(-1), Scalar::all(-1), vector<char>(),2);


imshow("image1", imageOutput1);
imshow("image2", imageOutput2);
imshow("image_matched", image_matched);
imwrite("result.bmp", image_matched);
waitKey(0);
return 0;
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值