FAST(Features fromaccelerated segment test)源碼分析

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組成,IIt

條件2:集合S由圓周上N個連續的像素x組成,IIp - t

N一般選擇爲12。

在一幅圖像中,非角點往往是佔多數,而且非角點檢測要比角點檢測容易得多,因此首先剔除掉非角點將大大提高角點檢測速度。由於N爲12,所以編號爲1,5,9,13的這4個圓周像素點中應該至少有三個像素點滿足角點條件,圓心纔有可能是角點。因此首先檢查1和9像素點,如果I1I9在[Ip – t It]之間,則圓心肯定不是角點,否則再檢查5和13像素點。如果這4個像素中至少有三個像素滿足亮度高於Ip+t或低於Ip – t,則進一步檢查圓周上其餘像素點。

以上方法還是有不夠魯棒的地方,但可以通過機器學習和非極大值抑制的方法來增強魯棒性。由於opencv中相關的函數沒有使用機器學習,因此我們這裏只介紹非極大值抑制的方法。由於分割測試並沒有計算角點響應函數,因此常規的非極大值抑制方法並不適用於FAST算法。下面是FAST的非極大值抑制方法:

1、計算得分函數,它的值V是特徵點與其圓周上16個像素點的絕對差值中的最小值;

2、在3×3的特徵點鄰域內(而不是圖像鄰域),比較V;

3、剔除掉非極大值的特徵點。

FAST角點檢測方法的具體步驟爲:

1、在圓周上的部分像素點上,進行非角點的檢測;

2、如果初步判斷是角點,則在圓周上的全部像素點上進行角點檢測;

3、對角點進行非極大值抑制,得到角點輸出。

 

在opencv中,實現FAST算法的核心函數有兩個,它們的原型爲:

  1. void FAST(InputArray image, vector<KeyPoint>& keypoints, int threshold, bool nonmaxSuppression=true )  
  2. 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文件內定義的:

  1. void FAST(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression)  
  2. {  
  3.     //調用FASTX函數  
  4.     FASTX(_img, keypoints, threshold, nonmax_suppression, FastFeatureDetector::TYPE_9_16);  
  5. }  
FASTX函數的作用是調用一個函數模板,模板的參數值是根據參數type的不同而定義的所使用的圓周像素的個數:

  1. void FASTX(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression, int type)  
  2. {  
  3.   switch(type) {  
  4.     case FastFeatureDetector::TYPE_5_8:  
  5.       FAST_t<8>(_img, keypoints, threshold, nonmax_suppression);  
  6.       break;  
  7.     case FastFeatureDetector::TYPE_7_12:  
  8.       FAST_t<12>(_img, keypoints, threshold, nonmax_suppression);  
  9.       break;  
  10.     case FastFeatureDetector::TYPE_9_16:  
  11. #ifdef HAVE_TEGRA_OPTIMIZATION  
  12.       if(tegra::FAST(_img, keypoints, threshold, nonmax_suppression))  
  13.         break;  
  14. #endif  
  15.       FAST_t<16>(_img, keypoints, threshold, nonmax_suppression);  
  16.       break;  
  17.   }  
  18. }  
