现在的位置: 首页 > 综合 > 正文

Opencv2.4.9源码分析——FAST

2018年04月22日 ⁄ 综合 ⁄ 共 14498字 ⁄ 字号 评论关闭

FAST(Features fromaccelerated segment test)是一种角点检测方法,它可以用于特征点的提取,并完成跟踪和映射物体。FAST角点检测算法最初是由Edward Rosten和Tom Drummond提出,该算法最突出的优点是它的计算效率。正如它的缩写名字,它很快而且事实上它比其他著名的特征点提取方法(如SIFT,SUSAN,Harris)都要快。而且如果应用机器学习方法的话,该算法能够取得更佳的效果。正因为它的快速特点,FAST角点检测方法非常适用于实时视频处理的领域。

该算法的基本原理是使用圆周长为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像素点,如果I1I9在[Ipt Ip
+ t]之间,则圆心肯定不是角点,否则再检查5和13像素点。如果这4个像素中至少有三个像素满足亮度高于Ip+t或低于Ipt,则进一步检查圆周上其余像素点。

以上方法还是有不够鲁棒的地方,但可以通过机器学习和非极大值抑制的方法来增强鲁棒性。由于opencv中相关的函数没有使用机器学习,因此我们这里只介绍非极大值抑制的方法。由于分割测试并没有计算角点响应函数,因此常规的非极大值抑制方法并不适用于FAST算法。下面是FAST的非极大值抑制方法:

