Opencv-5-ORB特征提取和匹配

4 程序结果

hq-orb-匹配
在这里插入图片描述

1 图像特征的变量初始化

    std::vector<KeyPoint> keypoints_1, keypoints_2;
    Mat descriptors_1, descriptors_2;
    Ptr<ORB> orb = ORB::create (500, 1.2f, 8, 31, 0, 2, ORB::HARRIS_SCORE,31,20 );

//500 关键点数目
//1.2f scaleFactor - float 金字塔抽取率,必须大于1。ORB使用图像金字塔来查找要素,必须提供金字塔中每个图层的比例因子。
//scaleFactor = 2表示经典金字塔,其中每个下一级别的像素比前一级低4倍。大比例因子将减少发现的功能数量。
//8 nlevels - int金字塔等级的数量。最小级别的线性大小等于input_image_linear_size / pow(scaleFactor,nlevels)。
//31 edgeThreshold - - int未检测到要素的边框大小。由于关键点具有特定的像素大小,因此必须从搜索中排除图像的边缘。 edgeThreshold的大小应该等于或大于patchSize参数。
//0 firstLevel - int此参数允许您确定应将哪个级别视为金字塔中的第一级别。它在当前实现中应为0。通常,具有统一标度的金字塔等级被认为是第一级。
//2 WTA_K - int用于生成定向的BRIEF描述符的每个元素的随机像素的数量。可能的值为2,3和4,其中2为默认值。例如,值3意味着一次选择三个随机像素来比较它们的亮度。返回最亮像素的索引。由于有3个像素,因此返回的索引将为0,1或2。
//scoreType -此参数可以设置为HARRIS_SCORE或FAST_SCORE。默认的HARRIS_SCORE表示Harris角算法用于对要素进行排名。该分数仅用于保留最佳功能。 FAST_SCORE生成的关键点稍差,但计算起来要快一些。
//31 patchSize - int面向简要描述符使用的补丁的大小。当然,在较小的金字塔层上,由特征覆盖的感知图像区域将更大。
//20 fastThreshold快速阈值

2角点检测和计算描述子

    //检测 Oriented FAST 角点位置
    orb->detect ( img_1,keypoints_1 );
    orb->detect ( img_2,keypoints_2 );
    //根据角点位置计算 BRIEF 描述子
    orb->compute ( img_1, keypoints_1, descriptors_1 );
    orb->compute ( img_2, keypoints_2, descriptors_2 );
    //画出关键点
    Mat outimg1;
    drawKeypoints( img_1, keypoints_1, outimg1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
    imshow("argv[1]图的ORB特征点",outimg1);

3 匹配BRIEF描述子和优化匹配对

//对两图中的BRIEF描述子进行匹配,使用 Hamming 距离
    vector<DMatch> matches;
    BFMatcher matcher ( NORM_HAMMING );
    matcher.match ( descriptors_1, descriptors_2, matches );
//匹配点对筛选
    double min_dist=10000, max_dist=0;
//找出所有匹配之间的最小距离和最大距离, 即是最相似的和最不相似的两组点之间的距离
    for ( int i = 0; i < descriptors_1.rows; i++ )
    {
        double dist = matches[i].distance;
        if ( dist < min_dist ) min_dist = dist;
        if ( dist > max_dist ) max_dist = dist;
    }
    
    // 供娱乐的写法
    //min_dist = min_element( matches.begin(), matches.end(), [](const DMatch& m1, const DMatch& m2) {return m1.distance<m2.distance;} )->distance;
    //max_dist = max_element( matches.begin(), matches.end(), [](const DMatch& m1, const DMatch& m2) {return m1.distance<m2.distance;} )->distance;

    printf ( "-- Max dist : %f \n", max_dist );
    printf ( "-- Min dist : %f \n", min_dist );

    //当描述子之间的距离大于两倍的最小距离时,即认为匹配有误.但有时候最小距离会非常小,设置一个经验值30作为下限.
    std::vector< DMatch > good_matches;
    for ( int i = 0; i < descriptors_1.rows; i++ )
    {
        if ( matches[i].distance <= max ( 2*min_dist, 30.0 ) )
        {
            good_matches.push_back ( matches[i] );
        }
    }
    
    //绘制匹配结果
    Mat img_match;
    Mat img_goodmatch;
    drawMatches ( img_1, keypoints_1, img_2, keypoints_2, matches, img_match );
    drawMatches ( img_1, keypoints_1, img_2, keypoints_2, good_matches, img_goodmatch );
    imshow ( "所有匹配点对", img_match );
    imshow ( "优化后匹配点对", img_goodmatch );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值