如何基于OpenCV&Python实现霍夫变换圆形检测

2023-12-18Python编程
70

下面是基于OpenCV&Python实现霍夫变换圆形检测的完整攻略:

1. 什么是霍夫变换

霍夫变换(Hough Transform)是一种图像处理算法,其功能是能够从边缘检测结果中得到直线或圆的方程表达式,即通过边缘点构造直线或圆,并统计在不同参数下断言通过该参数的点的数量,从而得到边缘的位置. 针对圆形检测,霍夫变换算法可以方便地实现圆心的检测。

2. 利用OpenCV实现霍夫圆形检测

2.1 程序示例1

下面是一个简单的程序示例,使用了OpenCV库函数来检测圆形。

import cv2

# load the image and convert it to grayscale
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# define the range of radii to be detected
min_radius = 10
max_radius = 30

# apply the HoughCircles function to detect circles
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)

# 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 and update the list of markers
        cv2.circle(image, (x, y), r, (0, 255, 0), 2)

    # show the output image
    cv2.imshow("output", image)
    cv2.waitKey(0)

在上面的程序中,cv2.HoughCircles函数可以直接进行圆形检测。其中,gray是输入图像的灰度图像,cv2.HOUGH_GRADIENT是圆形检测方法,1.2是圆形中心之间的最小距离,100是Canny边缘检测器的上阈值。

2.2 程序示例2

下面是一个更加详细的程序示例,使用了手动实现霍夫圆形检测算法。

import cv2
import numpy as np

# load the image and convert it to grayscale
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# apply edge detection (using Canny algorithm)
edges = cv2.Canny(gray, 50, 150)

# initialize accumulator (for the Hough transform)
accumulator = np.zeros((gray.shape[0], gray.shape[1], 30))

# loop through all edge pixels
for y in range(edges.shape[0]):
    for x in range(edges.shape[1]):
        if edges[y, x] == 255:  # if edge pixel
            # loop through a range of radii
            for r in range(10, 40):
                # for each radius, compute the center (x_,y_) of the circle
                for i in range(0, 360):
                    a = x - r * np.cos(i * np.pi / 180)
                    b = y - r * np.sin(i * np.pi / 180)
                    if a >= 0 and a < gray.shape[1] and b >= 0 and b < gray.shape[0]:
                        accumulator[int(b), int(a), r - 10] += 1

# get the (x,y,r) of the center and radius of each detected circle
circles = []
for y in range(gray.shape[0]):
    for x in range(gray.shape[1]):
        for r in range(0, 30):
            if accumulator[y, x, r] > 70:  # if enough edge support
                # add it to the list of detected circles
                circles.append((x, y, r + 10))

# loop through all detected circles and draw them on the output image
for (x, y, r) in circles:
    cv2.circle(image, (x, y), r, (0, 255, 0), 2)

# show the output image
cv2.imshow("output", image)
cv2.waitKey(0)

在上面的程序中,利用Canny算法对灰度图像进行了边缘检测。然后,程序自己实现了霍夫圆形检测算法,可以通过设定r的范围和阈值进行调整。最后,程序解析出检测到的圆的位置和半径,并在原图上画出圆。

The End

相关推荐

解析Python中的eval()、exec()及其相关函数
Python中有三个内置函数eval()、exec()和compile()来执行动态代码。这些函数能够从字符串参数中读取Python代码并在运行时执行该代码。但是,使用这些函数时必须小心,因为它们的不当使用可能会导致安全漏洞。...
2023-12-18 Python编程
117

Python下载网络文本数据到本地内存的四种实现方法示例
在Python中,下载网络文本数据到本地内存是常见的操作之一。本文将介绍四种常见的下载网络文本数据到本地内存的实现方法,并提供示例说明。...
2023-12-18 Python编程
101

Python 二进制字节流数据的读取操作(bytes与bitstring)
来给你详细讲解下Python 二进制字节流数据的读取操作(bytes与bitstring)。...
2023-12-18 Python编程
120

Python3.0与2.X版本的区别实例分析
Python 3.x 是 Python 2.x 的下一个重大版本,其中有一些值得注意的区别。 Python 3.0中包含了许多不兼容的变化,这意味着在迁移到3.0之前,必须进行代码更改和测试。本文将介绍主要的差异,并给出一些实例来说明不同点。...
2023-12-18 Python编程
34

python如何在终端里面显示一张图片
要在终端里显示图片,需要使用一些Python库。其中一种流行的库是Pillow,它有一个子库PIL.Image可以加载和处理图像文件。要在终端中显示图像,可以使用如下的步骤:...
2023-12-18 Python编程
91

Python图像处理实现两幅图像合成一幅图像的方法【测试可用】
在Python中,我们可以使用Pillow库来进行图像处理。具体实现两幅图像合成一幅图像的方法如下:...
2023-12-18 Python编程
103