2
我正在Python中編寫一個基本的霍夫變換 - 我相信我已經在概念上正確的了,但是,我的結果似乎被抵消了,因此它被分割成頂部和底部,而不是連續的。應該是什麼我想是這樣的:Hough Transform in Python - 結果不正確的偏移 - 索引錯誤?
但我得到這個:
這是接近的,但似乎是通過中間四分五裂!我相信這是由於我對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()
這能否幫助? https://youtu.be/hYcugbbf9ug?t=1455 – kmario23
這是你期待的答案嗎?如果沒有,請讓我知道,以便我可以改進我的答案。 – kmario23
我還沒有測試,但這是我期待的那種解決方案!我會測試,驗證和打勾如儘快解決 - 謝謝! – davidhood2