使用霍夫圆变换检测圆形轮廓

import cv2
import numpy as np

# 读取图像
image_path = r"C:\Users\yh\Pictures\2.png"
img = cv2.imread(image_path)

# 将图像转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 进行阈值分割
_, thresholded = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 形态学操作 - 填充孔洞
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
filled = cv2.morphologyEx(thresholded, cv2.MORPH_CLOSE, kernel)
# 进行边缘检测
edges = cv2.Canny(filled, 100, 200)

# 使用霍夫圆变换检测圆形轮廓
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp=1, minDist=50,
                           param1=4, param2=4, minRadius=340, maxRadius=380)

# 在原始图像上标注圆弧位置
if circles is not None:
    circles = np.round(circles[0, :]).astype(int)

    for (x, y, r) in circles:
        if ((x+1)>filled.shape[0]) or ((y+1)>filled.shape[1]):
            continue
        if filled[y][x]==0:
            continue
        # 绘制圆心和圆
        cv2.circle(img, (x, y), r, (0, 0, 255), 2)
        cv2.circle(img, (x, y), 3, (0, 255, 0), -1)

# 显示结果图像
cv2.imshow("Result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值