1. 程式人生 > >OpenCV學習筆記-霍夫圓變換

OpenCV學習筆記-霍夫圓變換

霍夫圓變換的函式為:

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 "stdafx.h"
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <math.h>
int _tmain(int argc, _TCHAR* argv[])
{
	IplImage* image0=cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE);
	IplImage* image= cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE);
	//IplImage* image=NULL;//
	//image=cvCreateImage(cvGetSize(image0),IPL_DEPTH_8U,3); 
	CvMemStorage* storage=cvCreateMemStorage(0);
	cvSmooth(image0,image,CV_GAUSSIAN,5,5);
	CvSeq* results=cvHoughCircles(image,storage,CV_HOUGH_GRADIENT,2,image->width /10);
	for(int i=0;i<results->total ;i++)
	{
		float* p=(float*) cvGetSeqElem(results,i);
		CvPoint pt=cvPoint(cvRound(p[0]),cvRound(p[1]));
		cvCircle(image,pt,cvRound(p[2]),CV_RGB(0xff,0xff,0xff));
	}
	cvNamedWindow("source",0);
	cvShowImage("source",image0);
	cvNamedWindow("cvHoughCircles",0);
	cvShowImage("cvHoughCircles",image);
	cvWaitKey(0);
	return 0;
}

運算結果不是太理想:

參考資料:‘

1.學習OpenCV,於仕祺,劉瑞禎,清華大學出版社,pp.179-183

PS:修改一下:

#include "stdafx.h"  
#include "stdafx.h"  
#include "cv.h"  
#include "highgui.h"  
#include <math.h>  
int _tmain(int argc, _TCHAR* argv[])  
{  
    IplImage* image0=cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE);  
    //IplImage* image= cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE);  
	IplImage *image1=cvLoadImage("circle.bmp",1);
    //IplImage* image=NULL;//  
    //image=cvCreateImage(cvGetSize(image0),IPL_DEPTH_8U,3);   
    CvMemStorage* storage=cvCreateMemStorage(0);  
    //cvSmooth(image0,image,CV_GAUSSIAN,5,5);  
    CvSeq* results=cvHoughCircles(image0,storage,CV_HOUGH_GRADIENT,2,image0->width /10);  
    for(int i=0;i<results->total ;i++)  
    {  
        float* p=(float*) cvGetSeqElem(results,i);  
        CvPoint pt=cvPoint(cvRound(p[0]),cvRound(p[1]));  
        cvCircle(image1,pt,cvRound(p[2]),CV_RGB(255,0,0));  
    }  
    cvNamedWindow("source",0);  
    cvShowImage("source",image0);  
    cvNamedWindow("cvHoughCircles",0);  
    cvShowImage("cvHoughCircles",image1);  
    cvWaitKey(0);  
    return 0;  
}  


結果:


看起來清楚些,可是還是檢測的不好。