我似乎找不到here所述的Hough变换算法的任何在线实现,这令我感到惊讶。 Opencv使用霍夫变换实现了行检测器,但是不提供任何无需执行行检测即可执行到霍夫空间的转换的功能。
所以我自己实现了一个简单的版本,其中seg_map是一个包含0
和1
的numpy数组:
def hough_transform(seg_map):
rho_step = 5
phi_step = 20 * np.pi/180
n_points_min = 2*rho_step
diag_length = np.sqrt(seg_map.shape[0]**2 + seg_map.shape[1]**2)
hough_map = np.zeros( (int(2*np.pi/phi_step),int(diag_length/rho_step)) )
Y, X = np.where(seg_map>0)
for x, y in zip(X, Y):
theta = np.arctan2(y, x)
r = np.sqrt(x**2+y**2)
for i in range(hough_map.shape[0]):
phi = i * phi_step - np.pi/2
rho = r * np.cos(theta-phi)
if rho>0:
rho_discretized = int(rho/rho_step)
hough_map[i, rho_discretized] += 1
return hough_map
但是,它非常慢(对于我的输入,霍夫变换+行检测的cv2函数比我的霍夫变换算法快200倍。]
所以我实际上有两个问题:
我是否错过了现成的[[python算法,该算法仅执行霍夫变换而没有行检测?我知道该代码的OpenCV版本是开源的,但是它是用C ++实现的。