gpt4 book ai didi

python - 检测图像中重叠的嘈杂圆圈

转载 作者:行者123 更新时间:2023-12-02 16:06:54 25 4
gpt4 key购买 nike

我尝试在下图中识别两个区域。内部区域和内部区域之间的区域-边界-用python openCV圈起来。

enter image description here

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

  • Detecting circles images using opencv hough circles
  • Find and draw contours using opencv python

  • 那不太适合。

    使用经典图像处理甚至可以做到这一点,还是我需要一些神经网络?

    编辑: Detecting circles images using opencv hough circles
    # 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")

    测试图片:
    Testimage

    最佳答案

    一般错误:当使用HoughCircles() ,参数应适当选择。我看到您只在代码中使用前4个参数。 Ypu可以检查here以获得关于这些参数的好主意。

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

    我的建议:我将在两个圆圈的代码中附加适当的参数。由于上述问题,我无法通过一个参数列表找到2个圆。我的建议是对同一张图片两次应用这两个参数,然后获取圆并获取结果。

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

    结果:

    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)

    结果:

    enter image description here

    关于python - 检测图像中重叠的嘈杂圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62040216/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com