Advertisement
sajid006

CannyEdgeDetection

Nov 30th, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Nov 30 12:45:12 2021
  4.  
  5. @author: Sajid
  6. """
  7. import numpy as np
  8. from scipy import ndimage
  9. import cv2
  10.  
  11. def gaussian_kernel(size, sigma=1):
  12. size = int(size) // 2
  13. x, y = np.mgrid[-size:size+1, -size:size+1]
  14. normal = 1 / (2.0 * np.pi * sigma**2)
  15. g = np.exp(-((x**2 + y**2) / (2.0*sigma**2))) * normal
  16. return g
  17.  
  18.  
  19.  
  20. def sobel_filters(img):
  21. Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float32)
  22. Ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float32)
  23.  
  24. Ix = cv2.filter2D(img, -1, Kx)
  25. Ix = cv2.convertScaleAbs(Ix)
  26. Iy = cv2.filter2D(img, -1, Ky)
  27. Iy = cv2.convertScaleAbs(Iy)
  28.  
  29. sobel_img = np.hypot(Ix, Iy).astype(np.uint8)
  30. sobel_img = cv2.convertScaleAbs(sobel_img)
  31. theta = np.arctan2(Iy, Ix)
  32.  
  33. return sobel_img,theta
  34.  
  35. def non_max_suppression(img, D):
  36. M, N = img.shape
  37. Z = np.zeros((M,N), dtype=np.int32)
  38. angle = D * 180. / np.pi
  39. angle[angle < 0] += 180
  40.  
  41.  
  42. for i in range(1,M-1):
  43. for j in range(1,N-1):
  44. try:
  45. q = 255
  46. r = 255
  47.  
  48. #angle 0
  49. if (0 <= angle[i,j] < 22.5) or (157.5 <= angle[i,j] <= 180):
  50. q = img[i, j+1]
  51. r = img[i, j-1]
  52. #angle 45
  53. elif (22.5 <= angle[i,j] < 67.5):
  54. q = img[i+1, j-1]
  55. r = img[i-1, j+1]
  56. #angle 90
  57. elif (67.5 <= angle[i,j] < 112.5):
  58. q = img[i+1, j]
  59. r = img[i-1, j]
  60. #angle 135
  61. elif (112.5 <= angle[i,j] < 157.5):
  62. q = img[i-1, j-1]
  63. r = img[i+1, j+1]
  64.  
  65. if (img[i,j] >= q) and (img[i,j] >= r):
  66. Z[i,j] = img[i,j]
  67. else:
  68. Z[i,j] = 0
  69.  
  70. except IndexError as e:
  71. pass
  72.  
  73. return Z
  74.  
  75. def threshold(img, lowThresholdRatio=0.05, highThresholdRatio=0.09):
  76.  
  77. highThreshold = img.max() * highThresholdRatio;
  78. lowThreshold = highThreshold * lowThresholdRatio;
  79.  
  80. M, N = img.shape
  81. res = np.zeros((M,N), dtype=np.int32)
  82.  
  83. weak = np.int32(25)
  84. strong = np.int32(255)
  85.  
  86. strong_i, strong_j = np.where(img >= highThreshold)
  87. zeros_i, zeros_j = np.where(img < lowThreshold)
  88.  
  89. weak_i, weak_j = np.where((img <= highThreshold) & (img >= lowThreshold))
  90.  
  91. res[strong_i, strong_j] = strong
  92. res[weak_i, weak_j] = weak
  93.  
  94. return (res, weak, strong)
  95.  
  96. def hysteresis(img, weak, strong=255):
  97. M, N = img.shape
  98. for i in range(1, M-1):
  99. for j in range(1, N-1):
  100. if (img[i,j] == weak):
  101. try:
  102. if ((img[i+1, j-1] == strong) or (img[i+1, j] == strong) or (img[i+1, j+1] == strong)
  103. or (img[i, j-1] == strong) or (img[i, j+1] == strong)
  104. or (img[i-1, j-1] == strong) or (img[i-1, j] == strong) or (img[i-1, j+1] == strong)):
  105. img[i, j] = strong
  106. else:
  107. img[i, j] = 0
  108. except IndexError as e:
  109. pass
  110. return img
  111.  
  112. img = cv2.imread('can.jpg',cv2.IMREAD_GRAYSCALE)
  113. cv2.imshow('Input Image', img)
  114.  
  115.  
  116.  
  117. kernel = gaussian_kernel(5)
  118. smoothed_image = cv2.filter2D(img, -1, kernel)
  119. smoothed_image = cv2.convertScaleAbs(smoothed_image)
  120. cv2.imshow('Noiseless Image',smoothed_image)
  121.  
  122.  
  123. sobel_img,theta = sobel_filters(smoothed_image)
  124. cv2.imshow('Sobel Filtered',sobel_img)
  125.  
  126.  
  127. noMaximg = non_max_suppression(sobel_img, theta)
  128. noMaximg = cv2.convertScaleAbs(noMaximg)
  129. cv2.imshow('Non Maximum Suppression',noMaximg)
  130.  
  131.  
  132. thresimg,w,s = threshold(noMaximg)
  133. thresimg = cv2.convertScaleAbs(thresimg)
  134. cv2.imshow('Double thresholding', thresimg)
  135.  
  136.  
  137. output = hysteresis(thresimg, w, s)
  138.  
  139. cv2.imshow('Final Output',output)
  140.  
  141. cv2.waitKey(0)
  142. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement