You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python检测图像中的同心圆?现有方法存问题求优化

检测图像中的同心圆问题

待检测的同心圆图像

我需要检测上述图像中的同心圆,已在Python中尝试cv2.findContours和cv2.HoughCircles方法,均在完成Canny、阈值等预处理后执行,但遇到了以下问题:

已尝试方法及问题

1. cv2.findContours检测问题

外圈圆因相互重叠被识别为单个轮廓,无法单独检测出外圈圆,相关代码如下:

contours, hier = cv2.findContours(input, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
    c= (random.randint(0,255),random.randint(0,255),random.randint(0,255))
    
    if area > 10000 and area < 100000:       # 筛选目标尺寸
        (x, y), radius = cv2.minEnclosingCircle(contour)
        center= (int(x), int(y))
        radius= int(radius)
        cv2.circle(color, center, radius, c,5)

检测结果:
findContours检测结果

2. cv2.HoughCircles检测问题

通过划分半径范围检测同心圆,结果有所改善但仍不理想,相关代码如下:

minR = 90  # 补充初始值,原代码中未定义
for maxR in range(100,200,10): # 半径范围从100到200,步长10
    circles = cv2.HoughCircles(input, cv2.HOUGH_GRADIENT, 1, 100, param1=100,param2=22,minRadius=minR,maxRadius=maxR)
    minR+=10
    
    # 检查是否有检测结果
    if circles is not None:
        # 将圆心坐标和半径转为整数
        circles = np.round(circles[0, :]).astype("int")

        for (x, y, r) in circles:
            c= (random.randint(0,255),random.randint(0,255),random.randint(0,255))
            cv2.circle(view, (x, y), r, c, 3)                                      # 绘制圆周
            cv2.rectangle(view, (x - 2, y - 2), (x + 2, y + 2), c, 5)              # 绘制圆心

检测结果:
HoughCircles检测结果

后续优化操作

我通过对比轮廓面积与最小外接圆面积,筛选出了圆形轮廓,代码如下:

contours, hier = cv2.findContours(aaa, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)

for contour in contours:
    c= (random.randint(0,255),random.randint(0,255),random.randint(0,255))
        
    area = cv2.contourArea(contour)
    (cx, cy), radius = cv2.minEnclosingCircle(contour)
    err=abs((area-np.pi*radius*radius)/(np.pi*radius*radius))
    
    if err < 0.1:
        cv2.drawContours(aaa, [contour], -1, (255,255,255) , thickness=cv2.FILLED)
        cv2.drawContours(color, [contour], -1, c , 5)

处理后得到的圆形轮廓:
筛选后的圆形轮廓

随后我将区域填充为白色:
填充后的图像

当前求助

现在问题简化为:如何将重叠的圆分离为独立的圆,并获取每个圆的圆心坐标与半径?

内容的提问来源于stack exchange,提问作者Haengyeong Kim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:26:20