canny.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import cv2
  2. import numpy as np
  3. from digital_image_processing.filters.convolve import img_convolve
  4. from digital_image_processing.filters.sobel_filter import sobel_filter
  5. PI = 180
  6. def gen_gaussian_kernel(k_size, sigma):
  7. center = k_size // 2
  8. x, y = np.mgrid[0 - center : k_size - center, 0 - center : k_size - center]
  9. g = (
  10. 1
  11. / (2 * np.pi * sigma)
  12. * np.exp(-(np.square(x) + np.square(y)) / (2 * np.square(sigma)))
  13. )
  14. return g
  15. def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
  16. image_row, image_col = image.shape[0], image.shape[1]
  17. # gaussian_filter
  18. gaussian_out = img_convolve(image, gen_gaussian_kernel(9, sigma=1.4))
  19. # get the gradient and degree by sobel_filter
  20. sobel_grad, sobel_theta = sobel_filter(gaussian_out)
  21. gradient_direction = np.rad2deg(sobel_theta)
  22. gradient_direction += PI
  23. dst = np.zeros((image_row, image_col))
  24. """
  25. Non-maximum suppression. If the edge strength of the current pixel is the largest compared to the other pixels
  26. in the mask with the same direction, the value will be preserved. Otherwise, the value will be suppressed.
  27. """
  28. for row in range(1, image_row - 1):
  29. for col in range(1, image_col - 1):
  30. direction = gradient_direction[row, col]
  31. if (
  32. 0 <= direction < 22.5
  33. or 15 * PI / 8 <= direction <= 2 * PI
  34. or 7 * PI / 8 <= direction <= 9 * PI / 8
  35. ):
  36. W = sobel_grad[row, col - 1]
  37. E = sobel_grad[row, col + 1]
  38. if sobel_grad[row, col] >= W and sobel_grad[row, col] >= E:
  39. dst[row, col] = sobel_grad[row, col]
  40. elif (PI / 8 <= direction < 3 * PI / 8) or (
  41. 9 * PI / 8 <= direction < 11 * PI / 8
  42. ):
  43. SW = sobel_grad[row + 1, col - 1]
  44. NE = sobel_grad[row - 1, col + 1]
  45. if sobel_grad[row, col] >= SW and sobel_grad[row, col] >= NE:
  46. dst[row, col] = sobel_grad[row, col]
  47. elif (3 * PI / 8 <= direction < 5 * PI / 8) or (
  48. 11 * PI / 8 <= direction < 13 * PI / 8
  49. ):
  50. N = sobel_grad[row - 1, col]
  51. S = sobel_grad[row + 1, col]
  52. if sobel_grad[row, col] >= N and sobel_grad[row, col] >= S:
  53. dst[row, col] = sobel_grad[row, col]
  54. elif (5 * PI / 8 <= direction < 7 * PI / 8) or (
  55. 13 * PI / 8 <= direction < 15 * PI / 8
  56. ):
  57. NW = sobel_grad[row - 1, col - 1]
  58. SE = sobel_grad[row + 1, col + 1]
  59. if sobel_grad[row, col] >= NW and sobel_grad[row, col] >= SE:
  60. dst[row, col] = sobel_grad[row, col]
  61. """
  62. High-Low threshold detection. If an edge pixel’s gradient value is higher than the high threshold
  63. value, it is marked as a strong edge pixel. If an edge pixel’s gradient value is smaller than the high
  64. threshold value and larger than the low threshold value, it is marked as a weak edge pixel. If an edge
  65. pixel's value is smaller than the low threshold value, it will be suppressed.
  66. """
  67. if dst[row, col] >= threshold_high:
  68. dst[row, col] = strong
  69. elif dst[row, col] <= threshold_low:
  70. dst[row, col] = 0
  71. else:
  72. dst[row, col] = weak
  73. """
  74. Edge tracking. Usually a weak edge pixel caused from true edges will be connected to a strong edge pixel while
  75. noise responses are unconnected. As long as there is one strong edge pixel that is involved in its 8-connected
  76. neighborhood, that weak edge point can be identified as one that should be preserved.
  77. """
  78. for row in range(1, image_row):
  79. for col in range(1, image_col):
  80. if dst[row, col] == weak:
  81. if 255 in (
  82. dst[row, col + 1],
  83. dst[row, col - 1],
  84. dst[row - 1, col],
  85. dst[row + 1, col],
  86. dst[row - 1, col - 1],
  87. dst[row + 1, col - 1],
  88. dst[row - 1, col + 1],
  89. dst[row + 1, col + 1],
  90. ):
  91. dst[row, col] = strong
  92. else:
  93. dst[row, col] = 0
  94. return dst
  95. if __name__ == "__main__":
  96. # read original image in gray mode
  97. lena = cv2.imread(r"../image_data/lena.jpg", 0)
  98. # canny edge detection
  99. canny_dst = canny(lena)
  100. cv2.imshow("canny", canny_dst)
  101. cv2.waitKey(0)