当前位置:   article > 正文

python Canny边缘检测

python canny

 

  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. img = cv2.imread('img1.png',0)
  5. edges = cv2.Canny(img, 100, 200)
  6. plt.subplot(121), plt.imshow(img, cmap='gray')
  7. plt.title('Original Image'), plt.xticks([]), plt.yticks([])
  8. plt.subplot(122), plt.imshow(edges, cmap='gray')
  9. plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
  10. plt.show()

 

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import math
  4. import cv2
  5. img = cv2.imread('img1.png')
  6. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  7. blur = cv2.GaussianBlur(img, (5, 5), 0) # 用高斯滤波处理原图像降噪
  8. canny = cv2.Canny(blur, 50, 150) # 50是最小阈值,150是最大阈值
  9. sigma1 = sigma2 = 1
  10. sum = 0
  11. gaussian = np.zeros([5, 5])
  12. for i in range(5):
  13. for j in range(5):
  14. gaussian[i, j] = math.exp(-1 / 2 * (np.square(i - 3) / np.square(sigma1) # 生成二维高斯分布矩阵
  15. + (np.square(j - 3) / np.square(sigma2)))) / (2 * math.pi * sigma1 * sigma2)
  16. sum = sum + gaussian[i, j]
  17. gaussian = gaussian / sum
  18. # print(gaussian)
  19. def rgb2gray(rgb):
  20. return np.dot(rgb[..., :3], [0.299, 0.587, 0.114])
  21. # step1.高斯滤波
  22. gray = rgb2gray(img)
  23. W, H = gray.shape
  24. new_gray = np.zeros([W - 5, H - 5])
  25. for i in range(W - 5):
  26. for j in range(H - 5):
  27. new_gray[i, j] = np.sum(gray[i:i + 5, j:j + 5] * gaussian) # 与高斯矩阵卷积实现滤波
  28. # plt.imshow(new_gray, cmap="gray")
  29. # step2.增强 通过求梯度幅值
  30. W1, H1 = new_gray.shape
  31. dx = np.zeros([W1 - 1, H1 - 1])
  32. dy = np.zeros([W1 - 1, H1 - 1])
  33. d = np.zeros([W1 - 1, H1 - 1])
  34. for i in range(W1 - 1):
  35. for j in range(H1 - 1):
  36. dx[i, j] = new_gray[i, j + 1] - new_gray[i, j]
  37. dy[i, j] = new_gray[i + 1, j] - new_gray[i, j]
  38. d[i, j] = np.sqrt(np.square(dx[i, j]) + np.square(dy[i, j])) # 图像梯度幅值作为图像强度值
  39. # plt.imshow(d, cmap="gray")
  40. # setp3.非极大值抑制 NMS
  41. W2, H2 = d.shape
  42. NMS = np.copy(d)
  43. NMS[0, :] = NMS[W2 - 1, :] = NMS[:, 0] = NMS[:, H2 - 1] = 0
  44. for i in range(1, W2 - 1):
  45. for j in range(1, H2 - 1):
  46. if d[i, j] == 0:
  47. NMS[i, j] = 0
  48. else:
  49. gradX = dx[i, j]
  50. gradY = dy[i, j]
  51. gradTemp = d[i, j]
  52. # 如果Y方向幅度值较大
  53. if np.abs(gradY) > np.abs(gradX):
  54. weight = np.abs(gradX) / np.abs(gradY)
  55. grad2 = d[i - 1, j]
  56. grad4 = d[i + 1, j]
  57. # 如果x,y方向梯度符号相同
  58. if gradX * gradY > 0:
  59. grad1 = d[i - 1, j - 1]
  60. grad3 = d[i + 1, j + 1]
  61. # 如果x,y方向梯度符号相反
  62. else:
  63. grad1 = d[i - 1, j + 1]
  64. grad3 = d[i + 1, j - 1]
  65. # 如果X方向幅度值较大
  66. else:
  67. weight = np.abs(gradY) / np.abs(gradX)
  68. grad2 = d[i, j - 1]
  69. grad4 = d[i, j + 1]
  70. # 如果x,y方向梯度符号相同
  71. if gradX * gradY > 0:
  72. grad1 = d[i + 1, j - 1]
  73. grad3 = d[i - 1, j + 1]
  74. # 如果x,y方向梯度符号相反
  75. else:
  76. grad1 = d[i - 1, j - 1]
  77. grad3 = d[i + 1, j + 1]
  78. gradTemp1 = weight * grad1 + (1 - weight) * grad2
  79. gradTemp2 = weight * grad3 + (1 - weight) * grad4
  80. if gradTemp >= gradTemp1 and gradTemp >= gradTemp2:
  81. NMS[i, j] = gradTemp
  82. else:
  83. NMS[i, j] = 0
  84. # plt.imshow(NMS, cmap = "gray")
  85. # step4. 双阈值算法检测、连接边缘
  86. W3, H3 = NMS.shape
  87. DT = np.zeros([W3, H3])
  88. # 定义高低阈值
  89. TL = 0.2 * np.max(NMS)
  90. TH = 0.3 * np.max(NMS)
  91. for i in range(1, W3 - 1):
  92. for j in range(1, H3 - 1):
  93. if (NMS[i, j] < TL):
  94. DT[i, j] = 0
  95. elif (NMS[i, j] > TH):
  96. DT[i, j] = 1
  97. elif ((NMS[i - 1, j - 1:j + 1] < TH).any() or (NMS[i + 1, j - 1:j + 1]).any()
  98. or (NMS[i, [j - 1, j + 1]] < TH).any()):
  99. DT[i, j] = 1
  100. plt.figure(1)
  101. # 第一行第一列图形
  102. ax1 = plt.subplot(1, 3, 1)
  103. plt.sca(ax1)
  104. plt.imshow(img)
  105. plt.title("artwork")
  106. # 第一行第二列图形
  107. ax2 = plt.subplot(1, 3, 2)
  108. plt.sca(ax2)
  109. plt.imshow(canny, cmap="gray")
  110. plt.title("opencv Canny")
  111. ax3 = plt.subplot(1, 3, 3)
  112. plt.sca(ax3)
  113. plt.imshow(DT, cmap="gray")
  114. plt.title("my Canny")
  115. plt.show()

参考文献

Canny边缘检测算法(python 实现)_Master_miao的博客-CSDN博客_python canny

使用Pytorch从头实现Canny边缘检测 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/838582
推荐阅读
相关标签
  

闽ICP备14008679号