下面是函數模板FAST_t,在這裏我們以patternSize=16爲例進行講解:

  1. template<int patternSize>  
  2. void FAST_t(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression)  
  3. {  
  4.     Mat img = _img.getMat();    //提取出輸入圖像矩陣  
  5.     //K爲圓周連續像素的個數  
  6.     //N用於循環圓周的像素點,因爲要首尾連接,所以N要比實際圓周像素數量多K+1個  
  7.     const int K = patternSize/2, N = patternSize + K + 1;  
  8. #if CV_SSE2  
  9.     const int quarterPatternSize = patternSize/4;  
  10.     (void)quarterPatternSize;  
  11. #endif  
  12.     int i, j, k, pixel[25];  
  13.     //找到圓周像素點相對於圓心的偏移量  
  14.     makeOffsets(pixel, (int)img.step, patternSize);  
  15.     //特徵點向量清零  
  16.     keypoints.clear();  
  17.     //保證閾值不大於255,不小於0  
  18.     threshold = std::min(std::max(threshold, 0), 255);  
  19.   
  20. #if CV_SSE2  
  21.     __m128i delta = _mm_set1_epi8(-128), t = _mm_set1_epi8((char)threshold), K16 = _mm_set1_epi8((char)K);  
  22.     (void)K16;  
  23.     (void)delta;  
  24.     (void)t;  
  25. #endif  
  26.     // threshold_tab爲閾值列表,在進行閾值比較的時候,只需查該表即可  
  27.     uchar threshold_tab[512];  
  28.     /*爲閾值列表賦值,該表分爲三段:第一段從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*/  
  29.     for( i = -255; i <= 255; i++ )  
  30.         threshold_tab[i+255] = (uchar)(i < -threshold ? 1 : i > threshold ? 2 : 0);  
  31.     //開闢一段內存空間  
  32.     AutoBuffer<uchar> _buf((img.cols+16)*3*(sizeof(int) + sizeof(uchar)) + 128);  
  33.     uchar* buf[3];  
  34.     /*buf[0、buf[1]和buf[2]分別表示圖像的前一行、當前行和後一行。因爲在非極大值抑制的步驟2中,是要在3×3的角點鄰域內進行比較,因此需要三行的圖像數據。因爲只有得到了當前行的數據,所以對於上一行來說,才湊夠了連續三行的數據,因此輸出的非極大值抑制的結果是上一行數據的處理結果*/  
  35.     buf[0] = _buf; buf[1] = buf[0] + img.cols; buf[2] = buf[1] + img.cols;  
  36.     //cpbuf存儲角點的座標位置,也是需要連續三行的數據  
  37.     int* cpbuf[3];  
  38.     cpbuf[0] = (int*)alignPtr(buf[2] + img.cols, sizeof(int)) + 1;  
  39.     cpbuf[1] = cpbuf[0] + img.cols + 1;  
  40.     cpbuf[2] = cpbuf[1] + img.cols + 1;  
  41.     memset(buf[0], 0, img.cols*3);    //buf數組內存清零  
  42.     //遍歷整幅圖像像素,尋找角點  
  43.     //由於圓的半徑爲3個像素,因此圖像的四周邊界都留出3個像素的寬度  
  44.     for(i = 3; i < img.rows-2; i++)  
  45.     {  
  46.         //得到圖像行的首地址指針  
  47.         const uchar* ptr = img.ptr<uchar>(i) + 3;  
  48.         //得到buf的某個數組,用於存儲當前行的得分函數的值V  
  49.         uchar* curr = buf[(i - 3)%3];  
  50.         //得到cpbuf的某個數組,用於存儲當前行的角點座標位置  
  51.         int* cornerpos = cpbuf[(i - 3)%3];  
  52.         memset(curr, 0, img.cols);    //清零  
  53.         int ncorners = 0;    //檢測到的角點數量  
  54.   
  55.         if( i < img.rows - 3 )  
  56.         {  
  57.             //每一行都留出3個像素的寬度  
  58.             j = 3;  
  59.     #if CV_SSE2  
  60.             if( patternSize == 16 )  
  61.             {  
  62.             for(; j < img.cols - 16 - 3; j += 16, ptr += 16)  
  63.             {  
  64.                 __m128i m0, m1;  
  65.                 __m128i v0 = _mm_loadu_si128((const __m128i*)ptr);  
  66.                 __m128i v1 = _mm_xor_si128(_mm_subs_epu8(v0, t), delta);  
  67.                 v0 = _mm_xor_si128(_mm_adds_epu8(v0, t), delta);  
  68.   
  69.                 __m128i x0 = _mm_sub_epi8(_mm_loadu_si128((const __m128i*)(ptr + pixel[0])), delta);  
  70.                 __m128i x1 = _mm_sub_epi8(_mm_loadu_si128((const __m128i*)(ptr + pixel[quarterPatternSize])), delta);  
  71.                 __m128i x2 = _mm_sub_epi8(_mm_loadu_si128((const __m128i*)(ptr + pixel[2*quarterPatternSize])), delta);  
  72.                 __m128i x3 = _mm_sub_epi8(_mm_loadu_si128((const __m128i*)(ptr + pixel[3*quarterPatternSize])), delta);  
  73.                 m0 = _mm_and_si128(_mm_cmpgt_epi8(x0, v0), _mm_cmpgt_epi8(x1, v0));  
  74.                 m1 = _mm_and_si128(_mm_cmpgt_epi8(v1, x0), _mm_cmpgt_epi8(v1, x1));  
  75.                 m0 = _mm_or_si128(m0, _mm_and_si128(_mm_cmpgt_epi8(x1, v0), _mm_cmpgt_epi8(x2, v0)));  
  76.                 m1 = _mm_or_si128(m1, _mm_and_si128(_mm_cmpgt_epi8(v1, x1), _mm_cmpgt_epi8(v1, x2)));  
  77.                 m0 = _mm_or_si128(m0, _mm_and_si128(_mm_cmpgt_epi8(x2, v0), _mm_cmpgt_epi8(x3, v0)));  
  78.                 m1 = _mm_or_si128(m1, _mm_and_si128(_mm_cmpgt_epi8(v1, x2), _mm_cmpgt_epi8(v1, x3)));  
  79.                 m0 = _mm_or_si128(m0, _mm_and_si128(_mm_cmpgt_epi8(x3, v0), _mm_cmpgt_epi8(x0, v0)));  
  80.                 m1 = _mm_or_si128(m1, _mm_and_si128(_mm_cmpgt_epi8(v1, x3), _mm_cmpgt_epi8(v1, x0)));  
  81.                 m0 = _mm_or_si128(m0, m1);  
  82.                 int mask = _mm_movemask_epi8(m0);  
  83.                 if( mask == 0 )  
  84.                     continue;  
  85.                 if( (mask & 255) == 0 )  
  86.                 {  
  87.                     j -= 8;  
  88.                     ptr -= 8;  
  89.                     continue;  
  90.                 }  
  91.   
  92.                 __m128i c0 = _mm_setzero_si128(), c1 = c0, max0 = c0, max1 = c0;  
  93.                 for( k = 0; k < N; k++ )  
  94.                 {  
  95.                     __m128i x = _mm_xor_si128(_mm_loadu_si128((const __m128i*)(ptr + pixel[k])), delta);  
  96.                     m0 = _mm_cmpgt_epi8(x, v0);  
  97.                     m1 = _mm_cmpgt_epi8(v1, x);  
  98.   
  99.                     c0 = _mm_and_si128(_mm_sub_epi8(c0, m0), m0);  
  100.                     c1 = _mm_and_si128(_mm_sub_epi8(c1, m1), m1);  
  101.   
  102.                     max0 = _mm_max_epu8(max0, c0);  
  103.                     max1 = _mm_max_epu8(max1, c1);  
  104.                 }  
  105.   
  106.                 max0 = _mm_max_epu8(max0, max1);  
  107.                 int m = _mm_movemask_epi8(_mm_cmpgt_epi8(max0, K16));  
  108.   
  109.                 for( k = 0; m > 0 && k < 16; k++, m >>= 1 )  
  110.                     if(m & 1)  
  111.                     {  
  112.                         cornerpos[ncorners++] = j+k;  
  113.                         if(nonmax_suppression)  
  114.                             curr[j+k] = (uchar)cornerScore<patternSize>(ptr+k, pixel, threshold);  
  115.                     }  
  116.             }  
  117.             }  
  118.     #endif  
  119.             for( ; j < img.cols - 3; j++, ptr++ )  
  120.             {  
  121.                 //當前像素的灰度值  
  122.                 int v = ptr[0];  
  123.                 //由當前像素的灰度值,確定其在閾值列表中的位置  
  124.                 const uchar* tab = &threshold_tab[0] - v + 255;  
  125.                 //pixel[0]表示圓周上編號爲0的像素相對於圓心座標的偏移量  
  126.                 //ptr[pixel[0]表示圓周上編號爲0的像素值  
  127.                 //tab[ptr[pixel[0]]]表示相對於當前像素(即圓心)圓周上編號爲0的像素值在閾值列表threshold_tab中所查詢得到的值,如果爲1,說明I0 < Ip - t,如果爲2,說明I0 > Ip + t,如果爲0,說明 Ip – t < I0 < Ip + t。因此通過tab,就可以得到當前像素是否滿足角點條件。  
  128.                 //編號爲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。只可能有這四種情況。  
  129.                 int d = tab[ptr[pixel[0]]] | tab[ptr[pixel[8]]];  
  130.                 //d=0說明圓周上不可能有連續12個像素滿足角點條件,因此當前值一定不是角點,所以退出此次循環,進入下一次循環  
  131.                 if( d == 0 )  
  132.                     continue;  
  133.                 //繼續進行其他直徑上兩個像素點的判斷  
  134.                 d &= tab[ptr[pixel[2]]] | tab[ptr[pixel[10]]];  
  135.                 d &= tab[ptr[pixel[4]]] | tab[ptr[pixel[12]]];  
  136.                 d &= tab[ptr[pixel[6]]] | tab[ptr[pixel[14]]];  
  137.                 //d=0說明上述d中至少有一個d爲0,所以肯定不是角點;另一種情況是一個d爲2,而另一個d爲1,相與後也爲0,這說明一個是滿足角點條件1,而另一個滿足角點條件2,所以肯定也不會有連續12個像素滿足同一個角點條件的,因此也一定不是角點。  
  138.                 if( d == 0 )  
  139.                     continue;  
  140.                 //繼續判斷圓周上剩餘的像素點  
  141.                 d &= tab[ptr[pixel[1]]] | tab[ptr[pixel[9]]];  
  142.                 d &= tab[ptr[pixel[3]]] | tab[ptr[pixel[11]]];  
  143.                 d &= tab[ptr[pixel[5]]] | tab[ptr[pixel[13]]];  
  144.                 d &= tab[ptr[pixel[7]]] | tab[ptr[pixel[15]]];  
  145.                 //如果滿足if條件,則說明有可能滿足角點條件2  
  146.                 if( d & 1 )  
  147.                 {  
  148.                     //vt爲真正的角點條件,即Ip – t,count爲連續像素的計數值  
  149.                     int vt = v - threshold, count = 0;  
  150.                     //遍歷整個圓周  
  151.                     for( k = 0; k < N; k++ )  
  152.                     {  
  153.                         int x = ptr[pixel[k]];    //提取出圓周上的像素值  
  154.                         if(x < vt)    //如果滿足條件2  
  155.                         {  
  156.                             //連續計數,並判斷是否大於K(K爲圓周像素的一半)  
  157.                             if( ++count > K )  
  158.                             {  
  159.                                 //進入該if語句,說明已經得到一個角點  
  160.                                 //保存該點的位置,並把當前行的角點數加1  
  161.                                 cornerpos[ncorners++] = j;  
  162.                                  //進行非極大值抑制的第一步,計算得分函數  
  163.                                 if(nonmax_suppression)     
  164.                                     curr[j] = (uchar)cornerScore<patternSize>(ptr, pixel, threshold);  
  165.                                 break;    //退出循環  
  166.                             }  
  167.                         }  
  168.                         else  
  169.                             count = 0;    //連續像素的計數值清零  
  170.                     }  
  171.                 }  
  172.                 //如果滿足if條件,則說明有可能滿足角點條件1  
  173.                 if( d & 2 )  
  174.                 {  
  175.                     //vt爲真正的角點條件,即Ip + t,count爲連續像素的計數值  
  176.                     int vt = v + threshold, count = 0;  
  177.                     //遍歷整個圓周  
  178.                     for( k = 0; k < N; k++ )  
  179.                     {  
  180.                         int x = ptr[pixel[k]];    //提取出圓周上的像素值  
  181.                         if(x > vt)    //如果滿足條件1  
  182.                         {  
  183.                             //連續計數,並判斷是否大於K(K爲圓周像素的一半)  
  184.                             if( ++count > K )  
  185.                             {  
  186.                                 //進入該if語句,說明已經得到一個角點  
  187.                                 //保存該點的位置,並把當前行的角點數加1  
  188.                                 cornerpos[ncorners++] = j;  
  189.                                  //進行非極大值抑制的第一步,計算得分函數  
  190.                                 if(nonmax_suppression)  
  191.                                     curr[j] = (uchar)cornerScore<patternSize>(ptr, pixel, threshold);  
  192.                                 break;    //退出循環  
  193.                             }  
  194.                         }  
  195.                         else  
  196.                             count = 0;    //連續像素的計數值清零  
  197.                     }  
  198.                 }  
  199.             }  
  200.         }  
  201.         //保存當前行所檢測到的角點數  
  202.         cornerpos[-1] = ncorners;  
  203.         //i=3說明只僅僅計算了一行的數據,還不能進行非極大值抑制的第二步,所以不進行下面代碼的操作,直接進入下一次循環  
  204.         if( i == 3 )  
  205.             continue;  
  206.         //以下代碼是進行非極大值抑制的第二步,即在3×3的角點鄰域內對得分函數的值進行非極大值抑制。因爲經過上面代碼的計算,已經得到了當前行的數據,所以可以進行上一行的非極大值抑制。因此下面的代碼進行的是上一行的非極大值抑制。  
  207.         //提取出上一行和上兩行的圖像像素  
  208.         const uchar* prev = buf[(i - 4 + 3)%3];  
  209.         const uchar* pprev = buf[(i - 5 + 3)%3];  
  210.         //提取出上一行所檢測到的角點位置  
  211.         cornerpos = cpbuf[(i - 4 + 3)%3];  
  212.         //提取出上一行的角點數  
  213.         ncorners = cornerpos[-1];  
  214.         //在上一行內遍歷整個檢測到的角點  
  215.         for( k = 0; k < ncorners; k++ )  
  216.         {  
  217.             j = cornerpos[k];    //得到角點的位置  
  218.             int score = prev[j];    //得到該角點的得分函數值  
  219.             //在3×3的角點鄰域內,計算當前角點是否爲最大值,如果是則壓入特性值向量中  
  220.             if( !nonmax_suppression ||  
  221.                (score > prev[j+1] && score > prev[j-1] &&  
  222.                 score > pprev[j-1] && score > pprev[j] && score > pprev[j+1] &&  
  223.                 score > curr[j-1] && score > curr[j] && score > curr[j+1]) )  
  224.             {  
  225.                 keypoints.push_back(KeyPoint((float)j, (float)(i-1), 7.f, -1, (float)score));  
  226.             }  
  227.         }  
  228.     }  
  229. }  

