如何用圆方程实现圆检测?

时间:2017-05-10 05:43:45

标签: python opencv geometry

我正在尝试通过等式实现circular hough transform r = sqrt((x-h)^2-(y-k)^2)来检测图像中的圆圈。

我应用了像高斯模糊,canny这样的步骤列表。在此之后,如果半径和边界点可用,我将无法实现上述方程式。实现后,我将获得包含半径和检测到的圆心的累加器空间。我想用opencv的HoughCircle函数实现。有什么想法可以帮助我吗?它需要花费很多时间。

import numpy as np
import cv2
import math

image = cv2.imread(imagepath)

h, w = image.shape[:2]
print h, w

grayimg = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

bimg = cv2.bilateralFilter(grayimg, 5, 175, 175)

cann = cv2.Canny(bimg,100,200)

pixel = np.argwhere(cann == 255)


accum = []
ct = 0
for r in range(10,21):
    for h in range(0,20):
        for k in range(0,20):
            for p in pixel:
                print r,h,k,p
                xpart = (h - p[0])**2
                ypart = (k - p[1])**2
                rhs = xpart + ypart
                lhs = r * r
                if(lhs == rhs):
                    accum.append((r,(h,k),p))

print len(accum)
cv2.waitKey(0)

1 个答案:

答案 0 :(得分:0)

此代码中仍需要一些升级,以便为累加器空间快速处理。

accum = [[[0 for r in range(10,21)]for h in range(0,30)]for k in range(0,30)]
print accum
ct = 0
for r in range(10,21):
    for h in range(0,30):
        for k in range(0,30):
            for p in pixel:
                #print r,h,k,p
                xpart = (h - p[0])**2
                ypart = (k - p[1])**2
                rhs = xpart + ypart
                lhs = r * r
                if(lhs == rhs):
                    accum[k][h][r-10] += 1