使用opencv和python-进行HoughCircles圆检测 [英] HoughCircles circle detection using opencv and python-

查看:787
本文介绍了使用opencv和python-进行HoughCircles圆检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OpenCV的(霍夫)圆检测来..检测圆.我在黑色背景上创建了一个实心圆,尝试使用参数,使用了模糊和所有内容,但是我无法使其找到任何东西.

I am trying to use OpenCV's (Hough)Circle detection to.. detect circles. I created a solid circle on a black background, tried to play with the parameters, used blur and everything, but I am just not able to make it find anything.

任何想法,建议等都很好,谢谢!

Any ideas, suggestions etc. would be great, thank you!

我当前的代码如下:

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阈值参数上.

Your code is working just fine. The problem is in your HoughCircles threshold parameters.

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

param1 –第一个方法特定的参数.如果是CV_HOUGH_GRADIENT ,这是传递给Canny()边缘的两个阈值中的较高阈值 检测器(下部的检测器小两倍).

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 –特定于方法的第二个参数.的情况下 CV_HOUGH_GRADIENT,它是圆的累加器阈值 集中在检测阶段.越小,越假 可能检测到圆圈.圆圈,对应较大 累加器值,将首先返回.

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边缘检测器,这意味着您可以在函数中使用灰度图像,而不是轮廓.

So, as you can see, internally the HoughCircles function calls the Canny edge detector, this means that you can use a gray image in the function, instead of their contours.

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

Now reduce the param1 to 30 and param2 to 15 and see the results in the code that follows:

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()

这篇关于使用opencv和python-进行HoughCircles圆检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