@@ -18,105 +18,126 @@ 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_shape , 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
"""
37
- for row in range (1 , image_row - 1 ):
38
- for col in range (1 , image_col - 1 ):
27
+ destination = np .zeros (image_shape )
28
+
29
+ for row in range (1 , image_shape [0 ] - 1 ):
30
+ for col in range (1 , image_shape [1 ] - 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
):
46
38
w = sobel_grad [row , col - 1 ]
47
39
e = sobel_grad [row , col + 1 ]
48
40
if sobel_grad [row , col ] >= w and sobel_grad [row , col ] >= e :
49
- dst [row , col ] = sobel_grad [row , col ]
41
+ destination [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 ]
56
49
if sobel_grad [row , col ] >= sw and sobel_grad [row , col ] >= ne :
57
- dst [row , col ] = sobel_grad [row , col ]
50
+ destination [row , col ] = sobel_grad [row , col ]
58
51
59
- elif (3 * PI / 8 <= direction < 5 * PI / 8 ) or (
60
- 11 * PI / 8 <= direction < 13 * PI / 8
52
+ elif (
53
+ 3 * PI / 8 <= direction < 5 * PI / 8
54
+ or 11 * PI / 8 <= direction < 13 * PI / 8
61
55
):
62
56
n = sobel_grad [row - 1 , col ]
63
57
s = sobel_grad [row + 1 , col ]
64
58
if sobel_grad [row , col ] >= n and sobel_grad [row , col ] >= s :
65
- dst [row , col ] = sobel_grad [row , col ]
59
+ destination [row , col ] = sobel_grad [row , col ]
66
60
67
- elif (5 * PI / 8 <= direction < 7 * PI / 8 ) or (
68
- 13 * PI / 8 <= direction < 15 * PI / 8
61
+ elif (
62
+ 5 * PI / 8 <= direction < 7 * PI / 8
63
+ or 13 * PI / 8 <= direction < 15 * PI / 8
69
64
):
70
65
nw = sobel_grad [row - 1 , col - 1 ]
71
66
se = sobel_grad [row + 1 , col + 1 ]
72
67
if sobel_grad [row , col ] >= nw and sobel_grad [row , col ] >= se :
73
- dst [row , col ] = sobel_grad [row , col ]
74
-
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
- """
83
- if dst [row , col ] >= threshold_high :
84
- dst [row , col ] = strong
85
- elif dst [row , col ] <= threshold_low :
86
- dst [row , col ] = 0
68
+ destination [row , col ] = sobel_grad [row , col ]
69
+
70
+ return destination
71
+
72
+
73
+ def detect_high_low_threshold (
74
+ image_shape , destination , threshold_low , threshold_high , weak , strong
75
+ ):
76
+ """
77
+ High-Low threshold detection. If an edge pixel’s gradient value is higher
78
+ than the high threshold value, it is marked as a strong edge pixel. If an
79
+ edge pixel’s gradient value is smaller than the high threshold value and
80
+ larger than the low threshold value, it is marked as a weak edge pixel. If
81
+ an edge pixel's value is smaller than the low threshold value, it will be
82
+ suppressed.
83
+ """
84
+ for row in range (1 , image_shape [0 ] - 1 ):
85
+ for col in range (1 , image_shape [1 ] - 1 ):
86
+ if destination [row , col ] >= threshold_high :
87
+ destination [row , col ] = strong
88
+ elif destination [row , col ] <= threshold_low :
89
+ destination [row , col ] = 0
87
90
else :
88
- dst [row , col ] = weak
91
+ destination [row , col ] = weak
89
92
93
+
94
+ def track_edge (image_shape , destination , weak , strong ):
90
95
"""
91
96
Edge tracking. Usually a weak edge pixel caused from true edges will be connected
92
97
to a strong edge pixel while noise responses are unconnected. As long as there is
93
98
one strong edge pixel that is involved in its 8-connected neighborhood, that weak
94
99
edge point can be identified as one that should be preserved.
95
100
"""
96
- for row in range (1 , image_row ):
97
- for col in range (1 , image_col ):
98
- if dst [row , col ] == weak :
101
+ for row in range (1 , image_shape [ 0 ] ):
102
+ for col in range (1 , image_shape [ 1 ] ):
103
+ if destination [row , col ] == weak :
99
104
if 255 in (
100
- dst [row , col + 1 ],
101
- dst [row , col - 1 ],
102
- dst [row - 1 , col ],
103
- dst [row + 1 , col ],
104
- dst [row - 1 , col - 1 ],
105
- dst [row + 1 , col - 1 ],
106
- dst [row - 1 , col + 1 ],
107
- dst [row + 1 , col + 1 ],
105
+ destination [row , col + 1 ],
106
+ destination [row , col - 1 ],
107
+ destination [row - 1 , col ],
108
+ destination [row + 1 , col ],
109
+ destination [row - 1 , col - 1 ],
110
+ destination [row + 1 , col - 1 ],
111
+ destination [row - 1 , col + 1 ],
112
+ destination [row + 1 , col + 1 ],
108
113
):
109
- dst [row , col ] = strong
114
+ destination [row , col ] = strong
110
115
else :
111
- dst [row , col ] = 0
116
+ destination [row , col ] = 0
117
+
118
+
119
+ def canny (image , threshold_low = 15 , threshold_high = 30 , weak = 128 , strong = 255 ):
120
+ # gaussian_filter
121
+ gaussian_out = img_convolve (image , gen_gaussian_kernel (9 , sigma = 1.4 ))
122
+ # get the gradient and degree by sobel_filter
123
+ sobel_grad , sobel_theta = sobel_filter (gaussian_out )
124
+ gradient_direction = PI + np .rad2deg (sobel_theta )
125
+
126
+ destination = suppress_non_maximum (image .shape , gradient_direction , sobel_grad )
127
+
128
+ detect_high_low_threshold (
129
+ image .shape , destination , threshold_low , threshold_high , weak , strong
130
+ )
131
+
132
+ track_edge (image .shape , destination , weak , strong )
112
133
113
- return dst
134
+ return destination
114
135
115
136
116
137
if __name__ == "__main__" :
117
138
# read original image in gray mode
118
139
lena = cv2 .imread (r"../image_data/lena.jpg" , 0 )
119
140
# canny edge detection
120
- canny_dst = canny (lena )
121
- cv2 .imshow ("canny" , canny_dst )
141
+ canny_destination = canny (lena )
142
+ cv2 .imshow ("canny" , canny_destination )
122
143
cv2 .waitKey (0 )
0 commit comments