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

标签 python opencv geometry detection

我正在尝试使用 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()

最佳答案

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

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

param1 – First method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny() edge detector (the lower one is twice smaller).

param2 – Second method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first.

因此,如您所见,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

关于python - HoughCircles圆检测使用opencv和python-,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26254287/

相关文章:

python 3类型模块

opencv_traincascade 找不到正样本

c++ - 如何避免在 C++ 项目(XCode)中的其他机器上加载动态库?

c++ - opencv SVM 不适用于缩放功能

python - 兼容 Keras 和 Tensorflow 版本

python - 为什么 python 在 Ubuntu 和 MacOS 上解析日期的方式不同?

python - 跨多个地理围栏的地理围栏半径分布/比率

c++ - 错误,实现绕数算法,(OpenGL,C++)

python - 球体表面点的均匀分布?

python - 如何从 Python 中的数字获取语言环境格式的工作日名称?