心胸决定格局,眼界决定境界...

BRISK特征提取算法

简介

        BRISK算法是2011年ICCV上《BRISK:Binary Robust Invariant Scalable Keypoints》文章中,提出来的一种特征提取算法,也是一种二进制的特征描述算子。

       它具有较好的旋转不变性、尺度不变性,较好的鲁棒性等。在图像配准应用中,速度比较:SIFT<SURF<BRISK<FREAK<ORB,在对有较大模糊的图像配准时,BRISK算法在其中表现最为出色。

BRISK算法

特征点检测

        BRISK算法主要利用FAST9-16进行特征点检测(为什么是主要?因为用到一次FAST5-8),可参见博客:FAST特征点检测算法。要解决尺度不变性,就必须在尺度空间进行特征点检测,于是BRISK算法中构造了图像金字塔进行多尺度表达。

建立尺度空间

        构造n个octave层(用ci表示)和n个intra-octave层(用di表示),文章中n=4,i={0,1,...,n-1}。假设有图像img,octave层的产生:c0层就是img原图像,c1层是c0层的2倍下采样,c2层是c1层的2倍下采样,以此类推。intra-octave层的产生:d0层是img的1.5倍下采样,d1层是d0层的2倍下采样(即img的2*1.5倍下采样),d2层是d1层的2倍下采样,以此类推。

则ci、di层与原图像的尺度关系用t表示为:

ci、di层与原图像大小关系为:

        由于n=4,所以一共可以得到8张图,octave层之间尺度(缩放因子)是2倍关系,intra-octave层之间尺度(缩放因子)也是2倍关系。

特征点检测

        对这8张图进行FAST9-16角点检测,得到具有角点信息的8张图,对原图像img进行一次FAST5-8角点检测(当做d(-1)层,虚拟层),总共会得到9幅有角点信息的图像。

非极大值抑制

        对这9幅图像,进行空间上的非极大值抑制(同SIFT算法的非极大值抑制):特征点在位置空间(8邻域点)和尺度空间(上下层2x9个点),共26个邻域点的FAST的得分值要最大,否则不能当做特征点;此时得到的极值点还比较粗糙,需要进一步精确定位。

亚像素插值

        进过上面步骤,得到了图像特征点的位置和尺度,在极值点所在层及其上下层所对应的位置,对FAST得分值(共3个)进行二维二次函数插值(x、y方向),得到真正意义上的得分极值点及其精确的坐标位置(作为特征点位置);再对尺度方向进行一维插值,得到极值点所对应的尺度(作为特征点尺度)。

特征点描述

高斯滤波

       现在,我们得到了特征点的位置和尺度(t)后,要对特征点赋予其描述符。均匀采样模式:以特征点为中心,构建不同半径的同心圆,在每个圆上获取一定数目的等间隔采样点(所有采样点包括特征点,一共N个),由于这种邻域采样模式会引起混叠效应,所以需要对同心圆上的采样点进行高斯滤波。

       采样模式如下图,蓝圈表示;以采样点为中心,为方差进行高斯滤波,滤波半径大小与高斯方差的大小成正比,红圈表示。最终用到的N个采样点是经过高斯平滑后的采样点。下图是t=1时的。(文章中:N=60)

局部梯度计算

         由于有N个采样点,则采样点两两组合成一对,共有N(N-1)/2钟组合方式,所有组合方式的集合称作采样点对,用集合表示,其中像素分别是,δ表示尺度。用表示特征点局部梯度集合,则有:

定义短距离点对子集、长距离点对子集(L个):

其中,,t是特征点所在的尺度。

现在要利用上面得到的信息,来计算特征点的主方向(注意:此处只用到了长距离子集),如下:

特征描述符

         要解决旋转不变性,则需要对特征点周围的采样区域进行旋转到主方向,旋转后得到新的采样区域,采样模式同上。BRISK描述子是二进制的特征,由采样点集合可得到N(N-1)/2对采样点对,就可以得到N(N-1)/2个距离的集合(包含长、短距离子集),考虑其中短距离子集中的512个短距离点对,进行二进制编码,判断方式如下:

其中,带有上标,表示经过旋转a角度后的,新的采样点。由此可得到,512Bit的二进制编码,也就是64个字节(BRISK64)。

匹配方法

汉明距离进行比较,与其他二进制描述子的匹配方式一样。

实验

