Advertisement
sajid006

canny0335

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