第六章 - 圖像變換 - 霍夫圓變換(cvHoughCircles)

該文章轉載於:http://blog.csdn.net/hitwengqi/article/details/6883299

霍夫圓變換與直線變換大體上是類似的,但是累加平面會被三維累加容器代替,(x,y,r),x,y確定圓心,r確定半徑,但這意味着需要大量內存速度較慢,OpenCV通過一個比較靈活的霍夫梯度法來解決圓變換問題,利用到cvSobel。累加器概念不是很明白??

-------------------------------------------------------------------------------------------------

霍夫圓變換的函數爲:

HoughCircles

利用 Hough 變換在灰度圖像中找圓

CvSeq* cvHoughCircles( CvArr* image, void* circle_storage, int method, double dp, double min_dist, double param1=100, double param2=100, int min_radius=0, int max_radius=0 );
image
輸入 8-比特、單通道灰度圖像.
circle_storage
檢測到的圓存儲倉. 可以是內存存儲倉 (此種情況下,一個線段序列在存儲倉中被創建,並且由函數返回)或者是包含圓參數的特殊類型的具有單行/單列的CV_32FC3型矩陣(CvMat*). 矩陣頭爲函數所修改,使得它的 cols/rows 將包含一組檢測到的圓。如果 circle_storage 是矩陣,而實際圓的數目超過矩陣尺寸,那麼最大可能數目的圓被返回

. 每個圓由三個浮點數表示:圓心座標(x,y)和半徑.

method
Hough 變換方式,目前只支持CV_HOUGH_GRADIENT, which is basically 21HT, described in [Yuen03].
dp
累加器圖像的分辨率。這個參數允許創建一個比輸入圖像分辨率低的累加器。(這樣做是因爲有理由認爲圖像中存在的圓會自然降低到與圖像寬高相同數量的範疇)。如果dp設置爲1,則分辨率是相同的;如果設置爲更大的值(比如2),累加器的分辨率受此影響會變小(此情況下爲一半)。dp的值不能比1小。

Resolution of the accumulator used to detect centers of the circles. For example, if it is 1, the accumulator will have the same resolution as the input image, if it is 2 - accumulator will have twice smaller width and height, etc.

min_dist
該參數是讓算法能明顯區分的兩個不同圓之間的最小距離。

Minimum distance between centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.

param1
用於Canny的邊緣閥值上限,下限被置爲上限的一半。

The first method-specific parameter. In case of CV_HOUGH_GRADIENT it is the higher threshold of the two passed to Canny edge detector (the lower one will be twice smaller).

param2
累加器的閥值。

The second method-specific parameter. In case of CV_HOUGH_GRADIENT it is accumulator threshold at the center detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first.

min_radius
最小圓半徑。

Minimal radius of the circles to search for.

max_radius
最大圓半徑。

Maximal radius of the circles to search for. By default the maximal radius is set to max(image_width, image_height).

The function cvHoughCircles finds circles in grayscale image using some modification of Hough transform.

-------------------------------------------------------------------------------------------------

/*code*/

利用cvHoughCircles在灰度圖中找到圓序列並返回。

  1. #include <highgui.h>  
  2. #include <math.h>  
  3. #include <cv.h>  
  4.   
  5. int main(int argc, char** argv)  
  6. {  
  7.     IplImage* src = cvLoadImage( argv[1], 0 );  
  8.     IplImage* dst = cvLoadImage( argv[1], 0 );  
  9.     CvMemStorage* storage = cvCreateMemStorage(0);  
  10.     cvSmooth( src, dst, CV_GAUSSIAN, 5, 5 );  //降噪  
  11.     CvSeq* results = cvHoughCircles(  //cvHoughCircles函數需要估計每一個像素梯度的方向,  
  12.                                       //因此會在內部自動調用cvSobel,而二值邊緣圖像的處理是比較難的  
  13.         dst,  
  14.         storage,  
  15.         CV_HOUGH_GRADIENT,  
  16.         2,  //累加器圖像的分辨率  
  17.         image->width/10  
  18.         );  
  19.     forint i = 0; i < results->total; i++ )  
  20.     {  
  21.         float* p = ( float* )cvGetSeqElem( results, i );  
  22.         //霍夫圓變換  
  23.         CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );  
  24.         cvCircle(  
  25.             dst,  
  26.             pt,  //確定圓心  
  27.             cvRound( p[2] ),  //確定半徑  
  28.             CV_RGB( 0xff, 0, 0 )  
  29.         );  //畫圓函數  
  30.     }  
  31.     cvNamedWindow( "cvHoughCircles", 1 );  
  32.     cvShowImage( "cvHoughCircles", dst );  
  33.     cvWaitKey(0);  
  34.   
  35.     return 0;  
  36. }  
-------------------------------------------------------------------------------------------------

/*result*/

Hough Circle
發佈了2 篇原創文章 · 獲贊 11 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章