首页 > 代码库 > Fast检测角点算法

Fast检测角点算法

1.角点定义

  角点是一种局部特征,具有旋转不变性和不随光照条件变化而变化的特点,一般将图像中曲率足够高或者曲率变化明显的点作为角点。检测得到的角点特征通常用于图像匹配、目标跟踪、运动估计等方面。

2.Fast检测角点

1)基本思想

  E.Rosten和T.Drummond两位大佬在06年一篇文章中提出了FAST特征算法,基本思想十分简单:以某个像素点为圆心,某半径的圆周上其他像素点与圆心像素点特性差异达到某种标准时即认为该点就是角点。

2)数学模型

  技术分享

  经过测试发现,选取的圆形半径为3时可以兼顾检测结果和效率。如上图所示,点p与半径为3的圆周上的16个像素点比较其灰度差,若灰度差绝对值大于某阈值的点数超过设定数目(一般可以取9/10/11/12),则认为该点是角点。

     使用小技巧加速该算法,这里取设定数目为12。

  (1)判断点1和点9灰度值与点p差值绝对值是否都大于阈值,如果是则继续;否则pass该点

      (2)判断点1、点5、点9、点13四点与点p灰度值差值大于阈值的数目是否大于2,如果是则继续;否则pass该点

      (3)判断圆周上16点与点p灰度值差值大于阈值的数目是否不小于12,如果是则认为该点是候选点;否则pass该点

  对图像中所有像素点进行上述操作后,得到候选点点集。通常使用非极大值抑制过滤局部非极大值点,在这之前需要先计算各候选点的score,score就定义为16个点与中心点灰度值差值的绝对值总和

3.opencv实现

    个人使用vs2012+opencv2.4.13作为开发环境,具体实现如下

 

  1 #include <iostream>
  2 #include <core/core.hpp>
  3 #include <highgui/highgui.hpp>
  4 #include <imgproc/imgproc.hpp>
  5 #include <features2d/features2d.hpp>
  6 
  7 using namespace std;
  8 using namespace cv;
  9 
 10 
 11 int getSum(uchar *p, int length)
 12 {
 13     int sum = 0;
 14     for(int i=0;i<length; i++)
 15     {
 16         sum += *(p+i);
 17     }
 18     return sum;
 19 }
 20 
 21 int main(int argc, char* argv[])
 22 {
 23     /* 1.读入图像 */
 24     Mat image = imread("../church01.jpg", 0);
 25     if(!image.data)
 26         return 0;
 27 
 28     namedWindow("Original Image");
 29     imshow("Original Image", image);
 30 
 31     Mat fastImg(image.size(), CV_8U, Scalar(0));//用于保存Fast特征点候选点
 32     Mat fastScore(image.size(), CV_32F, Scalar(0));//用于计算候选点score
 33     vector<Point> points;
 34     int rows, cols, threshold;
 35     rows = image.rows;
 36     cols = image.cols;
 37     threshold = 50;
 38 
 39     /* 2.检测Fast特征点 */
 40     for(int x = 3; x < rows - 3; x++)
 41     {
 42         for(int y = 3; y < cols - 3; y++)
 43         {
 44             uchar delta[16] = {0};
 45             uchar diff[16] = {0};
 46             delta[0] = (diff[0] = abs(image.at<uchar>(x,y) - image.at<uchar>(x, y-3))) > threshold;
 47             delta[8] = (diff[8] = abs(image.at<uchar>(x,y) - image.at<uchar>(x, y+3))) > threshold;
 48             if(getSum(delta, 16) == 0)
 49                 continue;
 50             else
 51             {
 52                 delta[12] = (diff[12] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-3, y))) > threshold;
 53                 delta[4] = (diff[4] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+3, y))) > threshold;
 54                 if(getSum(delta, 16) < 3)
 55                     continue;
 56 
 57                 else
 58                 {
 59                     delta[1] = (diff[1] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+1, y-3))) > threshold;
 60                     delta[2] = (diff[2] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+2, y-2))) > threshold;
 61                     delta[3] = (diff[3] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+3, y-1))) > threshold;
 62                                 
 63                     delta[5] = (diff[5] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+3, y+1))) > threshold;
 64                     delta[6] = (diff[6] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+2, y+2))) > threshold;
 65                     delta[7] = (diff[7] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+1, y+3))) > threshold;
 66 
 67                     delta[9] = (diff[9] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-1, y+3))) > threshold;
 68                     delta[10] = (diff[10] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-2, y+2))) > threshold;
 69                     delta[11] = (diff[11] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-3, y+1))) > threshold;
 70 
 71                     delta[13] = (diff[13] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-3, y-1))) > threshold;
 72                     delta[14] = (diff[14] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-2, y-2))) > threshold;
 73                     delta[15] = (diff[15] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-1, y-3))) > threshold;
 74 
 75                     if(getSum(delta, 16) >= 12)
 76                     {
 77                         points.push_back(Point(y,x));
 78                         fastScore.at<float>(x,y) = getSum(diff, 16);
 79                         fastImg.at<uchar>(x,y) = 255;
 80                     }
 81                 }
 82             }
 83         }
 84 
 85     }
 86 
 87     vector<Point>::const_iterator itp = points.begin();
 88     while(itp != points.end())
 89     {
 90         circle(image, *itp, 3, Scalar(255), 1);
 91         ++itp;
 92     }
 93     //未进行非极大值抑制之前的特征点检测结果
 94     namedWindow("Fast Image");
 95     imshow("Fast Image", image);
 96 
 97     /* 3.对特征点候选点进行非极大值抑制 */
 98     image = imread("../church01.jpg", 0);
 99     Mat dilated(fastScore.size(), CV_32F, Scalar(0));
100     Mat localMax;
101     Mat element(7, 7, CV_8U, Scalar(1));
102     dilate(fastScore, dilated, element);
103     compare(fastScore, dilated, localMax, CMP_EQ);
104     bitwise_and(fastImg, localMax, fastImg);
105 
106     for(int x = 0;x < fastImg.cols; x++)
107     {
108         for(int y = 0; y < fastImg.rows; y++)
109         {
110             if(fastImg.at<uchar>(y,x))
111             {
112                 circle(image, Point(x,y), 3, Scalar(255), 1);
113 
114             }
115         }
116     }
117     //进行非极大值抑制之后的特征点检测结果
118     namedWindow("Fast Image2");
119     imshow("Fast Image2", image);
120 
121     waitKey();
122     return 0;
123 }

  代码运行结果如下,比较第2/3张图可以发现,非极大值抑制具有过滤掉局部区域中非极大值角点的作用。

技术分享技术分享技术分享

4.算法小结

    该算法思想简单,运算量很小,适合实时检测方面的应用。不过也有缺点,主要表现为对于边缘点与噪点区分能力不强,当然后面也有很多人在此基础上加以改进提高算法的稳定性。

5.参考文献

 [1]《opencv2计算机视觉编程手册》

 [2]【特征检测】FAST特征检测算法(http://blog.csdn.net/hujingshuang/article/details/46898007)

 

Fast检测角点算法