霍夫圆变换(Hough Circle Transform)是一种用于检测图像中圆形的技术。在Python中,霍夫圆变换可以使用OpenCV库来实现。

下面是一个使用霍夫圆变换检测图像中圆形的示例代码:

import cv2
import numpy as np

# 读取图像
image = cv2.imread('circle.png', 0)

# 使用霍夫圆变换检测圆形
circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 1, 100,
                           param1=50, param2=30, minRadius=0, maxRadius=0)

# 将检测到的圆形标记在图像上
if circles is not None:
    circles = np.uint16(np.around(circles))
    for circle in circles[0, :]:
        center = (circle[0], circle[1])
        radius = circle[2]
        cv2.circle(image, center, radius, (0, 255, 0), 2)

# 显示结果图像
cv2.imshow('Circle Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

在上述代码中,我们首先使用cv2.imread()函数读取一张包含圆形的图像。然后,使用cv2.HoughCircles()函数进行霍夫圆变换,其中param1param2是算法的参数,控制圆形检测的灵敏度。最后,我们将检测到的圆形标记在图像上,并使用cv2.imshow()函数显示结果图像。

请注意,上述代码中的circle.png是需要检测圆形的图像文件名。你可以将代码中的文件名替换为你自己的图像文件名


原文地址: http://www.cveoy.top/t/topic/h70k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录