Skip to content

Commit a806c0d

Browse files
committed
Fix review issues
1 parent 7457c0f commit a806c0d

File tree

1 file changed

+14
-16
lines changed
  • digital_image_processing/edge_detection

1 file changed

+14
-16
lines changed

digital_image_processing/edge_detection/canny.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ def gen_gaussian_kernel(k_size, sigma):
1818
return g
1919

2020

21-
def suppress_non_maximum(image_row, image_col, gradient_direction, sobel_grad):
21+
def suppress_non_maximum(image_shape, gradient_direction, sobel_grad):
2222
"""
2323
Non-maximum suppression. If the edge strength of the current pixel is the largest
2424
compared to the other pixels in the mask with the same direction, the value will be
2525
preserved. Otherwise, the value will be suppressed.
2626
"""
27-
dst = np.zeros((image_row, image_col))
27+
dst = np.zeros(image_shape)
2828

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):
3131
direction = gradient_direction[row, col]
3232

3333
if (
@@ -71,7 +71,7 @@ def suppress_non_maximum(image_row, image_col, gradient_direction, sobel_grad):
7171

7272

7373
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
7575
):
7676
"""
7777
High-Low threshold detection. If an edge pixel’s gradient value is higher
@@ -81,8 +81,8 @@ def detect_high_low_threshold(
8181
an edge pixel's value is smaller than the low threshold value, it will be
8282
suppressed.
8383
"""
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):
8686
if dst[row, col] >= threshold_high:
8787
dst[row, col] = strong
8888
elif dst[row, col] <= threshold_low:
@@ -91,15 +91,15 @@ def detect_high_low_threshold(
9191
dst[row, col] = weak
9292

9393

94-
def track_edge(image_row, image_col, dst, weak, strong):
94+
def track_edge(image_shape, dst, weak, strong):
9595
"""
9696
Edge tracking. Usually a weak edge pixel caused from true edges will be connected
9797
to a strong edge pixel while noise responses are unconnected. As long as there is
9898
one strong edge pixel that is involved in its 8-connected neighborhood, that weak
9999
edge point can be identified as one that should be preserved.
100100
"""
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]):
103103
if dst[row, col] == weak:
104104
if 255 in (
105105
dst[row, col + 1],
@@ -117,21 +117,19 @@ def track_edge(image_row, image_col, dst, weak, strong):
117117

118118

119119
def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
120-
image_row, image_col = image.shape[0], image.shape[1]
121120
# gaussian_filter
122121
gaussian_out = img_convolve(image, gen_gaussian_kernel(9, sigma=1.4))
123122
# get the gradient and degree by sobel_filter
124123
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)
127125

128-
dst = suppress_non_maximum(image_row, image_col, gradient_direction, sobel_grad)
126+
dst = suppress_non_maximum(image.shape, gradient_direction, sobel_grad)
129127

130128
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
132130
)
133131

134-
track_edge(image_row, image_col, dst, weak, strong)
132+
track_edge(image.shape, dst, weak, strong)
135133

136134
return dst
137135

0 commit comments

Comments
 (0)