@@ -18,16 +18,16 @@ def gen_gaussian_kernel(k_size, sigma):
18
18
return g
19
19
20
20
21
- def suppress_non_maximum (image_row , image_col , gradient_direction , sobel_grad ):
21
+ def suppress_non_maximum (image_shape , gradient_direction , sobel_grad ):
22
22
"""
23
23
Non-maximum suppression. If the edge strength of the current pixel is the largest
24
24
compared to the other pixels in the mask with the same direction, the value will be
25
25
preserved. Otherwise, the value will be suppressed.
26
26
"""
27
- dst = np .zeros (( image_row , image_col ) )
27
+ dst = np .zeros (image_shape )
28
28
29
- for row in range (1 , image_row - 1 ):
30
- for col in range (1 , image_col - 1 ):
29
+ for row in range (1 , image_shape [ 0 ] - 1 ):
30
+ for col in range (1 , image_shape [ 1 ] - 1 ):
31
31
direction = gradient_direction [row , col ]
32
32
33
33
if (
@@ -71,7 +71,7 @@ def suppress_non_maximum(image_row, image_col, gradient_direction, sobel_grad):
71
71
72
72
73
73
def detect_high_low_threshold (
74
- image_row , image_col , dst , threshold_low , threshold_high , weak , strong
74
+ image_shape , dst , threshold_low , threshold_high , weak , strong
75
75
):
76
76
"""
77
77
High-Low threshold detection. If an edge pixel’s gradient value is higher
@@ -81,8 +81,8 @@ def detect_high_low_threshold(
81
81
an edge pixel's value is smaller than the low threshold value, it will be
82
82
suppressed.
83
83
"""
84
- for row in range (1 , image_row - 1 ):
85
- for col in range (1 , image_col - 1 ):
84
+ for row in range (1 , image_shape [ 0 ] - 1 ):
85
+ for col in range (1 , image_shape [ 1 ] - 1 ):
86
86
if dst [row , col ] >= threshold_high :
87
87
dst [row , col ] = strong
88
88
elif dst [row , col ] <= threshold_low :
@@ -91,15 +91,15 @@ def detect_high_low_threshold(
91
91
dst [row , col ] = weak
92
92
93
93
94
- def track_edge (image_row , image_col , dst , weak , strong ):
94
+ def track_edge (image_shape , dst , weak , strong ):
95
95
"""
96
96
Edge tracking. Usually a weak edge pixel caused from true edges will be connected
97
97
to a strong edge pixel while noise responses are unconnected. As long as there is
98
98
one strong edge pixel that is involved in its 8-connected neighborhood, that weak
99
99
edge point can be identified as one that should be preserved.
100
100
"""
101
- for row in range (1 , image_row ):
102
- for col in range (1 , image_col ):
101
+ for row in range (1 , image_shape [ 0 ] ):
102
+ for col in range (1 , image_shape [ 1 ] ):
103
103
if dst [row , col ] == weak :
104
104
if 255 in (
105
105
dst [row , col + 1 ],
@@ -117,21 +117,19 @@ def track_edge(image_row, image_col, dst, weak, strong):
117
117
118
118
119
119
def canny (image , threshold_low = 15 , threshold_high = 30 , weak = 128 , strong = 255 ):
120
- image_row , image_col = image .shape [0 ], image .shape [1 ]
121
120
# gaussian_filter
122
121
gaussian_out = img_convolve (image , gen_gaussian_kernel (9 , sigma = 1.4 ))
123
122
# get the gradient and degree by sobel_filter
124
123
sobel_grad , sobel_theta = sobel_filter (gaussian_out )
125
- gradient_direction = np .rad2deg (sobel_theta )
126
- gradient_direction += PI
124
+ gradient_direction = PI + np .rad2deg (sobel_theta )
127
125
128
- dst = suppress_non_maximum (image_row , image_col , gradient_direction , sobel_grad )
126
+ dst = suppress_non_maximum (image . shape , gradient_direction , sobel_grad )
129
127
130
128
detect_high_low_threshold (
131
- image_row , image_col , dst , threshold_low , threshold_high , weak , strong
129
+ image . shape , dst , threshold_low , threshold_high , weak , strong
132
130
)
133
131
134
- track_edge (image_row , image_col , dst , weak , strong )
132
+ track_edge (image . shape , dst , weak , strong )
135
133
136
134
return dst
137
135
0 commit comments