opencv代码

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include <cv.h>  
  2. #include <opencv2/highgui/highgui.hpp>  
  3. #include <opencv2/core/core.hpp>  
  4. #include <opencv2/nonfree/features2d.hpp>  
  5. #include <opencv2/nonfree/nonfree.hpp>  
  6. #include <Windows.h>  
  7.   
  8. using namespace cv;  
  9. using namespace std;  
  10.   
  11. int main()  
  12. {  
  13.     //Load Image  
  14.     Mat c_src1 =  imread( "1.png");  
  15.     Mat c_src2 = imread("2.png");  
  16.     Mat src1 = imread( "1.png", CV_LOAD_IMAGE_GRAYSCALE);  
  17.     Mat src2 = imread( "2.png", CV_LOAD_IMAGE_GRAYSCALE);  
  18.     if( !src1.data || !src2.data )  
  19.     {  
  20.         cout<< "Error reading images " << std::endl;  
  21.         return -1;  
  22.     }  
  23.     //feature detect  
  24.     BRISK detector;  
  25.     vector<KeyPoint> kp1, kp2;  
  26.     double start = GetTickCount();  
  27.     detector.detect( src1, kp1 );  
  28.     detector.detect( src2, kp2 );  
  29.     //cv::BRISK extractor;  
  30.     Mat des1,des2;//descriptor  
  31.     detector.compute(src1, kp1, des1);  
  32.     detector.compute(src2, kp2, des2);  
  33.     Mat res1,res2;  
  34.     int drawmode = DrawMatchesFlags::DRAW_RICH_KEYPOINTS;  
  35.     drawKeypoints(c_src1, kp1, res1, Scalar::all(-1), drawmode);//画出特征点  
  36.     drawKeypoints(c_src2, kp2, res2, Scalar::all(-1), drawmode);  
  37.     cout<<"size of description of Img1: "<<kp1.size()<<endl;  
  38.     cout<<"size of description of Img2: "<<kp2.size()<<endl;  
  39.   
  40.     BFMatcher matcher(NORM_HAMMING);  
  41.     vector<DMatch> matches;  
  42.     matcher.match(des1, des2, matches);  
  43.     double end = GetTickCount();  
  44.     cout<<"耗时:"<<(end - start) <<"ms"<<endl;  
  45.     Mat img_match;  
  46.     drawMatches(src1, kp1, src2, kp2, matches, img_match);  
  47.     cout<<"number of matched points: "<<matches.size()<<endl;  
  48.     imshow("matches",img_match);  
  49.     cvWaitKey(0);  
  50.     cvDestroyAllWindows();  
  51.     return 0;  
  52. }  

 

实验结果

视频地址

代码分析

