霍夫圆变换无法在这张图片上检测到圆形。

6

我正在尝试检测包含点圆的图像中的圆形,但是很遗憾我无法做到。我正在使用opencv的HoughTransform算法,但我找不到能使其正常工作的参数。

src = imread("encoded.jpg",1);
    /// Convert it to gray
    cvtColor(src, src_gray, CV_BGR2GRAY);

    vector<Vec3f> circles;

    /// Apply the Hough Transform to find the circles
    HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, 10,
        100, 30, 1, 30 // change the last two parameters
        // (min_radius & max_radius) to detect larger circles
        );

    /// Draw the circles detected
    for (size_t i = 0; i < circles.size(); i++)
    {
        cout << "Positive" << endl;
        Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
        int radius = cvRound(circles[i][2]);
        // circle center
        circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
        // circle outline
        circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);
    }

    /// Show your results
    namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
    imshow("Hough Circle Transform Demo", src_gray);
    waitKey(0);

这是我的图片: 输入图片

为什么HoughCircles不能在这张图片中检测到圆?它似乎可以在其他简单的图片上工作,比如电路板的图片。


您可能需要:1/增加迭代次数,2/预处理点以将它们减少为单个点。 - FiReTiTi
@MohitJain,您能否提供代码……可以解决这个问题,我是图像处理的初学者……我只想在这张图片中检测圆形形状。 - Utkarsh Dixit
看起来它们太小了。如果你减小HoughCircle的参数“param2”,你可以检测到绿色的圆形,但不能检测到(所有?)黑色的圆形。如果你减小“furtcher”,会得到许多错误检测。如果你调整图像大小(每个维度双倍),则更容易找到参数,但仍然难以检测出所有的黑色圆形。 - Micka
1
@Micka 谢谢,我终于做到了...现在所有的圆都被检测出来了。我增加了圆的大小,甚至调整了图像的大小。这就是诀窍...谢谢。 - Utkarsh Dixit
1
@Micka 哦,谢谢,我刚刚做完了...... - Utkarsh Dixit
显示剩余8条评论
1个回答

7

我曾经也有过和你一样的问题,并找到了解决方法

关键在于要对HoughCircles的操作有足够的直觉,以便您可以构建一个程序,自动调整各种图像中寻找圆形所需的超参数。

核心问题,一些直觉

HoughCircles并不是独立存在的,即使它使用最小和最大半径参数,它也需要运行数百或数千次迭代来自动调整和自动拨号正确的设置。然后,在完成后,您需要进行后处理验证步骤,以确保圆形是您想要的100%。问题在于您尝试手动调整输入参数以使用猜测和检查方法来调整HoughCircles自己。这根本行不通。让计算机为您自动调整这些参数。

何时手动调整HoughCircles可以令人满意?

如果您想手动硬编码参数,您绝对需要的是圆的精确半径,误差范围为一到两个像素。您可以猜测dp分辨率并设置累加器阵列投票阈值,您可能会得到满意结果。但是,如果您不知道半径,则HoughCircles的输出是无用的,因为它要么在图像中找到圆形,要么找不到。假设您通过手动调整找到了可接受的调整,则向其显示稍有不同的图像,您的HoughCircles会出现问题,并在图像中找到200个圆。毫无价值。

还有希望:

希望来自于HoughCircles即使在大型图像上也非常快速。您可以编写一个程序,让HoughCircles自动调整设置。如果您不知道半径且可能很小或很大,则从较大的“最小距离参数”,非常细的dp分辨率和非常高的投票阈值开始迭代。因此,当您开始迭代时,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秒钟的时间,但它几乎可以完美地完成任务(自动调谐的进一步手动微调可以使其变得次像素完美):

上述代码将此转换为: 原始图像

转换后如下所示:

霍夫圆变换

使这个工作可行的关键在于在开始之前拥有多少信息。如果您知道半径的容差范围,例如20个像素,那么这个方法可以完美地解决问题并且您就完成了。但是,如果您不知道,则必须通过精心逼近分辨率和投票阈值来聪明地追踪最大投票半径。如果圆形有奇怪的形状,则需要更高的dp分辨率,并且投票阈值需要探索更低的范围。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接