霍夫变换未检测到正确的圆

Hough transform not detecting correct circle

我正在做一个项目,我必须在其中检测交通信号灯(显然是圆圈)。现在我正在处理从某个地方拾取的示例图像,但是在我所有的努力之后我无法获得代码来检测正确的圆(光)。

这是代码:-

# import the necessary packages  
import numpy as np  
import cv2

image = cv2.imread('circleTestsmall.png')
output = image.copy()
# Apply Guassian Blur to smooth the image
blur = cv2.GaussianBlur(image,(9,9),0)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 200)

# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")

# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
    # 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, 255, 0), 4)
    cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

# show the output image
cv2.imshow("output", output)
    cv2.imshow('Blur', blur)
cv2.waitKey(0)

我要检测圆圈的图片-

输出图像是这样的:-

我尝试在霍夫变换中使用高斯模糊半径值和 minDist 参数,但没有取得多大成功。
谁能指出我正确的方向?

P.S- 一些题外话但对我的项目至关重要的问题-
1. 我的电脑大约需要 6-7 秒才能显示最终图像。是我的代码不好还是我的电脑不好?我的规格是 - Intel i3 M350 2.6 GHz(第一代)、6GB RAM、Intel HD Graphics 1000 1625 MB。
2. 霍夫变换是否可以直接作用于二值阈值图像?
3. 这段代码 运行 在 Raspberry Pi 3 上是否足够快成为实时的? (我要把它安装在移动的自主机器人上。)

谢谢!

首先你应该限制你的参数。

请参考:http://docs.opencv.org/2.4/modules/imgproc/doc/feature_detection.html#houghcircles

至少为最小和最大半径设置合理的值。尝试首先找到一个特定的圈子。如果你成功了,增加你的半径公差。

霍夫变换是一种蛮力方法。它将为图像中的每个边缘像素尝试任何可能的半径。这就是为什么它不太适合实时应用程序的原因。特别是如果您没有提供正确的参数和输入。你没有半径限制 atm。因此,您将为每个像素计算数百个(如果不是数千个)圆圈...

在你的情况下,红绿灯也不是很圆,所以累积的结果不会很好。尝试寻找合理大小的高度饱和、明亮、紧凑的斑点。它应该更快、更健壮。

如果限制图像大小,您可以进一步缩短处理时间。我猜你可以假设交通灯总是在你图像的上半部分。所以省略下半部分。交通灯总是绿色、红色或黄色。删除所有不是那种颜色的东西……我想你明白我的意思了……

另一个限制:仅接受内部颜色为 homogenous.This 的圆圈将丢弃上述示例中的所有错误命中。

我认为您应该首先根据红绿灯颜色进行颜色分割。它将极大地降低投资回报率。然后你可以只在 ROI 边缘应用霍夫变换(因为你想要轮廓)。