2017-04-23 108 views
2

我正在Python中编写一个基本的霍夫变换 - 我相信我已经在概念上正确的了,但是,我的结果似乎被抵消了,因此它被分割成顶部和底部,而不是连续的。应该是什么我想是这样的:Hough Transform in Python - 结果不正确的偏移 - 索引错误?

enter image description here

但我得到这个:

enter image description here

这是接近的,但似乎是通过中间四分五裂!我相信这是由于我对rho/theta数组进行了索引,但是尽管我做了许多改动,但我无法解决这个问题!任何关于我的错误步骤和我需要改变的解释都会非常感激!

我的源代码应该是完整的,然后直接运行...

非常感谢

大卫

来源

import numpy as np 
import matplotlib.pyplot as mpl 

cols, rows = [256,256] # Set size of image 
grey_levels = 256 #Grey levels in image 
testPixels = [[0 for x in range(rows)] for y in range(cols)] # Convert to black and white 
testPixels[100][100] = 255 #Set 3 pixels to white 
testPixels[200][200] = 255 
testPixels[150][150] = 255 

rho_size = int(np.sqrt(rows**2 + cols**2)) #Max possible rho is diagonal dist. 
angle_size = 360 #Test all angles 

houghspace = [[0 for x in range(rho_size)] for y in range(angle_size)] # Create hough space array 

for x in range(rows): # For each rows 
    for y in range(cols): # For each cols 
     if testPixels[x][y] == 0: #Skip if not edge point 
      continue 
     for theta in range(angle_size): 
      rho = int(x*np.cos(np.deg2rad(theta)) + y*np.sin(np.deg2rad(theta))) 
      houghspace[theta][rho] = 255 
houghspace = [list(a) for a in zip(*houghspace)] #Transpose to get angle on x axis 

fig = mpl.figure() # Create a figure 
fig.add_subplot(1, 2, 1).set_title("Original") 
mpl.imshow(np.uint8(np.dstack((testPixels,testPixels,testPixels))),cmap='Greys') 
fig.add_subplot(1, 2, 2).set_title("Hough Transform") 
mpl.imshow(np.uint8(np.dstack((houghspace, houghspace, houghspace))),cmap='Greys') 
mpl.show() 
+0

这能否帮助? https://youtu.be/hYcugbbf9ug?t=1455 – kmario23

+0

这是你期待的答案吗?如果没有,请让我知道,以便我可以改进我的答案。 – kmario23

+1

我还没有测试,但这是我期待的那种解决方案!我会测试,验证和打勾如尽快解决 - 谢谢! – davidhood2

回答

1

你已经混了索引时您创建houghspace作为列表的列表。请使用numpy数组,因为它会使索引更清晰。沿着x轴,角度theta发生变化,沿着y轴rho发生变化。但是,当使用列表理解来定义houghspace时,您已经找到了它。

以下是正确的代码。注意:评论开始##

rho_size = int(np.sqrt(rows**2 + cols**2)) #Max possible rho is diagonal dist. 
angle_size = 360 #Test all angles 

##houghspace = [[0 for y in range(angle_size) for x in range(2*rho_size)]] #buggy 
houghspace = [[0 for x in range(angle_size)] for y in range(rho_size*2)] #correct 
## Also double the rho_size, so that both crust and trough of sinusoidal is visible 

for x in range(rows): # For each rows 
    for y in range(cols): # For each cols 
     if testPixels[x][y] == 0: #Skip if not edge point 
      continue 
     for theta in range(angle_size): 
      rho = int(x*np.cos(np.deg2rad(theta)) + y*np.sin(np.deg2rad(theta))) \ 
                + rho_size ## also add rho_size 
      ##houghspace[theta][rho] = 255 ## buggy 
      houghspace[rho][theta] += 255 # <==== indices switched & it's += 
##houghspace = [list(a) for a in zip(*houghspace)] 
##Transposing not needed now (we switched indices) 

fig = mpl.figure() # Create a figure 
fig.add_subplot(1, 2, 1).set_title("Original") 
mpl.imshow(np.uint8(np.dstack((testPixels,testPixels,testPixels))),cmap='Greys') 
fig.add_subplot(1, 2, 2).set_title("Hough Transform") 
mpl.imshow(np.uint8(np.dstack((houghspace, houghspace, houghspace))),cmap='Greys') 
mpl.show() 

我碰到下面的情节:

enter image description here