该算法的基本原理是使用圆周长为16个像素点(半径为3的Bresenham圆)来判定其圆心像素P是否为角点。在圆周上按顺时针方向从1到16的顺序对圆周像素点进行编号。如果在圆周上有N个连续的像素的亮度都比圆心像素的亮度Ip加上阈值t还要亮,或者比圆心像素的亮度减去阈值还要暗,则圆心像素被称为角点。因此要想成为角点,必须满足下列两个条件之一:
条件1:集合S由圆周上N个连续的像素x组成,Ix > Ip + t;
条件2:集合S由圆周上N个连续的像素x组成,Ix < Ip - t。
N一般选择为12。
在一幅图像中,非角点往往是占多数,而且非角点检测要比角点检测容易得多,因此首先剔除掉非角点将大大提高角点检测速度。由于N为12,所以编号为1,5,9,13的这4个圆周像素点中应该至少有三个像素点满足角点条件,圆心才有可能是角点。因此首先检查1和9像素点,如果I1和I9在[Ip – t, Ip + t]之间,则圆心肯定不是角点,否则再检查5和13像素点。如果这4个像素中至少有三个像素满足亮度高于Ip+t或低于Ip – t,则进一步检查圆周上其余像素点。
以上方法还是有不够鲁棒的地方,但可以通过机器学习和非极大值抑制的方法来增强鲁棒性。由于opencv中相关的函数没有使用机器学习,因此我们这里只介绍非极大值抑制的方法。由于分割测试并没有计算角点响应函数,因此常规的非极大值抑制方法并不适用于FAST算法。下面是FAST的非极大值抑制方法:
1、计算得分函数,它的值V是特征点与其圆周上16个像素点的绝对差值中的最小值;
2、在3×3的特征点邻域内(而不是图像邻域),比较V;
3、剔除掉非极大值的特征点。
FAST角点检测方法的具体步骤为:
1、在圆周上的部分像素点上,进行非角点的检测;
2、如果初步判断是角点,则在圆周上的全部像素点上进行角点检测;
3、对角点进行非极大值抑制,得到角点输出。
在opencv中,实现FAST算法的核心函数有两个,它们的原型为:
- void FAST(InputArray image, vector<KeyPoint>& keypoints, int threshold, bool nonmaxSuppression=true )
- void FASTX(InputArray image, vector<KeyPoint>& keypoints, int threshold, bool nonmaxSuppression, int type)
image为输入图像,要求是灰度图像
keypoints为检测到的特征点向量
threshold为阈值t
nonmaxSuppression为是否进行非极大值抑制,true表示进行非极大值抑制
type为选取圆周像素点的个数,是8(FastFeatureDetector::TYPE_5_8)、12(FastFeatureDetector::TYPE_7_12)还是16(FastFeatureDetector::TYPE_9_16)。该参数是FAST函数和FASTX函数的区别,事实上,FAST函数是调用FASTX函数,而传入的type值为FastFeatureDetector::TYPE_9_16。
FAST角点检测方法是在sources/modules/features2d/src/fast.cpp文件内定义的:
- void FAST(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression)
- {
-
- FASTX(_img, keypoints, threshold, nonmax_suppression, FastFeatureDetector::TYPE_9_16);
- }
FASTX函数的作用是调用一个函数模板,模板的参数值是根据参数type的不同而定义的所使用的圆周像素的个数:
- void FASTX(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression, int type)
- {
- switch(type) {
- case FastFeatureDetector::TYPE_5_8:
- FAST_t<8>(_img, keypoints, threshold, nonmax_suppression);
- break;
- case FastFeatureDetector::TYPE_7_12:
- FAST_t<12>(_img, keypoints, threshold, nonmax_suppression);
- break;
- case FastFeatureDetector::TYPE_9_16:
- #ifdef HAVE_TEGRA_OPTIMIZATION
- if(tegra::FAST(_img, keypoints, threshold, nonmax_suppression))
- break;
- #endif
- FAST_t<16>(_img, keypoints, threshold, nonmax_suppression);
- break;
- }
- }
下面是函数模板FAST_t,在这里我们以patternSize=16为例进行讲解:
- template<int patternSize>
- void FAST_t(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression)
- {
- Mat img = _img.getMat();
-
-
- const int K = patternSize/2, N = patternSize + K + 1;
- #if CV_SSE2
- const int quarterPatternSize = patternSize/4;
- (void)quarterPatternSize;
- #endif
- int i, j, k, pixel[25];
-
- makeOffsets(pixel, (int)img.step, patternSize);
-
- keypoints.clear();
-
- threshold = std::min(std::max(threshold, 0), 255);
-
- #if CV_SSE2
- __m128i delta = _mm_set1_epi8(-128), t = _mm_set1_epi8((char)threshold), K16 = _mm_set1_epi8((char)K);
- (void)K16;
- (void)delta;
- (void)t;
- #endif
-
- uchar threshold_tab[512];
-
- for( i = -255; i <= 255; i++ )
- threshold_tab[i+255] = (uchar)(i < -threshold ? 1 : i > threshold ? 2 : 0);
-
- AutoBuffer<uchar> _buf((img.cols+16)*3*(sizeof(int) + sizeof(uchar)) + 128);
- uchar* buf[3];
-
- buf[0] = _buf; buf[1] = buf[0] + img.cols; buf[2] = buf[1] + img.cols;
-
- int* cpbuf[3];
- cpbuf[0] = (int*)alignPtr(buf[2] + img.cols, sizeof(int)) + 1;
- cpbuf[1] = cpbuf[0] + img.cols + 1;
- cpbuf[2] = cpbuf[1] + img.cols + 1;
- memset(buf[0], 0, img.cols*3);
-
-
- for(i = 3; i < img.rows-2; i++)
- {
-
- const uchar* ptr = img.ptr<uchar>(i) + 3;
-
- uchar* curr = buf[(i - 3)%3];
-
- int* cornerpos = cpbuf[(i - 3)%3];
- memset(curr, 0, img.cols);
- int ncorners = 0;
-
- if( i < img.rows - 3 )
- {
-
- j = 3;
- #if CV_SSE2
- if( patternSize == 16 )
- {
- for(; j < img.cols - 16 - 3; j += 16, ptr += 16)
- {
- __m128i m0, m1;
- __m128i v0 = _mm_loadu_si128((const __m128i*)ptr);
- __m128i v1 = _mm_xor_si128(_mm_subs_epu8(v0, t), delta);
- v0 = _mm_xor_si128(_mm_adds_epu8(v0, t), delta);
-
- __m128i x0 = _mm_sub_epi8(_mm_loadu_si128((const __m128i*)(ptr + pixel[0])), delta);
- __m128i x1 = _mm_sub_epi8(_mm_loadu_si128((const __m128i*)(ptr + pixel[quarterPatternSize])), delta);
- __m128i x2 = _mm_sub_epi8(_mm_loadu_si128((const __m128i*)(ptr + pixel[2*quarterPatternSize])), delta);
- __m128i x3 = _mm_sub_epi8(_mm_loadu_si128((const __m128i*)(ptr + pixel[3*quarterPatternSize])), delta);
- m0 = _mm_and_si128(_mm_cmpgt_epi8(x0, v0), _mm_cmpgt_epi8(x1, v0));
- m1 = _mm_and_si128(_mm_cmpgt_epi8(v1, x0), _mm_cmpgt_epi8(v1, x1));
- m0 = _mm_or_si128(m0, _mm_and_si128(_mm_cmpgt_epi8(x1, v0), _mm_cmpgt_epi8(x2, v0)));
- m1 = _mm_or_si128(m1, _mm_and_si128(_mm_cmpgt_epi8(v1, x1), _mm_cmpgt_epi8(v1, x2)));
- m0 = _mm_or_si128(m0, _mm_and_si128(_mm_cmpgt_epi8(x2, v0), _mm_cmpgt_epi8(x3, v0)));
- m1 = _mm_or_si128(m1, _mm_and_si128(_mm_cmpgt_epi8(v1, x2), _mm_cmpgt_epi8(v1, x3)));
- m0 = _mm_or_si128(m0, _mm_and_si128(_mm_cmpgt_epi8(x3, v0), _mm_cmpgt_epi8(x0, v0)));
- m1 = _mm_or_si128(m1, _mm_and_si128(_mm_cmpgt_epi8(v1, x3), _mm_cmpgt_epi8(v1, x0)));
- m0 = _mm_or_si128(m0, m1);
- int mask = _mm_movemask_epi8(m0);
- if( mask == 0 )
- continue;
- if( (mask & 255) == 0 )
- {
- j -= 8;
- ptr -= 8;
- continue;
- }
-
- __m128i c0 = _mm_setzero_si128(), c1 = c0, max0 = c0, max1 = c0;
- for( k = 0; k < N; k++ )
- {
- __m128i x = _mm_xor_si128(_mm_loadu_si128((const __m128i*)(ptr + pixel[k])), delta);
- m0 = _mm_cmpgt_epi8(x, v0);
- m1 = _mm_cmpgt_epi8(v1, x);
-
- c0 = _mm_and_si128(_mm_sub_epi8(c0, m0), m0);
- c1 = _mm_and_si128(_mm_sub_epi8(c1, m1), m1);
-
- max0 = _mm_max_epu8(max0, c0);
- max1 = _mm_max_epu8(max1, c1);
- }
-
- max0 = _mm_max_epu8(max0, max1);
- int m = _mm_movemask_epi8(_mm_cmpgt_epi8(max0, K16));
-
- for( k = 0; m > 0 && k < 16; k++, m >>= 1 )
- if(m & 1)
- {
- cornerpos[ncorners++] = j+k;
- if(nonmax_suppression)
- curr[j+k] = (uchar)cornerScore<patternSize>(ptr+k, pixel, threshold);
- }
- }
- }
- #endif
- for( ; j < img.cols - 3; j++, ptr++ )
- {
-
- int v = ptr[0];
-
- const uchar* tab = &threshold_tab[0] - v + 255;
-
-
-
-
- int d = tab[ptr[pixel[0]]] | tab[ptr[pixel[8]]];
-
- if( d == 0 )
- continue;
-
- d &= tab[ptr[pixel[2]]] | tab[ptr[pixel[10]]];
- d &= tab[ptr[pixel[4]]] | tab[ptr[pixel[12]]];
- d &= tab[ptr[pixel[6]]] | tab[ptr[pixel[14]]];
-
- if( d == 0 )
- continue;
-
- d &= tab[ptr[pixel[1]]] | tab[ptr[pixel[9]]];
- d &= tab[ptr[pixel[3]]] | tab[ptr[pixel[11]]];
- d &= tab[ptr[pixel[5]]] | tab[ptr[pixel[13]]];
- d &= tab[ptr[pixel[7]]] | tab[ptr[pixel[15]]];
-
- if( d & 1 )
- {
-
- int vt = v - threshold, count = 0;
-
- for( k = 0; k < N; k++ )
- {
- int x = ptr[pixel[k]];
- if(x < vt)
- {
-
- if( ++count > K )
- {
-
-
- cornerpos[ncorners++] = j;
-
- if(nonmax_suppression)
- curr[j] = (uchar)cornerScore<patternSize>(ptr, pixel, threshold);
- break;
- }
- }
- else
- count = 0;
- }
- }
-
- if( d & 2 )
- {
-
- int vt = v + threshold, count = 0;
-
- for( k = 0; k < N; k++ )
- {
- int x = ptr[pixel[k]];
- if(x > vt)
- {
-
- if( ++count > K )
- {
-
-
- cornerpos[ncorners++] = j;
-
- if(nonmax_suppression)
- curr[j] = (uchar)cornerScore<patternSize>(ptr, pixel, threshold);
- break;
- }
- }
- else
- count = 0;
- }
- }
- }
- }
-
- cornerpos[-1] = ncorners;
-
- if( i == 3 )
- continue;
-
-
- const uchar* prev = buf[(i - 4 + 3)%3];
- const uchar* pprev = buf[(i - 5 + 3)%3];
-
- cornerpos = cpbuf[(i - 4 + 3)%3];
-
- ncorners = cornerpos[-1];
-
- for( k = 0; k < ncorners; k++ )
- {
- j = cornerpos[k];
- int score = prev[j];
-
- if( !nonmax_suppression ||
- (score > prev[j+1] && score > prev[j-1] &&
- score > pprev[j-1] && score > pprev[j] && score > pprev[j+1] &&
- score > curr[j-1] && score > curr[j] && score > curr[j+1]) )
- {
- keypoints.push_back(KeyPoint((float)j, (float)(i-1), 7.f, -1, (float)score));
- }
- }
- }
- }
在该函数内,对阈值列表理解起来可能有一定的难度,下面我们举一个具体的例子来进行讲解。设我们选取的阈值threshold为30,则根据
for( i = -255; i <= 255; i++ )
threshold_tab[i+255] = (uchar)(i < -threshold ? 1 : i > threshold? 2 : 0);
我们可以从-255到255一共分为3段:-255~-30,-30~30,30~255。由于数组的序号不能小于0,因此在给threshold_tab数组赋值上,序号要加上255,这样区间就变为:0~225,225~285,285~510,而这三个区间对应的值分别为1,0和2。设我们当前像素值为40,则根据
const uchar* tab = &threshold_tab[0] -v + 255;
tab的指针指向threshold_tab[215]处,因为255-40=215。这样在圆周像素与当前像素进行比较时,使用的是threshold_tab[215]以后的值。例如圆周上编号为0的像素值为5,则该值在阈值列表中的位置是threshold_tab[215 + 5],是threshold_tab[220]。它在阈值列表中的第一段,即threshold_tab[220] = 1,说明编号为0的像素满足角点条件2。我们来验证一下:5 < 40 – 30,确实满足条件2;如果圆周上编号为1的像素值为80,则该值在阈值列表中的位置是threshold_tab[295](即215 + 80 = 295),而它在阈值列表中的第三段,即threshold_tab[295] = 2,因此它满足角点条件1,即80 > 40 + 30;而如果圆周上编号为2的像素值为45,则threshold_tab[260] = 0,它不满足角点条件,即40 – 30 < 45 < 40 + 30。
在函数模板FAST_t中还用到了两个重要的函数——makeOffsets和cornerScore,一个是用于计算圆周像素的偏移量,另一个用于非极大值抑制的第一步,计算得分函数。这两个函数都在sources/modules/features2d/src/fast_score.cpp文件内定义,而且代码编写得都很有特点,下面就来讲解一下。
计算圆周像素的偏移量:
- void makeOffsets(int pixel[25], int rowStride, int patternSize)
- {
-
- static const int offsets16[][2] =
- {
- {0, 3}, { 1, 3}, { 2, 2}, { 3, 1}, { 3, 0}, { 3, -1}, { 2, -2}, { 1, -3},
- {0, -3}, {-1, -3}, {-2, -2}, {-3, -1}, {-3, 0}, {-3, 1}, {-2, 2}, {-1, 3}
- };
-
- static const int offsets12[][2] =
- {
- {0, 2}, { 1, 2}, { 2, 1}, { 2, 0}, { 2, -1}, { 1, -2},
- {0, -2}, {-1, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-1, 2}
- };
-
- static const int offsets8[][2] =
- {
- {0, 1}, { 1, 1}, { 1, 0}, { 1, -1},
- {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}
- };
-
- const int (*offsets)[2] = patternSize == 16 ? offsets16 :
- patternSize == 12 ? offsets12 :
- patternSize == 8 ? offsets8 : 0;
-
- CV_Assert(pixel && offsets);
-
- int k = 0;
-
- for( ; k < patternSize; k++ )
- pixel[k] = offsets[k][0] + offsets[k][1] * rowStride;
-
- for( ; k < 25; k++ )
- pixel[k] = pixel[k - patternSize];
- }
计算得分函数,cornerScore函数是以圆周像素为16点为例而编写的:
- template<>
- int cornerScore<16>(const uchar* ptr, const int pixel[], int threshold)
- {
- const int K = 8, N = K*3 + 1;
-
- int k, v = ptr[0];
- short d[N];
-
- for( k = 0; k < N; k++ )
- d[k] = (short)(v - ptr[pixel[k]]);
-
- #if CV_SSE2
- __m128i q0 = _mm_set1_epi16(-1000), q1 = _mm_set1_epi16(1000);
- for( k = 0; k < 16; k += 8 )
- {
- __m128i v0 = _mm_loadu_si128((__m128i*)(d+k+1));
- __m128i v1 = _mm_loadu_si128((__m128i*)(d+k+2));
- __m128i a = _mm_min_epi16(v0, v1);
- __m128i b = _mm_max_epi16(v0, v1);
- v0 = _mm_loadu_si128((__m128i*)(d+k+3));
- a = _mm_min_epi16(a, v0);
- b = _mm_max_epi16(b, v0);
- v0 = _mm_loadu_si128((__m128i*)(d+k+4));
- a = _mm_min_epi16(a, v0);
- b = _mm_max_epi16(b, v0);
- v0 = _mm_loadu_si128((__m128i*)(d+k+5));
- a = _mm_min_epi16(a, v0);
- b = _mm_max_epi16(b, v0);
- v0 = _mm_loadu_si128((__m128i*)(d+k+6));
- a = _mm_min_epi16(a, v0);
- b = _mm_max_epi16(b, v0);
- v0 = _mm_loadu_si128((__m128i*)(d+k+7));
- a = _mm_min_epi16(a, v0);
- b = _mm_max_epi16(b, v0);
- v0 = _mm_loadu_si128((__m128i*)(d+k+8));
- a = _mm_min_epi16(a, v0);
- b = _mm_max_epi16(b, v0);
- v0 = _mm_loadu_si128((__m128i*)(d+k));
- q0 = _mm_max_epi16(q0, _mm_min_epi16(a, v0));
- q1 = _mm_min_epi16(q1, _mm_max_epi16(b, v0));
- v0 = _mm_loadu_si128((__m128i*)(d+k+9));
- q0 = _mm_max_epi16(q0, _mm_min_epi16(a, v0));
- q1 = _mm_min_epi16(q1, _mm_max_epi16(b, v0));
- }
- q0 = _mm_max_epi16(q0, _mm_sub_epi16(_mm_setzero_si128(), q1));
- q0 = _mm_max_epi16(q0, _mm_unpackhi_epi64(q0, q0));
- q0 = _mm_max_epi16(q0, _mm_srli_si128(q0, 4));
- q0 = _mm_max_epi16(q0, _mm_srli_si128(q0, 2));
- threshold = (short)_mm_cvtsi128_si32(q0) - 1;
- #else
-
- int a0 = threshold;
-
- for( k = 0; k < 16; k += 2 )
- {
-
- int a = std::min((int)d[k+1], (int)d[k+2]);
- a = std::min(a, (int)d[k+3]);
-
- if( a <= a0 )
- continue;
-
-
- a = std::min(a, (int)d[k+4]);
- a = std::min(a, (int)d[k+5]);
- a = std::min(a, (int)d[k+6]);
- a = std::min(a, (int)d[k+7]);
- a = std::min(a, (int)d[k+8]);
-
- a0 = std::max(a0, std::min(a, (int)d[k]));
- a0 = std::max(a0, std::min(a, (int)d[k+9]));
- }
-
- int b0 = -a0;
- for( k = 0; k < 16; k += 2 )
- {
- int b = std::max((int)d[k+1], (int)d[k+2]);
- b = std::max(b, (int)d[k+3]);
- b = std::max(b, (int)d[k+4]);
- b = std::max(b, (int)d[k+5]);
- if( b >= b0 )
- continue;
- b = std::max(b, (int)d[k+6]);
- b = std::max(b, (int)d[k+7]);
- b = std::max(b, (int)d[k+8]);
-
- b0 = std::min(b0, std::max(b, (int)d[k]));
- b0 = std::min(b0, std::max(b, (int)d[k+9]));
- }
-
- threshold = -b0-1;
- #endif
-
- #if VERIFY_CORNERS
- testCorner(ptr, pixel, K, N, threshold);
- #endif
-
- return threshold;
- }
可以有两种方法实现FAST角点检测,即直接调用FAST函数,和使用特征点检测类的方式。这两种方法我们都给出实例。
首先是直接调用FAST函数的应用程序:
- #include "opencv2/core/core.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/features2d/features2d.hpp" //需要添加该头文件
- #include <iostream>
- using namespace cv;
- using namespace std;
-
- int main( int argc, char** argv )
- {
- Mat src, gray;
- src=imread("building.jpg");
- if( !src.data )
- return -1;
-
- cvtColor( src, gray, CV_BGR2GRAY );
-
- std::vector<KeyPoint> keyPoints;
-
- FAST(gray, keyPoints, 55);
-
- int total = keyPoints.size();
-
- for(int i = 0; I < total; i++)
- {
- circle( src, Point( (int)keyPoints[i].pt.x, (int)keyPoints[i].pt.y ), 5, Scalar(0,0,255), -1, 8, 0 );
- }
-
- namedWindow( "Corners", CV_WINDOW_AUTOSIZE );
- imshow( "Corners", src );
-
- waitKey(0);
- return 0;
- }
下面是应用FeatureDetector类进行的FAST角点检测,使用的类为FastFeatureDetector,它继承于FeatureDetector,即:
class FastFeatureDetector : publicFeatureDetector
{
public:
FastFeatureDetector( int threshold=1, boolnonmaxSuppression=true, type=FastFeatureDetector::TYPE_9_16 );
virtual void read( const FileNode& fn);
virtual void write( FileStorage& fs )const;
protected:
...
};
从上面的定义可以看出,FastFeatureDetector的构造函数默认的阈值为1,进行非极大值抑制,以及圆周像素为16个。下面是具体的应用程序:
- #include "opencv2/core/core.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/features2d/features2d.hpp"
- #include <iostream>
- using namespace cv;
- using namespace std;
-
- int main( int argc, char** argv )
- {
- Mat src, gray,color_edge;
- src=imread("building.jpg");
- if( !src.data )
- return -1;
-
- std::vector<KeyPoint> keyPoints;
-
- FastFeatureDetector fast(55);
-
- fast.detect(src,keyPoints);
-
- drawKeypoints(src, keyPoints, src, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_OVER_OUTIMG);
- imshow("FAST feature", src);
- waitKey(0);
- return 0;
- }