@@ -18,28 +18,20 @@ def gen_gaussian_kernel(k_size, sigma):
18
18
return g
19
19
20
20
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 ):
32
22
"""
33
23
Non-maximum suppression. If the edge strength of the current pixel is the largest
34
24
compared to the other pixels in the mask with the same direction, the value will be
35
25
preserved. Otherwise, the value will be suppressed.
36
26
"""
27
+ dst = np .zeros ((image_row , image_col ))
28
+
37
29
for row in range (1 , image_row - 1 ):
38
30
for col in range (1 , image_col - 1 ):
39
31
direction = gradient_direction [row , col ]
40
32
41
33
if (
42
- 0 <= direction < 22.5
34
+ 0 <= direction < PI / 8
43
35
or 15 * PI / 8 <= direction <= 2 * PI
44
36
or 7 * PI / 8 <= direction <= 9 * PI / 8
45
37
):
@@ -48,8 +40,9 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
48
40
if sobel_grad [row , col ] >= w and sobel_grad [row , col ] >= e :
49
41
dst [row , col ] = sobel_grad [row , col ]
50
42
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
53
46
):
54
47
sw = sobel_grad [row + 1 , col - 1 ]
55
48
ne = sobel_grad [row - 1 , col + 1 ]
@@ -72,21 +65,31 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
72
65
if sobel_grad [row , col ] >= nw and sobel_grad [row , col ] >= se :
73
66
dst [row , col ] = sobel_grad [row , col ]
74
67
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 ):
83
84
if dst [row , col ] >= threshold_high :
84
85
dst [row , col ] = strong
85
86
elif dst [row , col ] <= threshold_low :
86
87
dst [row , col ] = 0
87
88
else :
88
89
dst [row , col ] = weak
89
90
91
+
92
+ def track_edge (image_row , image_col , dst , weak , strong ):
90
93
"""
91
94
Edge tracking. Usually a weak edge pixel caused from true edges will be connected
92
95
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):
110
113
else :
111
114
dst [row , col ] = 0
112
115
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
+
113
134
return dst
114
135
115
136
0 commit comments