在該函數內,對閾值列表理解起來可能有一定的難度,下面我們舉一個具體的例子來進行講解。設我們選取的閾值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文件內定義,而且代碼編寫得都很有特點,下面就來講解一下。

計算圓周像素的偏移量:
  1. void makeOffsets(int pixel[25], int rowStride, int patternSize)  
  2. {  
  3.     //分別定義三個數組,用於表示patternSize爲16,12和8時,圓周像素對於圓心的相對座標位置  
  4.     static const int offsets16[][2] =  
  5.     {  
  6.         {0,  3}, { 1,  3}, { 2,  2}, { 3,  1}, { 3, 0}, { 3, -1}, { 2, -2}, { 1, -3},  
  7.         {0, -3}, {-1, -3}, {-2, -2}, {-3, -1}, {-3, 0}, {-3,  1}, {-2,  2}, {-1,  3}  
  8.     };  
  9.   
  10.     static const int offsets12[][2] =  
  11.     {  
  12.         {0,  2}, { 1,  2}, { 2,  1}, { 2, 0}, { 2, -1}, { 1, -2},  
  13.         {0, -2}, {-1, -2}, {-2, -1}, {-2, 0}, {-2,  1}, {-1,  2}  
  14.     };  
  15.   
  16.     static const int offsets8[][2] =  
  17.     {  
  18.         {0,  1}, { 1,  1}, { 1, 0}, { 1, -1},  
  19.         {0, -1}, {-1, -1}, {-1, 0}, {-1,  1}  
  20.     };  
  21.     //根據patternSize值,得到具體應用上面定義的哪個數組  
  22.     const int (*offsets)[2] = patternSize == 16 ? offsets16 :  
  23.                               patternSize == 12 ? offsets12 :  
  24.                               patternSize == 8  ? offsets8  : 0;  
  25.   
  26.     CV_Assert(pixel && offsets);  
  27.   
  28.     int k = 0;  
  29.     //代入輸入圖像每行的像素個數,得到圓周像素的絕對座標位置  
  30.     for( ; k < patternSize; k++ )  
  31.         pixel[k] = offsets[k][0] + offsets[k][1] * rowStride;  
  32.     //由於要計算連續的像素,因此要循環的多列出一些值  
  33.     for( ; k < 25; k++ )  
  34.         pixel[k] = pixel[k - patternSize];  
  35. }  
