图像局部特征(十七)--DenseFeature

原文:

http://blog.csdn.net/zhaocj/article/details/45198965

DenseFeatureDetector可以生成具有一定密度和规律分布的图像特征点,它主要用于3D VIZ。

DenseFeatureDetector的原理是,把输入图像分割成大小相等的网格,每一个网格提取一个像素作为特征点。类似于图像尺度金字塔,该方法也可以生成不同层图像的特征点,每一层图像所分割的网格大小是不同的,即表示各层的尺度不同。

 

下面我们就来分析它的源码。

DenseFeatureDetector类的构造函数:

 

 

[cpp] view plain copy print?

  1. DenseFeatureDetector::DenseFeatureDetector( float _initFeatureScale, int _featureScaleLevels,  
  2.                                       float _featureScaleMul, int _initXyStep,  
  3.                                       int _initImgBound, bool _varyXyStepWithScale,  
  4.                                       bool _varyImgBoundWithScale ) :  
  5.     initFeatureScale(_initFeatureScale), featureScaleLevels(_featureScaleLevels),  
  6.     featureScaleMul(_featureScaleMul), initXyStep(_initXyStep), initImgBound(_initImgBound),  
  7.     varyXyStepWithScale(_varyXyStepWithScale), varyImgBoundWithScale(_varyImgBoundWithScale)  
  8. {}  

DenseFeatureDetector::DenseFeatureDetector( float _initFeatureScale, int _featureScaleLevels,
                                      float _featureScaleMul, int _initXyStep,
                                      int _initImgBound, bool _varyXyStepWithScale,
                                      bool _varyImgBoundWithScale ) :
    initFeatureScale(_initFeatureScale), featureScaleLevels(_featureScaleLevels),
    featureScaleMul(_featureScaleMul), initXyStep(_initXyStep), initImgBound(_initImgBound),
    varyXyStepWithScale(_varyXyStepWithScale), varyImgBoundWithScale(_varyImgBoundWithScale)
{}

 

 

 

initFeatureScale表示初始图像层特征点的尺度,默认为1

 

featureScaleLevels表示需要构建多少层图像,默认为1

 

featureScaleMul表示各层图像之间参数的比例系数,该系数等于相邻两层图像之间的网格宽度之比,尺度之比,以及预留边界宽度之比,默认为0.1

 

initXyStep表示初始图像层的网格宽度,默认为6

 

initImgBound表示初始图像层的预留边界宽度,默认为0

 

varyXyStepWithScale表示各层图像是否进行网格宽度的调整,如果为false,则表示各层图像网格宽度都是initXyStep,如果为true,则表示各层图像网格宽度不等,它们之间的比例系数为featureScaleMul,默认为true

 

varyImgBoundWithScale表示各层图像是否进行预留边界宽度的调整,如果为false,则表示各层图像预留边界宽度都是initImgBound,如果为true,则表示各层图像预留边界宽度不等,它们之间的比例系数为featureScaleMul,默认为false

 

下面是检测特征点函数detectImpl:

 

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. void DenseFeatureDetector::detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask ) const  
  2. {  
  3.     // curScale表示当前层图像特征点的尺度  
  4.     float curScale = static_cast<float>(initFeatureScale);  
  5.     //curStep表示当前层图像的网格宽度  
  6.     int curStep = initXyStep;  
  7.     //curBound表示当前层图像的预留边界宽度  
  8.     int curBound = initImgBound;  
  9.     //遍历各层图像  
  10.     for( int curLevel = 0; curLevel < featureScaleLevels; curLevel++ )  
  11.     {  
  12.         //遍历当前层图像的所有网格,图像四周的预留边界处是没有网格的,横、纵坐标的步长就是网格的宽度  
  13.         for( int x = curBound; x < image.cols - curBound; x += curStep )  
  14.         {  
  15.             for( int y = curBound; y < image.rows - curBound; y += curStep )  
  16.             {  
  17.                 //把网格的左上角坐标处的像素作为该网格的特征点,并保存  
  18.                 keypoints.push_back( KeyPoint(static_cast<float>(x), static_cast<float>(y), curScale) );  
  19.             }  
  20.         }  
  21.         //调整下一层图像特征点的尺度  
  22.         curScale = static_cast<float>(curScale * featureScaleMul);  
  23.         //如果varyXyStepWithScale为true,则调整下一层图像的网格宽度  
  24.         if( varyXyStepWithScale ) curStep = static_cast<int>( curStep * featureScaleMul + 0.5f );  
  25.         //如果varyImgBoundWithScale为true,则调整下一层图像的预留边界宽度  
  26.         if( varyImgBoundWithScale ) curBound = static_cast<int>( curBound * featureScaleMul + 0.5f );  
  27.     }  
  28.     //掩码矩阵的特征点处理  
  29.     KeyPointsFilter::runByPixelsMask( keypoints, mask );  
  30. }  

void DenseFeatureDetector::detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask ) const
{
    // curScale表示当前层图像特征点的尺度
    float curScale = static_cast<float>(initFeatureScale);
    //curStep表示当前层图像的网格宽度
    int curStep = initXyStep;
    //curBound表示当前层图像的预留边界宽度
    int curBound = initImgBound;
    //遍历各层图像
    for( int curLevel = 0; curLevel < featureScaleLevels; curLevel++ )
    {
        //遍历当前层图像的所有网格,图像四周的预留边界处是没有网格的,横、纵坐标的步长就是网格的宽度
        for( int x = curBound; x < image.cols - curBound; x += curStep )
        {
            for( int y = curBound; y < image.rows - curBound; y += curStep )
            {
                //把网格的左上角坐标处的像素作为该网格的特征点,并保存
                keypoints.push_back( KeyPoint(static_cast<float>(x), static_cast<float>(y), curScale) );
            }
        }
        //调整下一层图像特征点的尺度
        curScale = static_cast<float>(curScale * featureScaleMul);
        //如果varyXyStepWithScale为true,则调整下一层图像的网格宽度
        if( varyXyStepWithScale ) curStep = static_cast<int>( curStep * featureScaleMul + 0.5f );
        //如果varyImgBoundWithScale为true,则调整下一层图像的预留边界宽度
        if( varyImgBoundWithScale ) curBound = static_cast<int>( curBound * featureScaleMul + 0.5f );
    }
    //掩码矩阵的特征点处理
    KeyPointsFilter::runByPixelsMask( keypoints, mask );
}

下面给出一个具体的应用实例:

 

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. #include "opencv2/core/core.hpp"  
  2. #include "highgui.h"  
  3. #include "opencv2/imgproc/imgproc.hpp"  
  4. #include "opencv2/features2d/features2d.hpp"  
  5. #include "opencv2/nonfree/nonfree.hpp"  
  6.   
  7. using namespace cv;  
  8. //using namespace std;  
  9.   
  10. int main(int argc, char** argv)  
  11. {  
  12.    Mat img = imread("box_in_scene.png"), img1;;  
  13.      
  14.    cvtColor( img, img1, CV_BGR2GRAY );  
  15.   
  16.    DenseFeatureDetector dense;  
  17.   
  18.    vector<KeyPoint> key_points;  
  19.    Mat output_img;  
  20.   
  21.    dense.detect(img1,key_points,Mat());  
  22.    drawKeypoints(img, key_points, output_img, Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);  
  23.   
  24.    namedWindow("DENSE");  
  25.    imshow("DENSE", output_img);  
  26.    waitKey(0);  
  27.   
  28.    return 0;  
  29. }  

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值