第六章 - 图像变换 - 霍夫圆变换(cvHoughCircles)

霍夫圆变换与直线变换大体上是类似的,但是累加平面会被三维累加容器代替,(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在灰度图中找到圆序列并返回。

#include <highgui.h>
#include <math.h>
#include <cv.h>

int main(int argc, char** argv)
{
	IplImage* src = cvLoadImage( argv[1], 0 );
	IplImage* dst = cvLoadImage( argv[1], 0 );
	CvMemStorage* storage = cvCreateMemStorage(0);
	cvSmooth( src, dst, CV_GAUSSIAN, 5, 5 );  //降噪
	CvSeq* results = cvHoughCircles(  //cvHoughCircles函数需要估计每一个像素梯度的方向,
								      //因此会在内部自动调用cvSobel,而二值边缘图像的处理是比较难的
		dst,
		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(
			dst,
			pt,  //确定圆心
			cvRound( p[2] ),  //确定半径
			CV_RGB( 0xff, 0, 0 )
		);  //画圆函数
	}
	cvNamedWindow( "cvHoughCircles", 1 );
	cvShowImage( "cvHoughCircles", dst );
	cvWaitKey(0);

	return 0;
}
-------------------------------------------------------------------------------------------------

/*result*/

Hough Circle第六章 - 图像变换 - 霍夫圆变换(cvHoughCircles)_第1张图片

你可能感兴趣的:(image,search,存储,float,DST,distance)