計算得分函數,cornerScore函數是以圓周像素爲16點爲例而編寫的:
  1. template<>  
  2. int cornerScore<16>(const uchar* ptr, const int pixel[], int threshold)  
  3. {  
  4.     const int K = 8, N = K*3 + 1;  
  5.     //v爲當前像素值  
  6.     int k, v = ptr[0];  
  7.     short d[N];  
  8.     //計算當前像素值與其圓周像素值之間的差值  
  9.     for( k = 0; k < N; k++ )  
  10.         d[k] = (short)(v - ptr[pixel[k]]);  
  11.   
  12. #if CV_SSE2  
  13.     __m128i q0 = _mm_set1_epi16(-1000), q1 = _mm_set1_epi16(1000);  
  14.     for( k = 0; k < 16; k += 8 )  
  15.     {  
  16.         __m128i v0 = _mm_loadu_si128((__m128i*)(d+k+1));  
  17.         __m128i v1 = _mm_loadu_si128((__m128i*)(d+k+2));  
  18.         __m128i a = _mm_min_epi16(v0, v1);  
  19.         __m128i b = _mm_max_epi16(v0, v1);  
  20.         v0 = _mm_loadu_si128((__m128i*)(d+k+3));  
  21.         a = _mm_min_epi16(a, v0);  
  22.         b = _mm_max_epi16(b, v0);  
  23.         v0 = _mm_loadu_si128((__m128i*)(d+k+4));  
  24.         a = _mm_min_epi16(a, v0);  
  25.         b = _mm_max_epi16(b, v0);  
  26.         v0 = _mm_loadu_si128((__m128i*)(d+k+5));  
  27.         a = _mm_min_epi16(a, v0);  
  28.         b = _mm_max_epi16(b, v0);  
  29.         v0 = _mm_loadu_si128((__m128i*)(d+k+6));  
  30.         a = _mm_min_epi16(a, v0);  
  31.         b = _mm_max_epi16(b, v0);  
  32.         v0 = _mm_loadu_si128((__m128i*)(d+k+7));  
  33.         a = _mm_min_epi16(a, v0);  
  34.         b = _mm_max_epi16(b, v0);  
  35.         v0 = _mm_loadu_si128((__m128i*)(d+k+8));  
  36.         a = _mm_min_epi16(a, v0);  
  37.         b = _mm_max_epi16(b, v0);  
  38.         v0 = _mm_loadu_si128((__m128i*)(d+k));  
  39.         q0 = _mm_max_epi16(q0, _mm_min_epi16(a, v0));  
  40.         q1 = _mm_min_epi16(q1, _mm_max_epi16(b, v0));  
  41.         v0 = _mm_loadu_si128((__m128i*)(d+k+9));  
  42.         q0 = _mm_max_epi16(q0, _mm_min_epi16(a, v0));  
  43.         q1 = _mm_min_epi16(q1, _mm_max_epi16(b, v0));  
  44.     }  
  45.     q0 = _mm_max_epi16(q0, _mm_sub_epi16(_mm_setzero_si128(), q1));  
  46.     q0 = _mm_max_epi16(q0, _mm_unpackhi_epi64(q0, q0));  
  47.     q0 = _mm_max_epi16(q0, _mm_srli_si128(q0, 4));  
  48.     q0 = _mm_max_epi16(q0, _mm_srli_si128(q0, 2));  
  49.     threshold = (short)_mm_cvtsi128_si32(q0) - 1;  
  50. #else  
  51.     //a0爲閾值  
  52.     int a0 = threshold;  
  53.     //滿足角點條件1時,更新閾值  
  54.     for( k = 0; k < 16; k += 2 )  
  55.     {  
  56.         //a爲d[k+1],d[k+2]和d[k+3]中的最小值  
  57.         int a = std::min((int)d[k+1], (int)d[k+2]);  
  58.         a = std::min(a, (int)d[k+3]);  
  59.         //如果a小於閾值,則進行下一次循環  
  60.         if( a <= a0 )  
  61.             continue;  
  62.         //更新閾值  
  63.         //a爲從d[k+1]到d[k+8]中的最小值  
  64.         a = std::min(a, (int)d[k+4]);  
  65.         a = std::min(a, (int)d[k+5]);  
  66.         a = std::min(a, (int)d[k+6]);  
  67.         a = std::min(a, (int)d[k+7]);  
  68.         a = std::min(a, (int)d[k+8]);  
  69.         //從d[k]到d[k+9]中的最小值與a0比較,哪個大,哪個作爲新的閾值  
  70.         a0 = std::max(a0, std::min(a, (int)d[k]));  
  71.         a0 = std::max(a0, std::min(a, (int)d[k+9]));  
  72.     }  
  73.     //滿足角點條件2時,更新閾值  
  74.     int b0 = -a0;  
  75.     for( k = 0; k < 16; k += 2 )  
  76.     {  
  77.         int b = std::max((int)d[k+1], (int)d[k+2]);  
  78.         b = std::max(b, (int)d[k+3]);  
  79.         b = std::max(b, (int)d[k+4]);  
  80.         b = std::max(b, (int)d[k+5]);  
  81.         if( b >= b0 )  
  82.             continue;  
  83.         b = std::max(b, (int)d[k+6]);  
  84.         b = std::max(b, (int)d[k+7]);  
  85.         b = std::max(b, (int)d[k+8]);  
  86.   
  87.         b0 = std::min(b0, std::max(b, (int)d[k]));  
  88.         b0 = std::min(b0, std::max(b, (int)d[k+9]));  
  89.     }  
  90.   
  91.     threshold = -b0-1;  
  92. #endif  
  93.   
  94. #if VERIFY_CORNERS  
  95.     testCorner(ptr, pixel, K, N, threshold);  
  96. #endif  
  97.     //更新後的閾值作爲輸出  
  98.     return threshold;  
  99. }  

