霍夫变换的标准形式--The Hough Transform: Normal form

本文详细介绍了霍夫变换中的缺点及其解决方法,通过使用正交形式替代斜率截距形式来克服斜率趋向无穷大的问题。解释了正交形式的原理,包括角度和距离参数的有限取值范围,以及如何将直线方程转换为正交形式。阐述了在正交形式下,点到线的“投票”过程在xy空间到θp空间的转换,并讨论了检测直线和提高准确性的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Hough Transform: Normal form

name="fbee90b4" width="595px" height="1000px" frameborder="0" allowtransparency="true" allowfullscreen="true" scrolling="no" title="fb:like Facebook Social Plugin" src="http://www.facebook.com/v2.4/plugins/like.php?action=like&app_id=&channel=http%3A%2F%2Fstaticxx.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D42%23cb%3Dfa4dc25e%26domain%3Daishack.in%26origin%3Dhttp%253A%252F%252Faishack.in%252Ff1bad9d794%26relation%3Dparent.parent&container_width=597&href=http%3A%2F%2Ffacebook.com%2Faishack&layout=standard&locale=en_US&sdk=joey&show_faces=true&width=595" style="box-sizing: border-box; position: absolute; border-style: none; border-width: initial; visibility: visible; width: 0px; height: 0px;">

The flaw

The Hough transform described in the previous article has an obvious flaw. The value of m (slope) tends to infinity for vertical lines. So you need infinite memory to be able to store the mc space. Not good.

The solution

The problem is resolved by using a different parametrization. instead of the slope-intercept form of lines, we use the normal form.

The normal form

In this representation, a line is formed using two parameters - an angle θ and a distance p. p is the length of the normal from the origin (0, 0) onto the line. and θ is the angle this normal makes with the x axis. This solves the flaw perfectly.

The angle θ can be only from -90o to +90o and the length p can range from 0 to length of diagonal of the image. These values are finite, and thus you can "store" them on a computer without much trouble.

In this representation, the equation of the line is:

where  is a point through which the line passes.

With this new equation, we have a few changes in the end-point to line "transition" from the xy space to the pθ space. A line in the xy space is still equivalent to a point in the pθ space. But a point in the xy space is now equivalent to a sinusoidal curve in the pθ space.

The image at the top of this article is an actual pθ space. The sinusoidal curves have been generated while trying to figure out lines an image.

Implementation

With those ideas through, we're ready to implement the Hough transform. The idea is to let each pixel "vote". So an array of accumulator cells is created.

In our case, the accumulator cells form a 2D array. The horizontal axis is for the different θ values and the vertical axis for p values.

The accumulator

Next, you loop through every pixel of the edge detected image (Hough works only on edge detected images... not on normal images).

If a pixel is zero, you ignore it. It's not an edge, so it can't be a line. So move on to the next pixel.

If a pixel is nonzero, you generate its sinusoidal curve (in the pθ space). That is, you take θ = -90 and calculate the corresponding p value. Then you "vote" in the accumulator cell (θ, p). That is, you increase the value of this cell by 1. Then you take the next θ value and calculate the next p value. And so on till θ = +90. And for every such calculation, making sure they "vote".

So for every nonzero pixel, you'll get a sinusoidal curve in the pθ space (the accumulator cells). And you'll end up with an image similar to the one at the top.

Detecting lines

The image at the top has several "bright spots". A lot of points "voted" for this spot. And these are the parameters that describe the lines in the original image. Simple as that.

Accuracy

The accuracy of the Hough transform depends on the number of accumulator cells you have. Say you have only -900, -450, 00, 450 and 900 as the cells for θ values. The "voting" process would be terribly inaccurate. Similarly for the p axis. The more cells you have along a particular axis, the more accurate the transform would be.

Also, the technique depends on several "votes" being cast into a small region. Only then would you get those bright spots. Otherwise, differentiating between a line and background noise is one really tough job.

Done

Now you know how the standard Hough transform works! You can try implementing it... should be simple enough.


from: http://aishack.in/tutorials/hough-transform-normal/

### 直线检测的随机取样方法 在计算机视觉领域,直线检测是一种常见的任务,通常可以通过霍夫变换Hough Transform)来完成。然而,当面对噪声较多的数据或者需要提高效率时,RANSAC(Random Sample Consensus, 随机采样一致性)成为了一种非常有效的替代方案。 #### RANSAC 的基本原理 RANSAC 是一种迭代方法,用于从一组包含离群点的数据中估计数学模参数。对于直线检测而言,其目标是从二维平面上的一组点中找到最佳拟合的直线方程 \( y = ax + b \),其中 \( a \) 和 \( b \) 分别表示斜率和截距[^1]。 以下是 RANSAC 应用于直线检测的核心过程: - **随机选取两点**:每次迭代中,随机选择两个点作为候选点,并通过这两点计算一条可能的直线。 - **评估支持度**:将剩余的所有点投影到这条直线上,判断它们与该直线的距离是否小于预定义阈值。如果满足条件,则认为这些点属于内点集合。 - **更新最优模**:记录当前模的支持点数量。如果有新的模拥有更多的内点,则将其设为当前的最佳模- **重复上述步骤**:直到达到最大迭代次数或找到足够好的模为止。 这种方法能够有效减少异常值的影响,从而得到更加鲁棒的结果。 #### Python 实现示例 下面是一个简单的基于 OpenCV 的 RANSAC 直线检测实现代码片段: ```python import numpy as np import cv2 def ransac_line_detection(points, threshold=1.0, iterations=1000): best_inliers = None best_model = None for _ in range(iterations): # Step 1: Randomly select two points to form a line model. indices = np.random.choice(len(points), size=2, replace=False) point1, point2 = points[indices] # Calculate the parameters of the line (y = mx + c). if point2[0] != point1[0]: m = (point2[1] - point1[1]) / (point2[0] - point1[0]) c = point1[1] - m * point1[0] else: continue # Step 2: Evaluate all other points against this line model. distances = [] for p in points: distance = abs(m*p[0]-p[1]+c)/np.sqrt(m**2+1) distances.append(distance) # Count how many are within our tolerance level 'threshold'. current_inliers = [points[i] for i, d in enumerate(distances) if d < threshold] # Update the best found solution so far. if not best_inliers or len(current_inliers) > len(best_inliers): best_inliers = current_inliers best_model = (m, c) return best_model, best_inliers # Example usage with synthetic data. if __name__ == "__main__": pts = [(i, 2*i + 3 + np.random.normal(scale=5)) for i in range(-100, 100)] model, inliers = ransac_line_detection(pts, threshold=5, iterations=100) print(f"Best Model Found: {model}") ``` 这段程序展示了如何利用 RANSAC 来寻找最适配的直线以及对应的内点集合。 #### 吉布斯采样的补充说明 虽然吉布斯采样主要用于高维空间的概率分布近似问题,但在某些特定情况下也可以扩展应用于低维优化场景下的初始猜测生成阶段。不过它并不直接适用于像这里讨论的标准形式化直线拟合任务[^2]。 关于训练过程中涉及的具体数值确定方式及其背后机制则进一步涉及到机器学习框架内的概念解释[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值