1、计算得分函数,它的值V是特征点与其圆周上16个像素点的绝对差值中所有连续10个像素中的最小值的最大值,而且该值还要大于阈值t

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函数
    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();    //提取出输入图像矩阵
    //K为圆周连续像素的个数
    //N用于循环圆周的像素点,因为要首尾连接,所以N要比实际圆周像素数量多K+1个
    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();
    //保证阈值不大于255,不小于0
    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
    // threshold_tab为阈值列表,在进行阈值比较的时候,只需查该表即可
    uchar threshold_tab[512];
    /*为阈值列表赋值,该表分为三段:第一段从threshold_tab[0]至threshold_tab[255 - threshold],值为1,落在该区域的值表示满足角点判断条件2;第二段从threshold_tab[255 – threshold]至threshold_tab[255 + threshold],值为0,落在该区域的值表示不是角点;第三段从threshold_tab[255 + threshold]至threshold_tab[511],值为2,落在该区域的值表示满足角点判断条件1*/
    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[1]和buf[2]分别表示图像的前一行、当前行和后一行。因为在非极大值抑制的步骤2中,是要在3×3的角点邻域内进行比较,因此需要三行的图像数据。因为只有得到了当前行的数据,所以对于上一行来说,才凑够了连续三行的数据,因此输出的非极大值抑制的结果是上一行数据的处理结果*/
    buf[0] = _buf; buf[1] = buf[0] + img.cols; buf[2] = buf[1] + img.cols;
    //cpbuf存储角点的坐标位置,也是需要连续三行的数据
    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);    //buf数组内存清零
    //遍历整幅图像像素,寻找角点
    //由于圆的半径为3个像素,因此图像的四周边界都留出3个像素的宽度
    for(i = 3; i < img.rows-2; i++)
    {
        //得到图像行的首地址指针
        const uchar* ptr = img.ptr<uchar>(i) + 3;
        //得到buf的某个数组,用于存储当前行的得分函数的值V
        uchar* curr = buf[(i - 3)%3];
        //得到cpbuf的某个数组,用于存储当前行的角点坐标位置
        int* cornerpos = cpbuf[(i - 3)%3];
        memset(curr, 0, img.cols);    //清零
        int ncorners = 0;    //检测到的角点数量

        if( i < img.rows - 3 )
        {
            //每一行都留出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;
                //pixel[0]表示圆周上编号为0的像素相对于圆心坐标的偏移量
                //ptr[pixel[0]表示圆周上编号为0的像素值
                //tab[ptr[pixel[0]]]表示相对于当前像素(即圆心)圆周上编号为0的像素值在阈值列表threshold_tab中所查询得到的值,如果为1,说明I0 < Ip - t,如果为2,说明I0 > Ip + t,如果为0,说明 Ip – t < I0 < Ip + t。因此通过tab,就可以得到当前像素是否满足角点条件。
                //编号为0和8(即直径在圆周上的两个像素点)在列表中的值相或后得到d。d=0说明编号为0和8的值都是0;d=1说明编号为0和8的值至少有一个为1,而另一个不能为2;d=2说明编号为0和8的值至少有一个为2,而另一个不能为1;d=3说明编号为0和8的值有一个为1,另一个为2。只可能有这四种情况。
                int d = tab[ptr[pixel[0]]] | tab[ptr[pixel[8]]];
                //d=0说明圆周上不可能有连续12个像素满足角点条件,因此当前值一定不是角点,所以退出此次循环,进入下一次循环
                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]]];
                //d=0说明上述d中至少有一个d为0,所以肯定不是角点;另一种情况是一个d为2,而另一个d为1,相与后也为0,这说明一个是满足角点条件1,而另一个满足角点条件2,所以肯定也不会有连续12个像素满足同一个角点条件的,因此也一定不是角点。
                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条件,则说明有可能满足角点条件2
                if( d & 1 )
                {
                    //vt为真正的角点条件,即Ip – t,count为连续像素的计数值
                    int vt = v - threshold, count = 0;
                    //遍历整个圆周
                    for( k = 0; k < N; k++ )
                    {
                        int x = ptr[pixel[k]];    //提取出圆周上的像素值
                        if(x < vt)    //如果满足条件2
                        {
                            //连续计数,并判断是否大于K(K为圆周像素的一半)
                            if( ++count > K )
                            {
                                //进入该if语句,说明已经得到一个角点
                                //保存该点的位置,并把当前行的角点数加1
                                cornerpos[ncorners++] = j;
                                 //进行非极大值抑制的第一步,计算得分函数
                                if(nonmax_suppression)   
                                    curr[j] = (uchar)cornerScore<patternSize>(ptr, pixel, threshold);
                                break;    //退出循环
                            }
                        }
                        else
                            count = 0;    //连续像素的计数值清零
                    }
                }
                //如果满足if条件,则说明有可能满足角点条件1
                if( d & 2 )
                {
                    //vt为真正的角点条件,即Ip + t,count为连续像素的计数值
                    int vt = v + threshold, count = 0;
                    //遍历整个圆周
                    for( k = 0; k < N; k++ )
                    {
                        int x = ptr[pixel[k]];    //提取出圆周上的像素值
                        if(x > vt)    //如果满足条件1
                        {
                            //连续计数,并判断是否大于K(K为圆周像素的一半)
                            if( ++count > K )
                            {
                                //进入该if语句,说明已经得到一个角点
                                //保存该点的位置,并把当前行的角点数加1
                                cornerpos[ncorners++] = j;
                                 //进行非极大值抑制的第一步,计算得分函数
                                if(nonmax_suppression)
                                    curr[j] = (uchar)cornerScore<patternSize>(ptr, pixel, threshold);
                                break;    //退出循环
                            }
                        }
                        else
                            count = 0;    //连续像素的计数值清零
                    }
                }
            }
        }
        //保存当前行所检测到的角点数
        cornerpos[-1] = ncorners;
        //i=3说明只仅仅计算了一行的数据,还不能进行非极大值抑制的第二步,所以不进行下面代码的操作,直接进入下一次循环
        if( i == 3 )
            continue;
        //以下代码是进行非极大值抑制的第二步,即在3×3的角点邻域内对得分函数的值进行非极大值抑制。因为经过上面代码的计算,已经得到了当前行的数据,所以可以进行上一行的非极大值抑制。因此下面的代码进行的是上一行的非极大值抑制。
        //提取出上一行和上两行的图像像素
        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];    //得到该角点的得分函数值
            //在3×3的角点邻域内,计算当前角点是否为最大值,如果是则压入特性值向量中
            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)
{
    //分别定义三个数组,用于表示patternSize为16,12和8时,圆周像素对于圆心的相对坐标位置
    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}
    };
    //根据patternSize值,得到具体应用上面定义的哪个数组
    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;
    //v为当前像素值
    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
    //a0为阈值
    int a0 = threshold;
    //满足角点条件2时,更新阈值
    for( k = 0; k < 16; k += 2 )
    {
        //a为d[k+1],d[k+2]和d[k+3]中的最小值
        int a = std::min((int)d[k+1], (int)d[k+2]);
        a = std::min(a, (int)d[k+3]);
        //如果a小于阈值,则进行下一次循环
        if( a <= a0 )
            continue;
        //更新阈值
        //a为从d[k+1]到d[k+8]中的最小值
        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]);
        //从d[k]到d[k+9]中的最小值与a0比较,哪个大,哪个作为新的阈值
        a0 = std::max(a0, std::min(a, (int)d[k]));
        a0 = std::max(a0, std::min(a, (int)d[k+9]));
    }
    //满足角点条件1时,更新阈值
    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 );
    //定义特征点KeyPoint向量
	std::vector<KeyPoint> keyPoints;  
    //调用FAST函数,阈值选为55
	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;  
    //创建对象,阈值设为55
    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;
}

抱歉!评论已关闭.