OpenCV图像去噪及半圆检测实现方法咨询
半圆检测优化方案
标准霍夫圆检测针对完整圆周设计,半圆缺失部分弧段会导致投票数不足,直接使用要么参数设置过低引入大量误检,要么参数过高漏检目标,可通过以下方案优化:
- 替换原有的OTSU阈值预处理为「高斯模糊+Canny边缘提取」,能更精准保留圆弧边缘信息,减少噪声干扰
- 检测到候选圆后增加半圆有效性校验逻辑,通过统计候选圆周上边缘像素的占比过滤整圆和误检噪声
- 可根据实际半圆的弧段长度调整占比阈值,适配不同的半圆场景
优化后实现代码如下:
import cv2 import numpy as np # 读取灰度图 image = cv2.imread('path_here', 0) output = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) # 预处理:降噪+边缘提取 blur = cv2.GaussianBlur(image, (3, 3), 0) edges = cv2.Canny(blur, threshold1=20, threshold2=80) # 霍夫圆检测,适当降低param2适配不完整圆弧 circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp=1, minDist=110, param1=25, param2=12, minRadius=8, maxRadius=11) if circles is not None: detected_circles = np.uint16(np.around(circles)) for (x, y, r) in detected_circles[0, :]: edge_count = 0 total_points = 0 # 遍历圆周角统计边缘点占比,步长2度降低计算量 for angle in range(0, 360, 2): total_points += 1 px = int(x + r * np.cos(np.deg2rad(angle))) py = int(y + r * np.sin(np.deg2rad(angle))) if 0 <= px < edges.shape[1] and 0 <= py < edges.shape[0] and edges[py, px] == 255: edge_count += 1 # 边缘占比30%-70%判定为半圆,可根据实际场景调整阈值 ratio = edge_count / total_points if 0.3 <= ratio <= 0.7: cv2.circle(output, (x, y), r, (0, 0, 255), 2) cv2.circle(output, (x, y), 2, (0, 255, 255), 2) cv2.imshow('semicircle_output', output) cv2.waitKey(0) cv2.destroyAllWindows()
如果你的半圆弧段更短或者更长,对应调整ratio的上下限即可,比如1/4圆可以把下限调到0.2,3/4圆可以把上限调到0.8。
内容的提问来源于stack exchange,提问作者Oliver
相关产品推荐
相关产品推荐

