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