OpenCV学习笔记:实现获取匹配度最高的前N个匹配点对(SIFT算法)

来感觉了,再更一篇,马上科研。闲言少叙,开始操作。

搞计算机视觉的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;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

视觉闫小亘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值