使用opencv和python进行HoughCircles圆检测-

Posted

技术标签:

【中文标题】使用opencv和python进行HoughCircles圆检测-【英文标题】:HoughCircles circle detection using opencv and python- 【发布时间】:2014-12-02 23:01:12 【问题描述】:

我正在尝试使用 OpenCV 的 (Hough)Circle 检测来检测圆圈。我在黑色背景上创建了一个实心圆圈,尝试使用参数,使用模糊和一切,但我无法让它找到任何东西。

任何想法、建议等都会很棒,谢谢!

我当前的代码是这样的:

import cv2
import numpy as np

"""
params = dict(dp=1,
              minDist=1,
              circles=None,
              param1=300,
              param2=290,
              minRadius=1,
              maxRadius=100)
"""

img = np.ones((200,250,3), dtype=np.uint8)
for i in range(50, 80, 1):
    for j in range(40, 70, 1):
        img[i][j]*=200

cv2.circle(img, (120,120), 20, (100,200,80), -1)


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray, 200, 300)

cv2.imshow('shjkgdh', canny)
gray = cv2.medianBlur(gray, 5)
circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 20,
              param1=100,
              param2=30,
              minRadius=0,
              maxRadius=0)

print circles
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
    cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('circles', img)
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

【问题讨论】:

好的,由于某种原因,它突然可以使用很多参数。从昨天开始,我经历了所有事情,没有任何工作,但现在可以了。这对我来说没有多大意义,但是... 霍夫圆在实践中有点……棘手。 【参考方案1】:

您的代码运行良好。问题在于您的HoughCircles 阈值参数。

让我们尝试从OpenCV Docs 了解您正在使用的参数:

param1 – 第一个特定于方法的参数。在 CV_HOUGH_GRADIENT 的情况下 ,它是传递给 Canny() 边缘的两者中较高的阈值 检测器(下面的小一倍)。

param2 – 第二个特定于方法的参数。的情况下 CV_HOUGH_GRADIENT , 圆的累加器阈值 集中在检测阶段。越小越假 可能会检测到圆圈。圆圈,对应较大的 累加器值,将首先返回。

因此,如您所见,HoughCircles 函数内部调用 Canny 边缘检测器,这意味着您可以在函数中使用灰度图像,而不是它们的轮廓。

现在将 param1 减少到 30,将 param2 减少到 15,然后在下面的代码中查看结果:

import cv2
import numpy as np

img = np.ones((200,250,3), dtype=np.uint8)
for i in range(50, 80, 1):
    for j in range(40, 70, 1):
        img[i][j]*=200

cv2.circle(img, (120,120), 20, (100,200,80), -1)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 20,
              param1=30,
              param2=15,
              minRadius=0,
              maxRadius=0)

print circles
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
    cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('circles', img)

k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

【讨论】:

谢谢 :) 我展示了精明的结果,以便更好地了解 HoughCircles 实际使用的内容。精明的结果总是看起来很干净,这可能是我最大的困惑。不知何故,我设法总是使用不适合的参数,即使我已经尝试了很多不同的参数。由于 canny 图像似乎与工作参数和不工作参数相同,我仍然不确定为什么有些参数有效而其他参数无效。 如何使用 maxRadius=0?这肯定会限制任何圈子的出现吗? @ComputerScientist 如果您阅读HoughCircle 的文档,您可以看到maxRadiusminRadius 默认值为0。因此,您没有对半径大小应用任何阈值。因此,获取图像中的所有圆圈。【参考方案2】:

如果您没有让 HoughCircles 为您提供针对明显圆圈的像素完美解决方案,那么您就没有正确使用它

您的错误是您尝试自己手动调整超参数。那是行不通的。让计算机为您自动调整参数:

import numpy as np
import argparse
import cv2
import signal

from functools import wraps
import errno
import os
import copy

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
orig_image = np.copy(image)
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow("gray", gray)
cv2.waitKey(0)

circles = None

minimum_circle_size = 100      #this is the range of possible circle in pixels you want to find
maximum_circle_size = 150     #maximum possible circle size you're willing to find in pixels

guess_dp = 1.0

number_of_circles_expected = 1          #we expect to find just one circle
breakout = False

