检测图像中重叠的噪声圆圈

tom*_*ole 4 python opencv image-processing image-recognition

我尝试识别下图中的两个区域。内部区域内部以及外部和内部之间的区域 - 边界 - 使用 python openCV 进行圆化。

在此输入图像描述

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

这不太合适。

这对于经典图像处理来说是可能的还是我需要一些神经网络?

编辑:使用 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")
Run Code Online (Sandbox Code Playgroud)

测试图片: 测试图像

Yun*_*enk 6

常见错误:使用时HoughCircles(),应适当选择参数。我发现您在代码中只使用了前 4 个参数。您可以在此处查看以更好地了解这些参数。

经验想法:在使用时HoughCircles,我注意到如果两个圆的两个中心相同或几乎彼此接近,则HoughCircles无法检测到它们。即使您分配min_dist parameter一个很小的值。在你的情况下,圆心也相同。

我的建议:我将在两个圆圈的代码中附加适当的参数。由于我上面解释的问题,我找不到带有一个参数列表的 2 个圆圈。我的建议是对同一张图像应用这两个参数两次,然后得到圆圈并得到结果。

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

结果:

在此输入图像描述

# 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")
Run Code Online (Sandbox Code Playgroud)

对于内圆参数:

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

结果:

在此输入图像描述