Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. from numpy import *
  2. from pylab import *
  3. import cv2
  4. from scipy.ndimage import filters
  5.  
  6. def compute_harris_response(im, sigma=3):
  7. imx = zeros(im.shape)
  8. filters.gaussian_filter(im, (sigma, sigma), (0, 1), imx)
  9. imy = zeros(im.shape)
  10. filters.gaussian_filter(im, (sigma, sigma), (1, 0), imy)
  11.  
  12. Wxx = filters.gaussian_filter(imx * imx, sigma)
  13. Wyy = filters.gaussian_filter(imy * imy, sigma)
  14. Wxy = filters.gaussian_filter(imx * imy, sigma)
  15.  
  16. Wdet = Wxx * Wyy - Wxx ** 2
  17. Wtr = Wxx + Wyy
  18.  
  19. return Wdet / Wtr
  20.  
  21. def get_harris_points(harrisim, min_dist=10, threshold=0.1):
  22. corner_threshold = harrisim.max() * threshold
  23. harrisim_t = (harrisim > corner_threshold) * 1
  24.  
  25. coords = array(harrisim_t.nonzero()).T
  26.  
  27. candidate_values = [harrisim[c[0], c[1]] for c in coords]
  28. index = argsort(candidate_values)
  29.  
  30. allowed_locations = zeros(harrisim.shape)
  31. allowed_locations[min_dist:-min_dist,min_dist:-min_dist] = 1
  32.  
  33. filtered_coords = []
  34. for i in index:
  35. if allowed_locations[coords[i, 0], coords[i, 1]] == 1:
  36. filtered_coords.append(coords[i])
  37. allowed_locations[(coords[i,0]-min_dist):(coords[i,0]+min_dist),(coords[i,1]-min_dist):(coords[i,1]+min_dist)] = 0
  38.  
  39. return filtered_coords
  40.  
  41. def plot_harris_points(img, filtered_coords):
  42. figure()
  43. gray()
  44. imshow(img)
  45. plot([p[1] for p in filtered_coords],[p[0] for p in filtered_coords], "*")
  46. axis("off")
  47. show()
  48.  
  49.  
  50. img = cv2.imread("../Images/bookshelf.jpg", cv2.IMREAD_GRAYSCALE)
  51. res = compute_harris_response(img)
  52.  
  53. filtered = get_harris_points(res)
  54. plot_harris_points(res, filtered)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement