-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canny.py
224 lines (179 loc) · 7.16 KB
/
Canny.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
import numpy as np
import math
import matplotlib.pyplot as plt
import cv2
import sys
def filter_size_and_gradient(sigma,T):
if sigma < 0.5:
sigma = 0.5
if T > 1 and T < 0:
T = 0.1
shalf = round(math.sqrt(-math.log(T) * 2 * sigma ** 2))
N = 2 * shalf + 1 # N is total mask size
print(f"Mask Size: {N}")
map = np.linspace(-shalf,shalf, N)
[Y, X] = np.meshgrid (map, map)
print(f"Horizontal Mask:\n\n{X}")
print(f"\nVertical Mask:\n\n{Y}")
gx = []
gy = []
gx = Gaus_x(X,Y,sigma)
print(f"\nGx:\n\n{gx}")
gy = Gaus_y(X,Y,sigma)
print(f"\nGy:\n\n{gy}")
return N,gx, gy
def Gaus_x(X,Y,sigma):
Gx = []
scale = 255
for i in range(0, len(X)):
row = []
for j in range(0, len(X[0])):
e = math.exp((-X[i][j]**2 -Y[i][j]**2)/2*sigma**2)
deriv=((-X[i][j]/sigma**2)*e)
row.append(round(scale * deriv)) # for scaling
Gx.append(row)
return Gx
def Gaus_y(X,Y,sigma):
Gy = []
scale = 255
for i in range(0, len(X)):
row = []
for j in range(0, len(X[0])):
e = math.exp((-X[i][j]**2 -Y[i][j]**2)/2*sigma**2)
deriv=((-Y[i][j]/sigma**2)*e)
row.append(round(scale * deriv)) # for scaling
Gy.append(row)
return Gy
def conv(N,image,g):
g = np.flipud(g) # flipped due to no maxima supression
image_columns = image.shape[1]
image_rows = image.shape[0]
output_rows = image_rows - N + 1
output_columns = image_columns - N + 1
output = np.zeros((output_rows, output_columns))
for i in range(output_rows):
for j in range(output_columns):
output[i, j] = np.sum(g * image[i: i + N, j: j + N])
return output
def magnitude(fx,fy):
magnew = []
scale = 255
for i in range(0, len(fx)+2): #zero padding - this is enough for mag supressed also
row = []
for j in range(0, len(fy[1])+2):
row.append(0)
magnew.append(row)
for i in range(0, len(fx)):
row = []
for j in range(0, len(fy[1])):
mag = math.sqrt((fx[i][j]**2)+(fy[i][j]**2))
magnew[i+1][j+1] = (mag/scale) # for scaling
return magnew
def non_maxima_suppression(fx, fy, mag_suppressed):
mag = mag_suppressed
rows = len(fx)
columns = len(fy[1])
for i in range(1, len(fx)):
for j in range(1, len(fy[1])):
angle = np.arctan2(fy[i][j], fx[i][j])
angle = np.rad2deg(angle)
if angle < 0:
angle = angle + 360
if ((angle > 0 and angle < 22.5) or (angle > 157.5 and angle < 202.5) or (angle > 337.5 and angle < 360)):
if (mag[i][j] > mag[i][j+1]) or (mag[i][j] > mag[i][j-1]):
mag_suppressed[i][j] = mag_suppressed[i][j]
else:
mag_suppressed[i][j] = 0
if ((angle > 22.5 and angle < 67.5) or (angle > 202.5 and angle < 247.5)):
if (mag[i][j] > mag[i+1][j+1]) or (mag[i][j] > mag[i-1][j-1]):
mag_suppressed[i][j] = mag_suppressed[i][j]
else:
mag_suppressed[i][j] = 0
if ((angle > 67.5 and angle < 112.5) or (angle > 247.5 and angle < 292.5)):
if (mag[i][j] > mag[i+1][j]) or (mag[i][j] > mag[i-1][j]):
mag_suppressed[i][j] = mag_suppressed[i][j]
else:
mag_suppressed[i][j] = 0
if ((angle > 112.5 and angle < 157.5) or (angle > 292.5 and angle < 337.5)):
if (mag[i][j] > mag[i+1][j-1]) or (mag[i][j] > mag[i-1][j+1]):
mag_suppressed[i][j] = mag_suppressed[i][j]
else:
mag_suppressed[i][j] = 0
return mag_suppressed
def hysteresis(th, tl, mag_suppressed):
th = int(th)
tl = int(tl)
ced = np.zeros((len(mag_suppressed),len(mag_suppressed[1]),3))
for i in range(len(mag_suppressed)):
for j in range(len(mag_suppressed[1])-1):
if mag_suppressed[i][j] > th:
ced[i][j][0] = 1
ced[i][j][1] = 1
ced[i][j][2] = 1
while True:
done = 1
for i in range(len(mag_suppressed)):
for j in range(len(mag_suppressed[1])-1):
if (ced[i][j][0] == 1):
if (mag_suppressed[i+1][j] > tl and ced[i+1][j][0] == 0):
ced[i+1][j][0] = 1
ced[i+1][j][1] = 1
ced[i+1][j][2] = 1
done = 0
if (mag_suppressed[i-1][j] > tl and ced[i-1][j][0] == 0):
ced[i-1][j][0] = 1
ced[i-1][j][1] = 1
ced[i-1][j][2] = 1
done = 0
if (mag_suppressed[i][j+1] > tl and ced[i][j+1][0] == 0):
ced[i][j+1][0] = 1
ced[i][j+1][1] = 1
ced[i][j+1][2] = 1
done = 0
if (mag_suppressed[i][j-1] > tl and ced[i][j-1][0] == 0):
ced[i][j-1][0] = 1
ced[i][j-1][1] = 1
ced[i][j-1][2] = 1
done = 0
if (mag_suppressed[i-1][j-1] > tl and ced[i-1][j-1][0] == 0):
ced[i-1][j-1][0] = 1
ced[i-1][j-1][1] = 1
ced[i-1][j-1][2] = 1
done = 0
if (mag_suppressed[i+1][j+1] > tl and ced[i+1][j+1][0] == 0):
ced[i+1][j+1][0] = 1
ced[i+1][j+1][1] = 1
ced[i+1][j+1][2] = 1
done = 0
if (mag_suppressed[i+1][j-1] > tl and ced[i+1][j-1][0] == 0):
ced[i+1][j-1][0] = 1
ced[i+1][j-1][1] = 1
ced[i+1][j-1][2] = 1
done = 0
if (mag_suppressed[i-1][j+1] > tl and ced[i-1][j+1][0] == 0):
ced[i-1][j+1][0] = 1
ced[i-1][j+1][1] = 1
ced[i-1][j+1][2] = 1
done = 0
if done == 1:
break
return ced
def canny(img):
sigma = float(input("sigma: "))
T = float(input("T: "))
N, gx, gy = filter_size_and_gradient(sigma,T)
np.shape(gy)
fx = conv(N, img, gx)
fy = conv(N, img, gy)
mag = magnitude(fx,fy)
mag_suppressed = non_maxima_suppression(fx, fy, mag)
max_val = np.max(mag_suppressed)
th = input(f"input th value when max value is {max_val} : ")
tl = input(f"input tl value when max value is {max_val} : ")
ced = hysteresis(th, tl, mag_suppressed)
plt.imshow(ced)
return ced
target_file = input("type in your target file name with extension: ")
img = cv2.imread(target_file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edge = canny(img)