OpenCV霍夫圆检测原理

Here I’ll tell you how to detect circles (which are quite important in computer vision application) using a technique similar to the standard Hough transform. This article assumes you know how the Hough transform works, or you’ve understood the previous articles in this series (The Hough Transform).
The parameterization
A circle can be described completely with three pieces of information: the center (a, b) and the radius. (The center consists of two parts, hence a total of three)
x = a + Rcosθ
y = b + Rsinθ
When the θ varies from 0 to 360, a complete circle of radius R is generated.
So with the Circle Hough Transform, we expect to find triplets of (x, y, R) that are highly probably circles in the image. That is, we want to find three parameters. Thus, the parameter space is 3D… meaning things can get ugly if you don’t tread slowly. Out of memory errors are common even if your programming language uses virtual memory.
So we’ll start simple.
Assuming R is known
To begin, we’ll start with the assumption that you’re looking for circles of a particular radius, that is, R is known. The equation of each circle is:
x = a + Rcosθ
y = b + Rsinθ
So, every point in the xy space will be equivalent to a circle in the ab space (R isn’t a parameter, we already know it). This is because on rearranging the equations, we get:
a = x1 – Rcosθ
b = y1 – Rsinθ
for a particular point (x1, y1). And θ sweeps from 0 to 360 degrees.
So, the flow of events is something like this:
Load an image
Detect edges and generate a binary image
For every ‘edge’ pixel, generate a circle in the ab space
For every point on the circle in the ab space, cast ‘votes’ in the accumulator cells
The cells with greater number of votes are the centers
When R is not known
When the radius is not known, the simplest solution is to just guess. Assume R = 1, and then run the same algorithm. Then assume R = 2, and run it again. Assume R = 3…. and so on.
Whats the upper limit of R? A safe limit would be the length of the diagonal of the image. No possible circle on the image can have a radius greater than or equal to the diagonal.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import cv2 as cv import numpy as np def hough_circle(image): #因为霍夫检测对噪声很明显,所以需要先滤波一下。 dst =cv.pyrMeanShiftFiltering(image,10,100) cimage=cv.cvtColor(dst,cv.COLOR_BGR2GRAY) circles = cv.HoughCircles(cimage,cv.HOUGH_GRADIENT,1,40,param1=40,param2=29,minRadius=30,maxRadius=0) #把circles包含的圆心和半径的值变为整数 circles = np.uint16(np.around(circles)) for i in circles[0]: cv.circle(image,(i[0],i[1]),i[2],(0,255,0),3) cv.imshow("circle",image) src = cv.imread("E:/opencv/picture/coins.jpg") cv.imshow("inital_window",src) hough_circle(src) cv.waitKey(0) cv.destroyAllWindows() 霍夫圆变换的基本思路是认为图像上每一个非零像素点都有可能是一个潜在的圆上的一点, 跟霍夫线变换一样,也是通过投票,生成累积坐标平面,设置一个累积权重来定位圆。 在笛卡尔坐标系中圆的方程为: 其中(a,b)是圆心,r是半径,也可以表述为: 即 在笛卡尔的xy坐标系中经过某一点的所有圆映射到abr坐标系中就是一条三维的曲线: 经过xy坐标系中所有的非零像素点的所有圆就构成了abr坐标系中很多条三维的曲线。 在xy坐标系中同一个圆上的所有点的圆方程是一样的,它们映射到abr坐标系中的是同一个点,所以在abr坐标系中该点就应该有圆的总像素N0个曲线相交。 通过判断abr中每一点的相交(累积)数量,大于一定阈值的点就认为是圆。 以上是标准霍夫圆变换实现算法。 问题是它的累加到一个三维的空间,意味着比霍夫线变换需要更多的计算消耗。 Opencv霍夫圆变换对标准霍夫圆变换做了运算上的优化。 它采用的是“霍夫梯度法”。它的检测思路是去遍历累加所有非零点对应的圆心,对圆心进行考量。 如何定位圆心呢?圆心一定是在圆上的每个点的模向量上,即在垂直于该点并且经过该点的切线的垂直线上,这些圆上的模向量的交点就是圆心。 霍夫梯度法就是要去查找这些圆心,根据该“圆心”上模向量相交数量的多少,根据阈值进行最终的判断。 bilibili: 注意: 1.OpenCV霍夫圆变换函数原型为:HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) -> circles image参数表示8位单通道灰度输入图像矩阵。 method参数表示圆检测方法,目前唯一实现的方法是HOUGH_GRADIENT。 dp参数表示累加器与原始图像相比的分辨率的反比参数。例如,如果dp = 1,则累加器具有与输入图像相同的分辨率。如果dp=2,累加器分辨率是元素图像的一半,宽度和高度也缩减为原来的一半。 minDist参数表示检测到的两个圆心之间的最小距离。如果参数太小,除了真实的一个圆圈之外,可能错误地检测到多个相邻的圆圈。如果太大,可能会遗漏一些圆圈。 circles参数表示检测到的圆的输出向量,向量内第一个元素是圆的横坐标,第二个是纵坐标,第三个是半径大小。 param1参数表示Canny边缘检测的高阈值,低阈值会被自动置为高阈值的一半。 param2参数表示圆心检测的累加阈值,参数值越小,可以检测越多的假圆圈,但返回的是与较大累加器值对应的圆圈。 minRadius参数表示检测到的圆的最小半径。 maxRadius参数表示检测到的圆的最大半径。 2.OpenCV画圆的circle函数原型:circle(img, center, radius, color[, thickness[, lineType[, shift]]]) -> img img参数表示源图像。 center参数表示圆心坐标。 radius参数表示圆的半径。 color参数表示设定圆的颜色。 thickness参数:如果是正数,表示圆轮廓的粗细程度。如果是负数,表示要绘制实心圆。 lineType参数表示圆线条的类型。 shift参数表示圆心坐标和半径值中的小数位数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值