Image processing and computer vision--Chapter 5-Image segmentation-Hough transform

1. Introduction to the principle of Hough Transform

 Hough Transform is a commonly used computer vision graphics inspection method. Hough Transform is generally used to inspect straight lines or circles.

 The principle of Hough transform is as follows:
 Assume that there is a straight line in the image, and the expression is as follows:
y = kx + by=kx+by=kx+bSuppose
 we arbitrarily specify a point(x 0, y 0) (x_{0},y_{0})(x0,y0) , then for any straight line passing through this point, the following formula must be true:
b = − kx 0 + y 0 b=-kx_{0}+y_{0}b=kx0+y0
 At this time, I change the image with x and y as axes into an image with b and k as axes. At this time, the straight line can also change, and as can be deduced above, the corresponding image is also a straight line, as shown in the figure:
Insert image description here
 Further , we take a point from the straight line (x 1, y 1) (x_{1},y_{1})(x1,y1) , then there must be the following formula:
b = − kx 1 + y 1 b=-kx_{1}+y_{1}b=kx1+y1
 Drawing such a function on the image, we can see that the two straight lines intersect at a point (k ∗ , b ∗ ) (k^{*},b^{*})(k,b )(x 1, y 1) (x_{1},y_{1})on the(x1,y1)( x 0 , y 0 ) (x_{0},y_{0})(x0,y0) a straight line determined by two points.
![Insert image description here](https://img-blog.csdnimg.cn/905cb7cad8ce40eaa338bde0626db96d.png
 But in our actual straight line detection, we will not use the above coordinate system method. The above method only provides a solution idea. We will use polar coordinate equations to complete the solution of the above method. For the above straight line, polar coordinates The equation can be expressed as:
ρ = xcos θ + sin θ \rho=xcos\theta+sin\thetar=xcosθ+s in θwhere
 ,θ \thetaθ is the angle between the normal vector of the straight line and the positive direction of the x-axis, andρ \rhoρ is the vertical distance from the origin of the coordinate system to the straight line, as shown in the figure below: As shown
Insert image description here
 below, we can find that there is only one straight line in polar coordinates (ρ \rhoρ, θ \theta θ ) Correspondingly, changing the size of a parameter will change the straight line transformed into the spatial domain. And all points on this straight line in the airspace can be in polar coordinates (ρ \rhoρ, θ \theta How many polar coordinate pairs a point on the straight line represented by θ
Insert image description here
) (as shown in the figure below)  correspond to in the polar coordinate system depends onθ \thetaThe step size of θ , if the step size isβ \betaβ , then the expression of polar coordinates for n is as follows:
n = 360 β n=\frac{360}{\beta}n=b360
 The corresponding picture is as follows:
Insert image description here
 Next, we assume that the polar coordinate curve corresponding to three points in the airspace is as shown in (a) of the figure below. The polar coordinate curve passes through a point at the same time, which means that there is a straight line in the airspace passing through these three points. Just look for the point with the most intersections, which is the straight line you are looking for in the airspace.
Insert image description here

2. Hough Transform algorithm process

·Hough变换直线检测的步骤如下:
1.0的取值范围为[0,360],单位为度根据检测精度要求,采取适当的步长对角度和长度的取值范围进行离散化,形成0-p平面上的离散网格。
2.将每一个离散网格视为一个投票累加器,初始时全部清03.遍历图像的所有像素,对于每个像素计算离散值0i和p=xcos0+ysin0.
4.对在参数空间中将对应的累加器中的值加1,从而完成求出相应的离散化值p,对于每个(p,0)该像素点的投票的投票之后,在离散化的参数空间中找出所累积的投票值
5.访问完所有的图像像素并完成所有,点这些点所对应的参数即为检测得到的直线的参数大于某给定闽值T的局部极大值点,

3. Hough Transform algorithm code

import numpy as np
import cv2
from PIL import Image,ImageEnhance 
import matplotlib.pyplot as plt
"""
hough变换是一种常用的计算机视觉图形检验方法,霍夫变换一般用于检验直线或者圆。

"""
img = Image.open(r"C:\Users\Zeng Zhong Yan\Desktop\py.vs\python学习\test.webp")
#增强图像效果
img = ImageEnhance.Contrast(img).enhance(3)
img.show()
#处理成矩阵,便于后续处理
img = np.array(img)
#灰度处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#cv2.THRESH_OTSU具有双峰值,显示效果更好.
"""
cv2.THRESH_OTSU使用最小二乘法处理像素点。一般情况下,cv2.THRESH_OTSU适合双峰图。
cv2.THRESH_TRIANGLE使用三角算法处理像素点。一般情况下,cv2.THRESH_TRIANGLE适合单峰图。
"""
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
#canny边缘检验算法处理
result = cv2.Canny(thresh, ret-30, ret+30, apertureSize=3)

#霍夫变换检测直线
lines = cv2.HoughLinesP(result, 1, 1 * np.pi / 180, 10, minLineLength=10, maxLineGap=5)
# 画出检测的线段
for line in lines:
    for x1, y1, x2, y2 in line:
        cv2.line(img, (x1, y1), (x2, y2), (255, 0, 0),2)
img = Image.fromarray(img, 'RGB')
img.show()

4. Effect of Hough Transform algorithm

1. The original picture is as follows:
Insert image description here
2. The effect of detecting straight lines after Hough transform
Insert image description here

Guess you like

Origin blog.csdn.net/m0_71819746/article/details/133381679