2017-04-23 100 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