3

所以我试图在 C 上编写霍夫变换。我有一个二进制图像并从图像中提取了二进制值。现在要进行霍夫变换,我必须将图像中的 [X,Y] 值转换为 [rho,theta] 以进行形式的参数变换

rho=xcos(θ)+ysin(θ)

我不太明白它实际上是如何转换的,查看其他在线代码。任何解释算法以及 [rho,theta] 值的累加器应如何基于 [X,Y] 完成的帮助将不胜感激。在此先感谢。:)

4

1 回答 1

10

您的问题暗示您认为需要将图像中的每个 (X,Y) 兴趣点映射到霍夫空间中的一个 (rho, theta)向量

事实上,图像中的每个点都映射到一条曲线,即霍夫空间中的几个向量。每个输入点的向量数量取决于您决定的一些“任意”分辨率。例如,对于 1 度分辨率,您将在霍夫空间中获得 360 个向量。

对于 (rho, theta) 向量,有两种可能的约定:要么对 theta 使用 [0, 359] 度范围,在这种情况下 rho 始终为正,要么对 theta 使用 [0,179] 度并允许 rho要么是正面的,要么是负面的。后者通常用于许多实现中。

一旦你理解了这一点,累加器就是一个二维数组,它覆盖了 (rho, theta) 空间的范围,每个单元格都初始化为 0。它用于计算常见向量的数量到输入中不同点的各种曲线

因此,该算法为输入图像中的每个兴趣点计算所有 360 个向量(假设 theta 分辨率为 1 度) 。对于这些向量中的每一个,在将 rho 舍入到最接近的整数值之后(取决于 rho 维度的精度,例如,如果我们每单位有 2 个点,则为 0.5),它会在累加器中找到相应的单元格,并增加该单元格中的值.
当对所有兴趣点都完成此操作后,算法会搜索累加器中所有值高于所选阈值的单元格。这些单元格的 (rho, theta) “地址”是 Hough 算法已识别的线(在输入图像中)的极坐标值。

现在,请注意,这会为您提供线方程,通常会留下计算出有效属于输入图像的这些线段

上面的一个非常粗略的伪代码“实现”

Accumulator_rho_size = Sqrt(2) * max(width_of_image, height_of_image)
                          * precision_factor  // e.g. 2 if we want 0.5 precision
Accumulator_theta_size = 180  // going with rho positive or negative convention
Accumulator = newly allocated array of integers
    with dimension  [Accumulator_rho_size, Accumulator_theta_size]
Fill all cells of Accumulator with 0 value.

For each (x,y) point of interest in the input image
   For theta = 0 to 179
       rho = round(x * cos(theta) + y * sin(theta),
                   value_based_on_precision_factor)
       Accumulator[rho, theta]++

Search in Accumulator the cells with the biggest counter value
(or with a value above a given threshold) // picking threshold can be tricky

The corresponding (rho, theta) "address" of these cells with a high values are
the polar coordinates of the lines discovered in the the original image, defined
by their angle relative to the x axis, and their distance to the origin.

Simple math can be used to compute various points on this line, in particular
the axis intercepts to produce a  y = ax + b equation if so desired.

总的来说,这是一个相当简单的算法。复杂性主要在于与单位保持一致,例如度数和弧度之间的转换(大多数数学库的三角函数都是基于弧度的),以及用于输入图像的坐标系。

于 2013-06-07T03:37:46.097 回答