2014-10-28 58 views
1

我一直在使用Python和numpy实现标准Hough变换来进行线条检测。为什么hough变换曲线返回正弦曲线中途分开?

我已经成功实现了该算法,但是它的输出将得到的正弦波分成了两半。其中一半处于所得图像的一个极端,其余的处于图像的另一部分。

这里的输出我得到的一个例子:

This is the result of the algorithm.

这里是我的代码:

def houghTransf(img, r_ro, r_theta,thrs): 

    linhas, colunas =img.shape 

    theta = np.linspace(0.0,180.0,np.ceil(180.0/r_theta)+1) 

    max_rho = np.sqrt((linhas**2)+(colunas**2)) 

    rho = np.linspace(0.0,max_rho,np.ceil(max_rho/r_ro)+1) 


    res = np.zeros((len(theta),len(rho))) 

    # Hough Transform 

    for i in range(linhas): 
     for j in range(colunas): 
      if(img[i,j]<>0): 
       for k in theta: 
        v_rho = i*np.sin(k*(np.pi/180)) + j*np.cos(k*(np.pi/180)) 
        res[k,v_rho] += 1 

    return res 

我怀疑,这个问题是某处霍夫空间的定义(定义thetarho),但更改linspace中每个的最小限制似乎没有帮助。

有没有什么方法可以显示正弦曲线,而不会像图像中那样分裂?

可以以任何方式帮助调整rhotheta的范围吗?

编辑:

我也试着只用一个线运行算法。

enter image description here

下面是我实现的算法的情况下的输出中,只有一个行:

enter image description here

回答

1

请注意,此行之后:

v_rho = i*np.sin(k*(np.pi/180)) + j*np.cos(k*(np.pi/180)) 

v_rho可能会消极。你应该一半的范围内添加到它,像这样:

v_rho = 1.0*len(rho)/2 + i*np.sin(k*(np.pi/180)) + j*np.cos(k*(np.pi/180)) 

(你需要验证,如果范围是所有的权利,也许rho现在需要两倍大,不知道)。

这个问题对你来说是隐藏的,因为Python和numpy允许索引带负数的数组。这个修复会将您的正弦曲线的一半范围转换到右侧,这样它们就不会再分裂了。

+0

正确。而rho真的需要两倍的大才能工作。 – 2014-11-04 01:18:59