霍夫圓變換的函數爲:
算法
利用 Hough 變換在灰度圖像中找圓ide
CvSeq* cvHoughCircles( CvArr* p_w_picpath, void* circle_storage, int method, double dp, double min_dist, double param1=100, double param2=100, int min_radius=0, int max_radius=0 );
. 每一個圓由三個浮點數表示:圓心座標(x,y)和半徑.函數
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 p_w_picpath, if it is 2 - accumulator will have twice smaller width and height, etc.測試
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.spa
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).3d
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.code
Minimal radius of the circles to search for.orm
Maximal radius of the circles to search for. By default the maximal radius is set to max(p_w_picpath_width, p_w_picpath_height).blog
The function cvHoughCircles finds circles in grayscale p_w_picpath using some modification of Hough transform.圖片
-------------------------------------------------------------------------------------------------
個人目標是在一直全是圓的圖中找出全部圓,並用紅色表圈出。
//霍夫變換尋找圓
img、img一、img2函數外定義。
void CdialogCVDlg::OnBnClickedBtnhoughcircles()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
cvNamedWindow("a",1);
cvNamedWindow("b",1);
cvNamedWindow("c",1);
img = cvLoadImage("D:\ii.jpg",1); //原圖
img1 = cvCreateImage (cvGetSize(img), IPL_DEPTH_8U, 1);//處理的圖像必須是八位單通道的
if (img->nChannels == 1)
{
img1 = cvCloneImage (img);
}
else
{
cvCvtColor (img, img1, CV_RGB2GRAY); //轉爲單通道
}
CvMemStorage* storage=cvCreateMemStorage(0);
CvSeq* circle = 0;
//cvSmooth( img1, img1, CV_GAUSSIAN, 5, 5 ); //看了不少事例,要降噪處理,但測試的結果是
//找圓會有必定誤差,說明用這個要因圖而異,噪聲高的圖纔要用
circle = cvHoughCircles( //cvHoughCircles函數須要估計每個像素梯度的方向,
//所以會在內部自動調用cvSobel,而二值邊緣圖像的處理是比較難的
img1,
storage,
CV_HOUGH_GRADIENT,
1, //累加器圖像的分辨率,增大則分辨率變小
18, //很重要的一個參數,告訴兩個圓之間的距離的最小距離,若是已知一副圖像,能夠先行計
//算出符合本身須要的兩個圓之間的最小距離。
100, //canny算法的閾值上限,下限爲一半(即100以上爲邊緣點,50如下拋棄,中間視是否相連而
//定)
25, //決定成圓的多寡 ,一個圓上的像素超過這個閾值,則成圓,不然丟棄
32,//最小圓半徑,這個能夠經過圖片肯定你須要的圓的區間範圍
45 //最大圓半徑
);
img2 = cvCreateImage (cvGetSize(img), IPL_DEPTH_8U, 3); //用一個三通道的圖片來顯示紅色的
//圓圈
cvCvtColor (img1, img2, CV_GRAY2RGB); //轉爲3通道
for( int i = 0; i < circle->total; i++ )
{
float* p = ( float* )cvGetSeqElem( circle, i );
//霍夫圓變換
CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) ); //圓心座標(p(0),p(1))
cvCircle(
img2,
pt, //肯定圓心
cvRound( p[2] ), //肯定半徑
CV_RGB( 255, 0, 0 ),
3
); //畫圓函數
}
cvShowImage( "a", img );
cvShowImage("b",img1);
cvShowImage("c",img2);
}
//原圖:
//效果圖
結論:只要調好參數,霍夫圓算法能夠找出你的設定目標圓。