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.
Example. Detecting circles with Hough transform.
#include#include #include int main(int argc, char** argv) { IplImage* img; if( argc == 2 && (img=cvLoadImage(argv[1], 1))!= 0) { IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 ); CvMemStorage* storage = cvCreateMemStorage(0); cvCvtColor( img, gray, CV_BGR2GRAY ); cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100 ); int i; for( i = 0; i < circles->total; i++ ) { float* p = (float*)cvGetSeqElem( circles, i ); cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } cvNamedWindow( "circles", 1 ); cvShowImage( "circles", img ); } return 0; }