我遇到了你的确切问题并找到了解决方案
关键是对 HoughCircles 正在做的事情有足够的直觉,这样您就可以构建一个程序,为您想要在其中查找圆圈的所有各种图像自动调整超参数。
核心问题,一些直觉
HoughCircles 并不是独立存在的,尽管它建议它可以使用最小和最大半径参数,但您需要运行数百或数千次迭代才能在正确的设置中自动调整和自动拨号。完成后,您需要进行后处理验证步骤,以 100% 确定该圆是您想要的。问题是您试图通过猜测和检查来手动调整 HoughCircles 的输入参数。那根本行不通。让计算机为您自动调整这些参数。
HoughCircles 的手动调整什么时候才能令人满意?
如果您想手动对参数进行硬编码,那么您绝对需要的一件事就是圆的精确半径在一两个像素之内。您可以猜测 dp 分辨率并设置累加器数组投票阈值,您可能会没事。但如果您不知道半径,HoughCircles 输出就没用,因为它要么到处都能找到圆,要么根本找不到圆。假设您确实手动找到了可接受的调整,您向它显示了一个有几个像素不同的图像,您的 HoughCircles 会惊慌失措,在图像中发现了 200 个圆圈。一文不值。
还有希望:
希望来自这样一个事实:即使在处理大图像时,HoughCircles 也非常快。您可以为 HoughCircles 编写一个程序来完美地自动调整设置。如果您不知道半径并且它可能很小或很大,那么您可以从较大的“最小距离参数”、非常精细的 dp 分辨率和非常高的投票阈值开始。因此,当您开始迭代时,HoughCircles 可以预见地拒绝找到任何圈子,因为设置过于激进并且投票没有清除阈值。但循环会不断迭代并逐渐达到最佳设置,让最佳设置成为指示您已完成的避雷针。您找到的第一个圆将是图像中像素完美的最大且最佳的圆,您会对 HoughCircles 为您提供的像素完美的圆印象深刻,就在它应该在的位置。只是你必须运行它 5000 次。
python 代码示例(抱歉,它不是 C++):
它的边缘仍然很粗糙,但你应该能够清理它,这样你就可以在不到一秒钟的时间内获得令人满意的像素完美结果。
import numpy as np
import argparse
import cv2
import signal
from functools import wraps
import errno
import os
import copy
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())
# load the image, clone it for output, and then convert it to grayscale
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
#hand tune this
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.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")
# loop over the (x, y) coordinates and radius of the circles
if (len(cir) > 1):
print("FAIL after")
exit()
for (x, y, r) in cir:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
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)
# show the output image
cv2.imshow("output", np.hstack([orig_image, output]))
cv2.waitKey(0)
因此,如果你运行它,需要花费 5 秒的时间,但它几乎可以达到像素完美(进一步手动调整自动调谐器可以使其达到亚像素完美):
The above code converts this:
To this:
让这项工作成功的秘诀在于在开始之前你掌握了多少信息。如果您知道半径达到一定的公差(例如 20 像素),那么这非常有效,您就完成了。但如果你不这样做,你就必须聪明地了解如何通过小心接近决议和投票阈值来爬升最大投票半径。如果圆圈的形状很奇怪,则 dp 分辨率需要更高,而投票阈值则需要探索更低的范围。