max_guess_accumulator_array_threshold = 100     #minimum of 1, no maximum, (max 300?) the quantity of votes 
                                                #needed to qualify for a circle to be found.
circleLog = []

guess_accumulator_array_threshold = max_guess_accumulator_array_threshold

while guess_accumulator_array_threshold > 1 and breakout == False:
    #start out with smallest resolution possible, to find the most precise circle, then creep bigger if none found
    guess_dp = 1.0
    print("resetting guess_dp:" + str(guess_dp))
    while guess_dp < 9 and breakout == False:
        guess_radius = maximum_circle_size
        print("setting guess_radius: " + str(guess_radius))
        print(circles is None)
        while True:

            #HoughCircles algorithm isn't strong enough to stand on its own if you don't
            #know EXACTLY what radius the circle in the image is, (accurate to within 3 pixels) 
            #If you don't know radius, you need lots of guess and check and lots of post-processing 
            #verification.  Luckily HoughCircles is pretty quick so we can brute force.

            print("guessing radius: " + str(guess_radius) + 
                    " and dp: " + str(guess_dp) + " vote threshold: " + 
                    str(guess_accumulator_array_threshold))

            circles = cv2.HoughCircles(gray, 
                cv2.cv.CV_HOUGH_GRADIENT, 
                dp=guess_dp,               #resolution of accumulator array.
                minDist=100,                #number of pixels center of circles should be from each other, hardcode
                param1=50,
                param2=guess_accumulator_array_threshold,
                minRadius=(guess_radius-3),    #HoughCircles will look for circles at minimum this size
                maxRadius=(guess_radius+3)     #HoughCircles will look for circles at maximum this size
                )

            if circles is not None:
                if len(circles[0]) == number_of_circles_expected:
                    print("len of circles: " + str(len(circles)))
                    circleLog.append(copy.copy(circles))
                    print("k1")
                break
                circles = None
            guess_radius -= 5 
            if guess_radius < 40:
                break;

        guess_dp += 1.5

    guess_accumulator_array_threshold -= 2

#Return the circleLog with the highest accumulator threshold

# ensure at least some circles were found
for cir in circleLog:
    # convert the (x, y) coordinates and radius of the circles to integers
    output = np.copy(orig_image)

    if (len(cir) > 1):
        print("FAIL before")
        exit()

    print(cir[0, :])

    cir = np.round(cir[0, :]).astype("int")

    for (x, y, r) in cir:
        cv2.circle(output, (x, y), r, (0, 0, 255), 2)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

    cv2.imshow("output", np.hstack([orig_image, output]))
    cv2.waitKey(0)

上面的代码转换了这个:

至此:

有关此功能的更多信息,请参阅:https://***.com/a/46500223/445131

【讨论】:

很酷的代码,运行良好!只是一些cmets。似乎根本没有使用 minimum_circle_size 。我会将“如果guess_radius 那段代码写的很匆忙,所以有bug。该代码是一种布朗运动尝试“找出我想要的圆的半径”,因为 houghcircles 返回很多。因此,知道您想要的圆的确切半径,houghcircles 会产生完美的结果。 Houghcircles 内部投票系统的问题是它会在后台的缺陷中发现无效的圈子。【参考方案3】:

我已经为这个算法编写了一个 GUI,您可以按时更改参数以查看它是如何工作的。在这里你可以从 Github 看到它:

Github 链接:https://github.com/pwwiur/hough-counter

安装:

将其克隆到您的目录:

git clone https://github.com/pwwiur/hough-counter.git

安装要求:

pip install numpy opencv-python matplotlib pillow

运行:

python main.py

演示

【讨论】:

以上是关于使用opencv和python进行HoughCircles圆检测-的主要内容,如果未能解决你的问题,请参考以下文章

使用Python,OpenCV进行平滑和模糊

使用 OpenCV 和 Python-2.7 进行屏幕截图

使用opencv和python进行HoughCircles圆检测-

在 Raspberry PI 上使用 Python 和 OpenCV 进行图像处理 [关闭]

使用Python+OpenCV进行图像模板匹配(Match Template)

使用Python,OpenCV和Haar级联进行人脸检测