Skip to content

Commit 7414096

Browse files
committed
Reduce the complexity of digital_image_processing/edge_detection/canny.py
1 parent 978414b commit 7414096

File tree

1 file changed

+43
-22
lines changed
  • digital_image_processing/edge_detection

1 file changed

+43
-22
lines changed

digital_image_processing/edge_detection/canny.py

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,20 @@ def gen_gaussian_kernel(k_size, sigma):
1818
return g
1919

2020

21-
def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
22-
image_row, image_col = image.shape[0], image.shape[1]
23-
# gaussian_filter
24-
gaussian_out = img_convolve(image, gen_gaussian_kernel(9, sigma=1.4))
25-
# get the gradient and degree by sobel_filter
26-
sobel_grad, sobel_theta = sobel_filter(gaussian_out)
27-
gradient_direction = np.rad2deg(sobel_theta)
28-
gradient_direction += PI
29-
30-
dst = np.zeros((image_row, image_col))
31-
21+
def suppress_non_maximum(image_row, image_col, gradient_direction, sobel_grad):
3222
"""
3323
Non-maximum suppression. If the edge strength of the current pixel is the largest
3424
compared to the other pixels in the mask with the same direction, the value will be
3525
preserved. Otherwise, the value will be suppressed.
3626
"""
27+
dst = np.zeros((image_row, image_col))
28+
3729
for row in range(1, image_row - 1):
3830
for col in range(1, image_col - 1):
3931
direction = gradient_direction[row, col]
4032

4133
if (
42-
0 <= direction < 22.5
34+
0 <= direction < PI / 8
4335
or 15 * PI / 8 <= direction <= 2 * PI
4436
or 7 * PI / 8 <= direction <= 9 * PI / 8
4537
):
@@ -48,8 +40,9 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
4840
if sobel_grad[row, col] >= w and sobel_grad[row, col] >= e:
4941
dst[row, col] = sobel_grad[row, col]
5042

51-
elif (PI / 8 <= direction < 3 * PI / 8) or (
52-
9 * PI / 8 <= direction < 11 * PI / 8
43+
elif (
44+
PI / 8 <= direction < 3 * PI / 8
45+
or 9 * PI / 8 <= direction < 11 * PI / 8
5346
):
5447
sw = sobel_grad[row + 1, col - 1]
5548
ne = sobel_grad[row - 1, col + 1]
@@ -72,21 +65,31 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
7265
if sobel_grad[row, col] >= nw and sobel_grad[row, col] >= se:
7366
dst[row, col] = sobel_grad[row, col]
7467

75-
"""
76-
High-Low threshold detection. If an edge pixel’s gradient value is higher
77-
than the high threshold value, it is marked as a strong edge pixel. If an
78-
edge pixel’s gradient value is smaller than the high threshold value and
79-
larger than the low threshold value, it is marked as a weak edge pixel. If
80-
an edge pixel's value is smaller than the low threshold value, it will be
81-
suppressed.
82-
"""
68+
return dst
69+
70+
71+
def detect_high_low_threshold(
72+
image_row, image_col, dst, threshold_low, threshold_high, weak, strong
73+
):
74+
"""
75+
High-Low threshold detection. If an edge pixel’s gradient value is higher
76+
than the high threshold value, it is marked as a strong edge pixel. If an
77+
edge pixel’s gradient value is smaller than the high threshold value and
78+
larger than the low threshold value, it is marked as a weak edge pixel. If
79+
an edge pixel's value is smaller than the low threshold value, it will be
80+
suppressed.
81+
"""
82+
for row in range(1, image_row - 1):
83+
for col in range(1, image_col - 1):
8384
if dst[row, col] >= threshold_high:
8485
dst[row, col] = strong
8586
elif dst[row, col] <= threshold_low:
8687
dst[row, col] = 0
8788
else:
8889
dst[row, col] = weak
8990

91+
92+
def track_edge(image_row, image_col, dst, weak, strong):
9093
"""
9194
Edge tracking. Usually a weak edge pixel caused from true edges will be connected
9295
to a strong edge pixel while noise responses are unconnected. As long as there is
@@ -110,6 +113,24 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
110113
else:
111114
dst[row, col] = 0
112115

116+
117+
def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
118+
image_row, image_col = image.shape[0], image.shape[1]
119+
# gaussian_filter
120+
gaussian_out = img_convolve(image, gen_gaussian_kernel(9, sigma=1.4))
121+
# get the gradient and degree by sobel_filter
122+
sobel_grad, sobel_theta = sobel_filter(gaussian_out)
123+
gradient_direction = np.rad2deg(sobel_theta)
124+
gradient_direction += PI
125+
126+
dst = suppress_non_maximum(image_row, image_col, gradient_direction, sobel_grad)
127+
128+
detect_high_low_threshold(
129+
image_row, image_col, dst, threshold_low, threshold_high, weak, strong
130+
)
131+
132+
track_edge(image_row, image_col, dst, weak, strong)
133+
113134
return dst
114135

115136

0 commit comments

Comments
 (0)