由于代码都很长,只列出了brisk类的两个方法,其余详见:..\opencv\sources\modules\features2d\src\brisk.c
[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. // construct the image pyramids(构造图像金字塔)  
  2. void  
  3. BriskScaleSpace::constructPyramid(const cv::Mat& image)  
  4. {  
  5.   
  6.   // set correct size:  
  7.   pyramid_.clear();  
  8.   
  9.   // fill the pyramid:  
  10.   pyramid_.push_back(BriskLayer(image.clone()));  
  11.   if (layers_ > 1)  
  12.   {  
  13.     pyramid_.push_back(BriskLayer(pyramid_.back(), BriskLayer::CommonParams::TWOTHIRDSAMPLE));//d0层是2/3  
  14.   }  
  15.   const int octaves2 = layers_;  
  16.   
  17.   for (uchar i = 2; i < octaves2; i += 2)  
  18.   {  
  19.     pyramid_.push_back(BriskLayer(pyramid_[i - 2], BriskLayer::CommonParams::HALFSAMPLE));//c?层是前两层的1/2  
  20.     pyramid_.push_back(BriskLayer(pyramid_[i - 1], BriskLayer::CommonParams::HALFSAMPLE));//d?层是前两层的1/2(除d0层外)  
  21.   }  
  22. }  
[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. //提取特征点  
  2. void  
  3. BriskScaleSpace::getKeypoints(const int threshold_, std::vector<cv::KeyPoint>& keypoints)  
  4. {  
  5.   // make sure keypoints is empty  
  6.   keypoints.resize(0);  
  7.   keypoints.reserve(2000);  
  8.   
  9.   // assign thresholds  
  10.   int safeThreshold_ = (int)(threshold_ * safetyFactor_);  
  11.   std::vector<std::vector<cv::KeyPoint> > agastPoints;  
  12.   agastPoints.resize(layers_);  
  13.   
  14.   // go through the octaves and intra layers and calculate fast corner scores:  
  15.   for (int i = 0; i < layers_; i++)  
  16.   {  
  17.     // call OAST16_9 without nms  
  18.     BriskLayer& l = pyramid_[i];  
  19.     l.getAgastPoints(safeThreshold_, agastPoints[i]);  
  20.   }  
  21.   
  22.   if (layers_ == 1)  
  23.   {  
  24.     // just do a simple 2d subpixel refinement...  
  25.     const size_t num = agastPoints[0].size();  
  26.     for (size_t n = 0; n < num; n++)  
  27.     {  
  28.       const cv::Point2f& point = agastPoints.at(0)[n].pt;  
  29.       // first check if it is a maximum:  
  30.       if (!isMax2D(0, (int)point.x, (int)point.y))  
  31.         continue;  
  32.   
  33.       // let's do the subpixel and float scale refinement:  
  34.       BriskLayer& l = pyramid_[0];  
  35.       int s_0_0 = l.getAgastScore(point.x - 1, point.y - 1, 1);  
  36.       int s_1_0 = l.getAgastScore(point.x, point.y - 1, 1);  
  37.       int s_2_0 = l.getAgastScore(point.x + 1, point.y - 1, 1);  
  38.       int s_2_1 = l.getAgastScore(point.x + 1, point.y, 1);  
  39.       int s_1_1 = l.getAgastScore(point.x, point.y, 1);  
  40.       int s_0_1 = l.getAgastScore(point.x - 1, point.y, 1);  
  41.       int s_0_2 = l.getAgastScore(point.x - 1, point.y + 1, 1);  
  42.       int s_1_2 = l.getAgastScore(point.x, point.y + 1, 1);  
  43.       int s_2_2 = l.getAgastScore(point.x + 1, point.y + 1, 1);  
  44.       float delta_x, delta_y;  
  45.       float max = subpixel2D(s_0_0, s_0_1, s_0_2, s_1_0, s_1_1, s_1_2, s_2_0, s_2_1, s_2_2, delta_x, delta_y);  
  46.   
  47.       // store:  
  48.       keypoints.push_back(cv::KeyPoint(float(point.x) + delta_x, float(point.y) + delta_y, basicSize_, -1, max, 0));  
  49.   
  50.     }  
  51.   
  52.     return;  
  53.   }  
  54.   
  55.   float x, y, scale, score;  
  56.   for (int i = 0; i < layers_; i++)  
  57.   {  
  58.     BriskLayer& l = pyramid_[i];  
  59.     const size_t num = agastPoints[i].size();  
  60.     if (i == layers_ - 1)  
  61.     {  
  62.       for (size_t n = 0; n < num; n++)  
  63.       {  
  64.         const cv::Point2f& point = agastPoints.at(i)[n].pt;  
  65.         // consider only 2D maxima...  
  66.         if (!isMax2D(i, (int)point.x, (int)point.y))  
  67.           continue;  
  68.   
  69.         bool ismax;  
  70.         float dx, dy;  
  71.         getScoreMaxBelow(i, (int)point.x, (int)point.y, l.getAgastScore(point.x, point.y, safeThreshold_), ismax, dx, dy);  
  72.         if (!ismax)  
  73.           continue;  
  74.   
  75.         // get the patch on this layer:  
  76.         int s_0_0 = l.getAgastScore(point.x - 1, point.y - 1, 1);  
  77.         int s_1_0 = l.getAgastScore(point.x, point.y - 1, 1);  
  78.         int s_2_0 = l.getAgastScore(point.x + 1, point.y - 1, 1);  
  79.         int s_2_1 = l.getAgastScore(point.x + 1, point.y, 1);  
  80.         int s_1_1 = l.getAgastScore(point.x, point.y, 1);  
  81.         int s_0_1 = l.getAgastScore(point.x - 1, point.y, 1);  
  82.         int s_0_2 = l.getAgastScore(point.x - 1, point.y + 1, 1);  
  83.         int s_1_2 = l.getAgastScore(point.x, point.y + 1, 1);  
  84.         int s_2_2 = l.getAgastScore(point.x + 1, point.y + 1, 1);  
  85.         float delta_x, delta_y;  
  86.         float max = subpixel2D(s_0_0, s_0_1, s_0_2, s_1_0, s_1_1, s_1_2, s_2_0, s_2_1, s_2_2, delta_x, delta_y);  
  87.   
  88.         // store:  
  89.         keypoints.push_back(  
  90.             cv::KeyPoint((float(point.x) + delta_x) * l.scale() + l.offset(),  
  91.                          (float(point.y) + delta_y) * l.scale() + l.offset(), basicSize_ * l.scale(), -1, max, i));  
  92.       }  
  93.     }  
  94.     else  
  95.     {  
  96.       // not the last layer:  
  97.       for (size_t n = 0; n < num; n++)  
  98.       {  
  99.         const cv::Point2f& point = agastPoints.at(i)[n].pt;  
  100.   
  101.         // first check if it is a maximum:  
  102.         if (!isMax2D(i, (int)point.x, (int)point.y))  
  103.           continue;  
  104.   
  105.         // let's do the subpixel and float scale refinement:  
  106.         bool ismax=false;  
  107.         score = refine3D(i, (int)point.x, (int)point.y, x, y, scale, ismax);  
  108.         if (!ismax)  
  109.         {  
  110.           continue;  
  111.         }  
  112.   
  113.         // finally store the detected keypoint:  
  114.         if (score > float(threshold_))  
  115.         {  
  116.           keypoints.push_back(cv::KeyPoint(x, y, basicSize_ * scale, -1, score, i));  
  117.         }  
  118.       }  
  119.     }  
  120.   }  
  121. }  

参考文献

 

1、BRISK:binary robust invariant scalable keypoints,2011,ICCV.

2、多种角度比较SIFT、SURF、RISK、ORB、FREAK算法[J],2014.

3、基于颜色不变量的特征匹配算法研究[硕士论文],2014.

 
 
posted @ 2016-11-22 11:49  WELEN  阅读(9612)  评论(1编辑  收藏  举报