I am suprised by the fact that I can't seem to find any online implementation of the Hough Transform algorithm as described here. Opencv implements a line detector using the Hough Transform but does not provide any function that performs the transformation into the Hough Space without the line detection after.
So I implemented a naive version myself where seg_map is a numpy array containing 0 and 1:
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
However, it's pretty slow (for the inputs I have, the cv2 functions is 200 times faster for the Hough transform + line detection than my Hough Transform algorithm).
So I actually have two questions :
Did I miss an of-the-shelf python algorithm to perform only the Hough Transform without Line detection ? I know that the OpenCV version of the code is open source, but it's implemented in C++.
Apart from parallelization or non-Python implementation, is there something in my code that is inefficient and should be changed ?