-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canny Edge Detector.py
241 lines (153 loc) · 6.17 KB
/
Canny Edge Detector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# coding: utf-8
# In[20]:
#环境
import scipy
from scipy import misc
import imageio
from scipy import ndimage
import numpy as np
import matplotlib.pyplot as plt
#导入图像
lion = imageio.imread('/Users/leslie/Desktop/革命成果-学术/LENA.jpg')
#plt.imshow(lion, cmap = plt.get_cmap('gray'))
#plt.show()
# In[23]:
#变成灰度图
lion_gray = np.dot(lion[...,:3], [0.299, 0.587, 0.114])
#lion_gray = lion_gray.astype('int32')
#plt.imshow(lion_gray, cmap = plt.get_cmap('gray'))
#plt.show()
# In[30]:
#高斯模糊,降噪
lion_gray_blurred = ndimage.gaussian_filter(lion_gray, sigma=1.0) # sigma值根据图像变化
#plt.imshow(lion_gray_blurred, cmap = plt.get_cmap('gray'))
#plt.show()
# In[31]:
# 同sobel算子
#sobel算子也可以是其他矩阵,这里选择最大梯度是2的矩阵
def SobelFilter(img, direction):
if(direction == 'x'):
Gx = np.array([[-1,0,+1], [-2,0,+2], [-1,0,+1]])
Res = ndimage.convolve(img, Gx)
if(direction == 'y'):
Gy = np.array([[-1,-2,-1], [0,0,0], [+1,+2,+1]])
Res = ndimage.convolve(img, Gy)
return Res
# In[32]:
# 正则化像素,正则化后像素<=1
def Normalize(img):
# img = np.multiply(img, 255 / np.max(img))
img = img/np.max(img)
return img
# In[33]:
#X轴应用Sobel算子
gx = SobelFilter(lion_gray_blurred, 'x')
gx = Normalize(gx)
#plt.imshow(gx, cmap = plt.get_cmap('gray'))
#plt.show()
# In[34]:
#Y轴应用Sobel算子
gy = SobelFilter(lion_gray_blurred, 'y')
gy = Normalize(gy)
#plt.imshow(gy, cmap = plt.get_cmap('gray'))
#plt.show()
# In[35]:
# 应用SCIPY自带的函数实现Sobel算子,进行验证
#dx = ndimage.sobel(lion_gray_blurred, axis=1, mode='constant', cval=0.0) # horizontal derivative
#dy = ndimage.sobel(lion_gray_blurred, axis=0, mode='constant', cval=0.0) # vertical derivative
dx = ndimage.sobel(lion_gray_blurred, axis=1) # horizontal derivative
dy = ndimage.sobel(lion_gray_blurred, axis=0) # vertical derivative
# In[36]:
# In[37]:
#计算获得的梯度的大小 NB使得每个像素都<=1
Mag = np.hypot(gx,gy)
Mag = Normalize(Mag)
# In[39]:
# 计算梯度方向
Gradient = np.degrees(np.arctan2(gy,gx))
# In[40]:
# In[42]:
#得到边界
def NonMaxSupWithInterpol(Gmag, Grad, Gx, Gy):
NMS = np.zeros(Gmag.shape)
for i in range(1, int(Gmag.shape[0]) - 1):
for j in range(1, int(Gmag.shape[1]) - 1):
if((Grad[i,j] >= 0 and Grad[i,j] <= 45) or (Grad[i,j] < -135 and Grad[i,j] >= -180)):
yBot = np.array([Gmag[i,j+1], Gmag[i+1,j+1]])
yTop = np.array([Gmag[i,j-1], Gmag[i-1,j-1]])
x_est = np.absolute(Gy[i,j]/Gmag[i,j])
if (Gmag[i,j] >= ((yBot[1]-yBot[0])*x_est+yBot[0]) and Gmag[i,j] >= ((yTop[1]-yTop[0])*x_est+yTop[0])):
NMS[i,j] = Gmag[i,j]
else:
NMS[i,j] = 0
if((Grad[i,j] > 45 and Grad[i,j] <= 90) or (Grad[i,j] < -90 and Grad[i,j] >= -135)):
yBot = np.array([Gmag[i+1,j] ,Gmag[i+1,j+1]])
yTop = np.array([Gmag[i-1,j] ,Gmag[i-1,j-1]])
x_est = np.absolute(Gx[i,j]/Gmag[i,j])
if (Gmag[i,j] >= ((yBot[1]-yBot[0])*x_est+yBot[0]) and Gmag[i,j] >= ((yTop[1]-yTop[0])*x_est+yTop[0])):
NMS[i,j] = Gmag[i,j]
else:
NMS[i,j] = 0
if((Grad[i,j] > 90 and Grad[i,j] <= 135) or (Grad[i,j] < -45 and Grad[i,j] >= -90)):
yBot = np.array([Gmag[i+1,j] ,Gmag[i+1,j-1]])
yTop = np.array([Gmag[i-1,j] ,Gmag[i-1,j+1]])
x_est = np.absolute(Gx[i,j]/Gmag[i,j])
if (Gmag[i,j] >= ((yBot[1]-yBot[0])*x_est+yBot[0]) and Gmag[i,j] >= ((yTop[1]-yTop[0])*x_est+yTop[0])):
NMS[i,j] = Gmag[i,j]
else:
NMS[i,j] = 0
if((Grad[i,j] > 135 and Grad[i,j] <= 180) or (Grad[i,j] < 0 and Grad[i,j] >= -45)):
yBot = np.array([Gmag[i,j-1] ,Gmag[i+1,j-1]])
yTop = np.array([Gmag[i,j+1] ,Gmag[i-1,j+1]])
x_est = np.absolute(Gy[i,j]/Gmag[i,j])
if (Gmag[i,j] >= ((yBot[1]-yBot[0])*x_est+yBot[0]) and Gmag[i,j] >= ((yTop[1]-yTop[0])*x_est+yTop[0])):
NMS[i,j] = Gmag[i,j]
else:
NMS[i,j] = 0
return NMS
# 获取非最大抑制输出
NMS = NonMaxSupWithInterpol(Mag, Gradient, gx, gy)
NMS = Normalize(NMS)
# In[45]:
#双阈值
#连接图像1和2
def DoThreshHyst(img):
highThresholdRatio = 0.2
lowThresholdRatio = 0.15
GSup = np.copy(img)
h = int(GSup.shape[0])
w = int(GSup.shape[1])
highThreshold = np.max(GSup) * highThresholdRatio
lowThreshold = highThreshold * lowThresholdRatio
x = 0.1
oldx=0
#使用while循环使循环继续执行,直到强边的数量不变,即连接到强边的所有弱边都被找到。
while(oldx != x):
oldx = x
for i in range(1,h-1):
for j in range(1,w-1):
if(GSup[i,j] > highThreshold):
GSup[i,j] = 1
elif(GSup[i,j] < lowThreshold):
GSup[i,j] = 0
else:
if((GSup[i-1,j-1] > highThreshold) or
(GSup[i-1,j] > highThreshold) or
(GSup[i-1,j+1] > highThreshold) or
(GSup[i,j-1] > highThreshold) or
(GSup[i,j+1] > highThreshold) or
(GSup[i+1,j-1] > highThreshold) or
(GSup[i+1,j] > highThreshold) or
(GSup[i+1,j+1] > highThreshold)):
GSup[i,j] = 1
x = np.sum(GSup == 1)
GSup = (GSup == 1) * GSup # 把在更不严格(图1)阈值图中的,并且没有连接到严格(图2)阈值的图中的边删除,即只保留连接到强边的弱边。
return GSup
# In[ ]:
#Canny 边缘检测的输出
Final_Image = DoThreshHyst(NMS)
plt.imshow(Final_Image, cmap = plt.get_cmap('gray'))
plt.show()
# test = np.arctan2(gx,gy)
# plt.imshow(test, cmap= plt.get_cmap('gray'))
# plt.show()