可以有兩種方法實現FAST角點檢測,即直接調用FAST函數,和使用特徵點檢測類的方式。這兩種方法我們都給出實例。

首先是直接調用FAST函數的應用程序:

  1. #include "opencv2/core/core.hpp"  
  2. #include "opencv2/highgui/highgui.hpp"  
  3. #include "opencv2/imgproc/imgproc.hpp"  
  4. #include "opencv2/features2d/features2d.hpp"    //需要添加該頭文件  
  5. #include <iostream>  
  6. using namespace cv;  
  7. using namespace std;  
  8.   
  9. int main( int argc, char** argv )  
  10. {  
  11.     Mat src, gray;  
  12.     src=imread("building.jpg");  
  13.     if( !src.data )    
  14.         return -1;    
  15.     //彩色圖像轉換爲灰度圖像  
  16.     cvtColor( src, gray, CV_BGR2GRAY );  
  17.     //定義特徵點KeyPoint向量  
  18.     std::vector<KeyPoint> keyPoints;    
  19.     //調用FAST函數,閾值選爲55  
  20.     FAST(gray, keyPoints, 55);  
  21.   
  22.     int total = keyPoints.size();  
  23.     //在原圖上畫出特徵點  
  24.     for(int i = 0; I < total; i++)  
  25.     {  
  26.             circle( src, Point( (int)keyPoints[i].pt.x, (int)keyPoints[i].pt.y ), 5, Scalar(0,0,255), -1, 8, 0 );  
  27.     }  
  28.   
  29.   namedWindow( "Corners", CV_WINDOW_AUTOSIZE );  
  30.   imshow( "Corners", src );  
  31.    
  32.   waitKey(0);  
  33.   return 0;  
  34. }  

下面是應用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個。下面是具體的應用程序:
  1. #include "opencv2/core/core.hpp"  
  2. #include "opencv2/highgui/highgui.hpp"  
  3. #include "opencv2/imgproc/imgproc.hpp"  
  4. #include "opencv2/features2d/features2d.hpp"  
  5. #include <iostream>  
  6. using namespace cv;  
  7. using namespace std;  
  8.   
  9. int main( int argc, char** argv )  
  10. {  
  11.     Mat src, gray,color_edge;  
  12.     src=imread("building.jpg");  
  13.     if( !src.data )    
  14.         return -1;    
  15.   
  16.      std::vector<KeyPoint> keyPoints;    
  17.     //創建對象,閾值設爲55  
  18.     FastFeatureDetector fast(55);       
  19.     //特徵點檢測  
  20.     fast.detect(src,keyPoints);    
  21.     //在原圖上畫出特徵點  
  22.     drawKeypoints(src, keyPoints, src, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_OVER_OUTIMG);    
  23.     imshow("FAST feature", src);    
  24.     waitKey(0);    
  25.     return 0;  
  26. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章