所以我试图在c上编码霍夫变换。我有一个二进制图像,并从图像中提取了二进制值。现在要做这个变换我需要把图像中的[X,Y]值转换成[,]来做一个形式为
的参数变换ρ= xcos(θ)+ ysin(θ)
我不太明白它实际上是如何转换的,看看其他在线代码。任何有助于解释算法和[rho,theta]值的累加器应该如何基于[X,Y]完成,将不胜感激。提前感谢。:)
你的问题暗示了这样一个事实:你认为你需要将图像中的每个(X,Y)兴趣点映射到一个 (rho, θ)向量在霍夫空间
。事实是图像中的每个点都映射到曲线,即霍夫空间中的几个向量。每个输入点的向量数量取决于您决定的一些"任意"分辨率。例如,对于1度分辨率,您将在霍夫空间中获得360个向量。
对于(,)向量有两种可能的约定:要么用[0,359]度表示,在这种情况下,总是正的,要么用[0,179]度表示,允许为正或负。后者通常用于许多实现。
一旦你理解了这一点, Accumulator就是一个二维数组,它覆盖了(rho, θ)空间的范围,其中每个单元格初始化为0。用于计算输入中不同点的各种曲线共有的向量的个数。
因此,该算法计算输入图像中每个感兴趣点的所有360个向量(假设θ的分辨率为1度)。对于这些向量,将rho舍入到最接近的整数值后(取决于rho维度的精度,例如,如果每个单位有2个点,则为0.5),它会在累加器中找到相应的单元格,并增加该单元格中的值。当对所有感兴趣的点都这样做后,算法搜索累加器中值高于所选阈值的所有单元格。(,)这些单元格的"地址"是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.
总的来说,这是一个相当简单的算法。复杂性主要在于与单位保持一致,例如度数和弧度之间的转换(大多数数学库的三角函数都是基于弧度的),以及用于输入图像的坐标系。