2014-10-08 124 views
3

我想使用OpenCV(霍夫)圈检测来检测圆。我在黑色背景上创建了一个实心圆,尝试使用参数进行游戏,使用模糊和所有内容,但我无法使其找到任何东西。HoughCircles圆检测使用opencv和python-

任何想法,建议等将是伟大的,谢谢!

我当前的代码是这样的:

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

好吧,出于某种原因,它突然与相当多的参数工作。我从昨天开始经历了一切,没有任何工作,但现在它确实如此。这对我来说没有什么意义,但是... – Shin 2014-10-08 10:27:03

+0

Hough圈有点棘手,在实践中。 – 2014-10-08 12:50:25

回答

8

您的代码工作得很好。问题在于您的HoughCircles阈值参数。

让我们试着去了解你从OpenCV Docs使用的参数:

参数1 - 第一个具体的方法参数。在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

+0

谢谢:)我显示了canny结果,以便更好地了解HoughCircles实际正在使用的内容。 canny结果总是看起来干净和相同的tho可能造成我的混乱的最大部分。不知何故,我设法总是使用不适合的参数,即使我已经尝试了很多不同的参数。 由于canny图像似乎与工作而不是工作参数相同,所以我仍然不确定为什么某些参数可以工作而其他参数不工作。 – Shin 2014-10-08 15:39:25

+0

+1但我不知道输入图像是什么样子。 – karlphillip 2014-11-08 17:47:43

+0

这是如何与maxRadius = 0一起工作的?这当然会限制任何圈子出现? – ComputerScientist 2017-07-17 22:09:13

0

如果你没有得到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) 

上面的代码转换这样: Original

要这样:

HoughCircles

有关的更多信息这是做什么,请参阅:https://stackoverflow.com/a/46500223/445131