Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. Harris corner detection
  2.  
  3.  
  4. Detect corners
  5. ```python
  6.  
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9. import cv2
  10.  
  11. %matplotlib inline
  12.  
  13. # Read in the image
  14. image = cv2.imread('images/waffle.jpg')
  15.  
  16. # Make a copy of the image
  17. image_copy = np.copy(image)
  18.  
  19. # Change color to RGB (from BGR)
  20. image_copy = cv2.cvtColor(image_copy, cv2.COLOR_BGR2RGB)
  21.  
  22. # Convert to grayscale
  23. gray = cv2.cvtColor(image_copy, cv2.COLOR_RGB2GRAY)
  24.  
  25. gray = np.float32(gray)
  26.  
  27. # Detect corners
  28. dst = cv2.cornerHarris(gray, 2, 3, 0.04)
  29.  
  30. # Dilate corner image to enhance corner points
  31. dst = cv2.dilate(dst,None)
  32.  
  33. plt.imshow(dst, cmap='gray')
  34.  
  35.  
  36. ## TODO: Define a threshold for extracting strong corners
  37. # This value vary depending on the image and how many corners you want to detect
  38. # Try changing this free parameter, 0.1, to be larger or smaller ans see what happens
  39. thresh = 0.01*dst.max()
  40.  
  41. # Create an image copy to draw corners on
  42. corner_image = np.copy(image_copy)
  43.  
  44. # Iterate through all the corners and draw them on the image (if they pass the threshold)
  45. for j in range(0, dst.shape[0]):
  46. for i in range(0, dst.shape[1]):
  47. if(dst[j,i] > thresh):
  48. # image, center pt, radius, color, thickness
  49. cv2.circle( corner_image, (i, j), 1, (0,255,0), 1)
  50.  
  51. plt.imshow(corner_image)
  52.  
  53. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement