@@ -18,105 +18,126 @@ 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_shape , 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 """
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 ):
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 ):
4638 w = sobel_grad [row , col - 1 ]
4739 e = sobel_grad [row , col + 1 ]
4840 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 ]
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 ]
5649 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 ]
5851
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
6155 ):
6256 n = sobel_grad [row - 1 , col ]
6357 s = sobel_grad [row + 1 , col ]
6458 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 ]
6660
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
6964 ):
7065 nw = sobel_grad [row - 1 , col - 1 ]
7166 se = sobel_grad [row + 1 , col + 1 ]
7267 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
8790 else :
88- dst [row , col ] = weak
91+ destination [row , col ] = weak
8992
93+
94+ def track_edge (image_shape , destination , weak , strong ):
9095 """
9196 Edge tracking. Usually a weak edge pixel caused from true edges will be connected
9297 to a strong edge pixel while noise responses are unconnected. As long as there is
9398 one strong edge pixel that is involved in its 8-connected neighborhood, that weak
9499 edge point can be identified as one that should be preserved.
95100 """
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 :
99104 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 ],
108113 ):
109- dst [row , col ] = strong
114+ destination [row , col ] = strong
110115 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 )
112133
113- return dst
134+ return destination
114135
115136
116137if __name__ == "__main__" :
117138 # read original image in gray mode
118139 lena = cv2 .imread (r"../image_data/lena.jpg" , 0 )
119140 # 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 )
122143 cv2 .waitKey (0 )
0 commit comments