opencv 特征描述子匹配,暴力匹配,交叉匹配,KNN 匹配,随机一致性匹配(RANSAC)

本章内容:


1. 暴力匹配
2. 匹配对分析
3. 交叉匹配
4. KNN 匹配
5. 随机一致性匹配(RANSAC)

 

1. 暴力匹配

输出结果

 

2. 匹配点分析

 

输出结果:

3. 交叉匹配

 

输出结果

 

4.KNN匹配

输出结果

 

 

5. 随机一致性匹配(RANSAC)

 

输出结果:

 

代码

#include <ostream>
#include <opencv.hpp>
#include<opencv2/opencv.hpp>
#include "opencv2/xfeatures2d.hpp"

int main(int argc, char *argv[])
{
    /*
     本章内容:
        1. 暴力匹配
        2. 匹配对分析
        3. 交叉匹配
        4. KNN 匹配
        5. 随机一致性匹配(RANSAC)
    */
    cv::String fileName = "/home/wang/dev/Image/3-left.JPG";
    cv::String fileName1 = "/home/wang/dev/Image/3-right.JPG";
    cv::Mat src = cv::imread(fileName,cv::IMREAD_REDUCED_COLOR_2);
    cv::Mat src1 = cv::imread(fileName1,cv::IMREAD_REDUCED_COLOR_2);
    if(src.data == NULL){
        printf("图像读入失败\n");
        return -1;
    }
    cv::imshow("src",src);
    cv::imshow("src1",src1);
    // 特征描述符计算
    cv::Ptr<cv::xfeatures2d::SURF> surf =  cv::xfeatures2d::SURF::create();
    std::vector<cv::KeyPoint> kp1;
    std::vector<cv::KeyPoint> kp2;
    cv::Mat des1;
    cv::Mat des2;
    surf->detectAndCompute(src,cv::Mat(),kp1,des1);
    surf->detectAndCompute(src1,cv::Mat(),kp2,des2);
    /*1.暴力匹配
     * 构造: CV_WRAP BFMatcher( int normType=NORM_L2, bool crossCheck=false );
    */
    cv::BFMatcher matcher(cv::NORM_L2);
    std::vector<cv::DMatch> Dmatch;
    matcher.match(des1,des2,Dmatch);

    cv::Mat dstMatch1;
    cv::drawMatches(src,kp1,src1,kp2,Dmatch,dstMatch1);
    cv::imshow("BFMatch", dstMatch1);

    /*2.cv::DMatch分析
     * 属性
        CV_PROP_RW int queryIdx; //!< query descriptor index
        CV_PROP_RW int trainIdx; //!< train descriptor index
        CV_PROP_RW int imgIdx;   //!< train image index
        CV_PROP_RW float distance;
    */
    std::cout << "查询关键点数: " << kp1.size() << std::endl;
    std::cout << "训练关键点数: " << kp2.size() << std::endl;
    std::cout << "queryIdx: " << Dmatch[10].queryIdx << std::endl;
    std::cout << "trainIdx: " << Dmatch[10].trainIdx << std::endl;
    std::cout << "imgIdx: " << Dmatch[10].imgIdx << std::endl;
    std::cout << "distance: " << Dmatch[10].distance << std::endl;

    // 3.交叉匹配
    cv::BFMatcher matcher1(cv::NORM_L2, true);
    std::vector<cv::DMatch> Dmatch1;
    matcher1.match(des1,des2,Dmatch1);
    cv::Mat dstMatch2;
    cv::drawMatches(src,kp1,src1,kp2,Dmatch1,dstMatch2);
    cv::imshow("交叉匹配",dstMatch2);

    std::cout << "交叉匹配点数目: " << Dmatch1.size() << std::endl;

    /*KNN匹配
        api接口:    CV_WRAP void knnMatch( InputArray queryDescriptors, InputArray trainDescriptors,
                   CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
                   InputArray mask=noArray(), bool compactResult=false ) const;
        参数分析:
            @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
    */
    cv::BFMatcher matcher2(cv::NORM_L2);
    std::vector<std::vector<cv::DMatch>> Dmatch2;
    matcher2.knnMatch(des1,des2,Dmatch2,2);
    std::vector<cv::DMatch> goodMatchs;
    std::cout << "KNN总数目:" << Dmatch2.size() << std::endl;
    for(int i = 0; i < Dmatch2.size(); i++){
        if(Dmatch2[i][0].distance < 0.69 * Dmatch2[i][1].distance){
            goodMatchs.push_back(Dmatch2[i][0]);
        }
    }
    std::cout << "KNN总数目交叉匹配数目:" << goodMatchs.size() << std::endl;

    cv::Mat dstKNN2;
    cv::drawMatches(src,kp1,src1,kp2,goodMatchs,dstKNN2);
    cv::imshow("基于KNN的交叉匹配",dstKNN2);

    cv::waitKey(0);
    return 1;
}

 

 

 

 

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值