-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedges.py
265 lines (197 loc) · 6.98 KB
/
edges.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import numpy as np
def conv(image, kernel):
"""
Args:
image: numpy array of shape (Hi, Wi).
kernel: numpy array of shape (Hk, Wk).
Returns:
out: numpy array of shape (Hi, Wi).
"""
Hi, Wi = image.shape
Hk, Wk = kernel.shape
out = np.zeros((Hi, Wi))
pad_width0 = Hk // 2
pad_width1 = Wk // 2
pad_width = ((pad_width0,pad_width0),(pad_width1,pad_width1))
image = np.pad(image, pad_width, mode='edge')
new_height, new_width = image.shape
kernel = np.flip(kernel)
kernel_width = Wk // 2
kernel_height = Hk // 2
for x in range(kernel_height, new_height - kernel_height):
for y in range(kernel_width, new_width - kernel_width):
neighbourhood = image[x - kernel_height : x + kernel_height + 1, y - kernel_width : y + kernel_width + 1]
out[x - kernel_height, y - kernel_width] = np.sum(np.multiply(neighbourhood, kernel))
return out
def gaussian_kernel(size, sigma):
"""
Args:
size: int of the size of output matrix.
sigma: float of sigma to calculate kernel.
Returns:
kernel: numpy array of shape (size, size).
"""
kernel = np.zeros((size, size))
k = size // 2
sq_sig = np.square(sigma)
for i in range(size):
for j in range(size):
kernel[i, j] = np.exp(-(np.square(i - k) + np.square(j - k)) / (2 * sq_sig)) / (2 * np.pi * sq_sig)
return kernel
def partial_x(img):
""" Computes partial x-derivative of input img.
Args:
img: numpy array of shape (H, W).
Returns:
out: x-derivative image.
"""
out = None
x_filter = np.array([[0.5, 0, -0.5]])
out = conv(img, x_filter)
return out
def partial_y(img):
""" Computes partial y-derivative of input img.
Args:
img: numpy array of shape (H, W).
Returns:
out: y-derivative image.
"""
y_filter = np.array([[0.5], [0], [-0.5]])
out = conv(img, y_filter)
return out
def gradient(img):
"""
Args:
img: Grayscale image. Numpy array of shape (H, W).
Returns:
G: Magnitude of gradient at each pixel in img.
Numpy array of shape (H, W).
theta: Direction(in degrees, 0 <= theta < 360) of gradient
at each pixel in img. Numpy array of shape (H, W).
"""
G = np.zeros(img.shape)
theta = np.zeros(img.shape)
filtered_x = partial_x(img)
filtered_y = partial_y(img)
G = np.sqrt(filtered_x ** 2 + filtered_y ** 2)
theta = (np.rad2deg(np.arctan2(filtered_y, filtered_x)) + 180) % 360
# Angles are needed clockwise.
# Phase of tan is 180
return G, theta
def non_maximum_suppression(G, theta):
"""
Args:
G: gradient magnitude image with shape of (H, W).
theta: direction of gradients with shape of (H, W).
Returns:
out: non-maxima suppressed image.
"""
H, W = G.shape
out = np.zeros((H, W))
# Round the gradient direction to the nearest 45 degrees
theta = np.floor((theta + 22.5) / 45) * 45
#print(G)
for i in range(H):
for j in range(W):
direction = theta[i, j]
pixel = G[i, j]
neighbor1 = -1
neighbor2 = -1
if direction == 45 or direction == 225:
if i != H - 1 and j != W - 1:
neighbor1 = G[i + 1, j + 1]
if i != 0 and j != 0:
neighbor2 = G[i - 1, j - 1]
elif direction == 90 or direction == 270:
if i != H - 1:
neighbor1 = G[i + 1, j]
if i != 0:
neighbor2 = G[i - 1, j]
elif direction == 135 or direction == 315:
if i != 0 and j != W - 1:
neighbor1 = G[i - 1, j + 1]
if i != H - 1 and j != 0:
neighbor2 = G[i + 1, j - 1]
elif direction == 180 or direction == 360 or direction == 0:
if j != W - 1:
neighbor1 = G[i, j + 1]
if j != 0:
neighbor2 = G[i, j - 1]
if G[i, j] >= neighbor1 and G[i, j] >= neighbor2:
out[i, j] = G[i, j]
else:
out[i, j] = 0
return out
def double_thresholding(img, high, low):
"""
Args:
img: numpy array of shape (H, W) representing NMS edge response.
high: high threshold(float) for strong edges.
low: low threshold(float) for weak edges.
Returns:
strong_edges: Boolean array representing strong edges.
Strong edeges are the pixels with the values greater than
the higher threshold.
weak_edges: Boolean array representing weak edges.
Weak edges are the pixels with the values smaller or equal to the
higher threshold and greater than the lower threshold.
"""
strong_edges = np.zeros(img.shape, dtype=bool)
weak_edges = np.zeros(img.shape, dtype=bool)
strong_edges = (img >= high)
weak_edges = (img < high) & (img >= low)
return strong_edges, weak_edges
def get_neighbors(y, x, H, W):
""" Return indices of valid neighbors of (y, x).
Return indices of all the valid neighbors of (y, x) in an array of
shape (H, W). An index (i, j) of a valid neighbor should satisfy
the following:
1. i >= 0 and i < H
2. j >= 0 and j < W
3. (i, j) != (y, x)
Args:
y, x: location of the pixel.
H, W: size of the image.
Returns:
neighbors: list of indices of neighboring pixels [(i, j)].
"""
neighbors = []
for i in (y-1, y, y+1):
for j in (x-1, x, x+1):
if i >= 0 and i < H and j >= 0 and j < W:
if (i == y and j == x):
continue
neighbors.append((i, j))
return neighbors
def link_edges(strong_edges, weak_edges):
""" Find weak edges connected to strong edges and link them.
Args:
strong_edges: binary image of shape (H, W).
weak_edges: binary image of shape (H, W).
Returns:
edges: numpy boolean array of shape(H, W).
"""
H, W = strong_edges.shape
indices = np.stack(np.nonzero(strong_edges)).T
edges = np.zeros((H, W), dtype=bool)
# Make new instances of arguments to leave the original
# references intact
weak_edges = np.copy(weak_edges)
edges = np.copy(strong_edges)
seen = set()
queue = []
for i in range(H):
for j in range(W):
if edges[i, j]:
queue.append((i, j))
while queue != []:
coord = queue.pop(0)
if coord in seen:
continue
i, j = coord
for n in get_neighbors(i, j, H, W):
if weak_edges[n[0], n[1]]:
edges[n[0], n[1]] = True
queue.append(n)
seen.add(coord)
return edges