Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. def hough_line(img):
  2. # Rho and Theta ranges
  3. thetas = np.deg2rad(np.arange(-90.0, 90.0))
  4. width, height = img.shape
  5. diag_len = int(np.sqrt(width**2 + height**2)) # max_dist
  6. rhos = np.linspace(-diag_len, diag_len, int(diag_len * 2.0))
  7.  
  8. # Cache some reusable values
  9. cos_t = np.cos(thetas)
  10. sin_t = np.sin(thetas)
  11. num_thetas = len(thetas)
  12.  
  13. # Hough accumulator array of theta vs rho
  14. accumulator = np.zeros((2*diag_len, num_thetas))
  15. y_idxs, x_idxs = np.nonzero(img) # (row, col) indexes to edges
  16.  
  17. # Vote in the hough accumulator
  18. for i in range(len(x_idxs)):
  19. x = x_idxs[i]
  20. y = y_idxs[i]
  21.  
  22. for t_idx in range(num_thetas):
  23. # Calculate rho. diag_len is added for a positive index
  24. rho = int(round(x*cos_t[t_idx] + y * sin_t[t_idx]) + diag_len)
  25. accumulator[rho, t_idx] += 1
  26.  
  27. return accumulator, thetas, rhos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement