在图像中检测重叠嘈杂的圆形

3

我尝试使用Python的OpenCV识别以下图像中的两个区域:内圆内部的区域和外圆与内圆之间的边界区域。

enter image description here

我尝试过不同的方法,例如:

但这些方法并不是很适合。

这个问题是否可以通过传统的图像处理方法解决,还是需要神经网络的帮助?

编辑:使用OpenCV霍夫圆检测在图像中检测圆形

# import the necessary packages
import numpy as np
import argparse
import cv2
from PIL import Image

# 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"])
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 500)
# 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
    img = Image.fromarray(image)
    if img.height > 1500:
        imS = cv2.resize(np.hstack([image, output]), (round((img.width * 2) / 3), round(img.height / 3)))
    else:
        imS = np.hstack([image, output])
    # Resize image
    cv2.imshow("gray", gray)
    cv2.imshow("output", imS)
    cv2.waitKey(0)
else:
    print("No circle detected")

测试图片: 测试图片


你能分享一下你声称houghcircle无法工作的代码吗?可能是参数选择的问题。当然,对于这个简单的任务,你不需要神经网络。 - Yunus Temurlenk
这基本上是源代码,但我更新了答案。谢谢。 - tomole
1个回答

6

一般错误:在使用HoughCircles()函数时,应恰当选择参数。我看到您的代码中只使用了前4个参数。您可以在这里找到关于这些参数的好建议。

经验技巧:在使用HoughCircles函数时,我发现如果两个圆的中心重合或几乎靠近,HoughCircles无法检测到它们。即使您将min_dist parameter赋值为一个小数。在您的情况下,圆的中心也是重合的。

我的建议:我将以适用于两个圆的代码附上相应的参数。由于我上面解释的问题,我找不到一个参数列表来识别两个圆。我的建议是将这两个参数应用于同一图像两次,并获取圆的结果。

外圆结果和包含参数的代码:

结果:

enter image description here

# import the necessary packages
import numpy as np
import argparse
import cv2
from PIL import Image

# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread('image.jpg')
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray = cv2.medianBlur(gray,15)
rows = gray.shape[0]

# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT,1, rows / 8,
                               param1=100, param2=30,
                               minRadius=200, maxRadius=260)
# 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
    img = Image.fromarray(image)
    if img.height > 1500:
        imS = cv2.resize(np.hstack([image, output]), (round((img.width * 2) / 3), round(img.height / 3)))
    else:
        imS = np.hstack([image, output])
    # Resize image
    cv2.imshow("gray", gray)
    cv2.imshow("output", imS)
    cv2.waitKey(0)
else:
    print("No circle detected")

对于内部圆的参数:

circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT,1, rows / 8,
                                   param1=100, param2=30,
                                   minRadius=100, maxRadius=200)

结果:

这里输入图片描述


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