【问题标题】:Unable to find correct circles with cv2.HoughCircles无法使用 cv2.HoughCircles 找到正确的圆圈
【发布时间】:2021-04-16 20:45:59
【问题描述】:

当我运行下面的代码时,我无法在图像中找到一致的圆圈。我用作输入的图像是:

import numpy as np
import matplotlib.pyplot as plt
import cv2
img = cv2.imread("pipe.jpg")


# convert the image to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# copy the RGB image
cimg = img.copy()
# convert the RGB image to grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# detect circles using hough transformation
circles = cv2.HoughCircles(image=img, method=cv2.HOUGH_GRADIENT, dp=3,
                        minDist=60, param1=100, param2=39, maxRadius=200)

for co,i in enumerate(circles[0, :],start=1):
    i = [round(num) for num in i]
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
print("Number of circles detected:", co)
plt.imshow(cimg)
plt.show()

我得到的结果是:

【问题讨论】:

  • 我还建议在去饱和图像上尝试Laplacian Transform 以帮助您找到边缘,然后扩展该新图像以增强/增亮那些圆环。这是一些输出图像的link。如果您喜欢我的方法并认为可以将其应用到您的代码中,我可以将其代码发布为新答案。
  • 我相信你的 minDist 值太大了。也许您需要从文档中减少您的价值:“minDist”检测到的圆圈中心之间的最小距离。如果参数太小,除了一个真实的圆圈外,可能还会错误地检测到多个相邻圆圈。如果太大,可能会漏掉一些圈子。”见docs.opencv.org/4.1.1/dd/d1a/…

标签: python image opencv image-processing hough-transform


【解决方案1】:

作为预处理步骤,您通常会在检测之前对图像进行平滑处理。不平滑会提示很多错误检测。有时您在某些教程中看不到预平滑,因为处理的图像具有非常干净的边缘和非常少的噪点。在执行检测之前尝试使用中值模糊或高斯模糊。

因此,请尝试以下操作:

import cv2

img = cv2.imread("pipe.jpg")

# convert the image to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# copy the RGB image
cimg = img.copy()
# convert the RGB image to grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

### NEW - Smooth the image first
blur = cv2.GaussianBlur(img, (5, 5), 0)
# or try
# blur = cv2.medianBlur(img, 5)

# detect circles using hough transformation
circles = cv2.HoughCircles(image=blur, method=cv2.HOUGH_GRADIENT, dp=3,
                        minDist=60, param1=100, param2=39, maxRadius=200)

除此之外,要通过检测来找到图像中的所有圆圈,还需要使用 Circular Hough 变换的超参数并通过反复试验。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多