mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-04 16:57:32 +08:00
psf/black code formatting (#1277)
This commit is contained in:

committed by
Christian Clauss

parent
07f04a2e55
commit
9eac17a408
@ -8,8 +8,12 @@ PI = 180
|
||||
|
||||
def gen_gaussian_kernel(k_size, sigma):
|
||||
center = k_size // 2
|
||||
x, y = np.mgrid[0 - center:k_size - center, 0 - center:k_size - center]
|
||||
g = 1 / (2 * np.pi * sigma) * np.exp(-(np.square(x) + np.square(y)) / (2 * np.square(sigma)))
|
||||
x, y = np.mgrid[0 - center : k_size - center, 0 - center : k_size - center]
|
||||
g = (
|
||||
1
|
||||
/ (2 * np.pi * sigma)
|
||||
* np.exp(-(np.square(x) + np.square(y)) / (2 * np.square(sigma)))
|
||||
)
|
||||
return g
|
||||
|
||||
|
||||
@ -34,27 +38,33 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
|
||||
|
||||
if (
|
||||
0 <= direction < 22.5
|
||||
or 15 * PI / 8 <= direction <= 2 * PI
|
||||
or 7 * PI / 8 <= direction <= 9 * PI / 8
|
||||
or 15 * PI / 8 <= direction <= 2 * PI
|
||||
or 7 * PI / 8 <= direction <= 9 * PI / 8
|
||||
):
|
||||
W = sobel_grad[row, col - 1]
|
||||
E = sobel_grad[row, col + 1]
|
||||
if sobel_grad[row, col] >= W and sobel_grad[row, col] >= E:
|
||||
dst[row, col] = sobel_grad[row, col]
|
||||
|
||||
elif (PI / 8 <= direction < 3 * PI / 8) or (9 * PI / 8 <= direction < 11 * PI / 8):
|
||||
elif (PI / 8 <= direction < 3 * PI / 8) or (
|
||||
9 * PI / 8 <= direction < 11 * PI / 8
|
||||
):
|
||||
SW = sobel_grad[row + 1, col - 1]
|
||||
NE = sobel_grad[row - 1, col + 1]
|
||||
if sobel_grad[row, col] >= SW and sobel_grad[row, col] >= NE:
|
||||
dst[row, col] = sobel_grad[row, col]
|
||||
|
||||
elif (3 * PI / 8 <= direction < 5 * PI / 8) or (11 * PI / 8 <= direction < 13 * PI / 8):
|
||||
elif (3 * PI / 8 <= direction < 5 * PI / 8) or (
|
||||
11 * PI / 8 <= direction < 13 * PI / 8
|
||||
):
|
||||
N = sobel_grad[row - 1, col]
|
||||
S = sobel_grad[row + 1, col]
|
||||
if sobel_grad[row, col] >= N and sobel_grad[row, col] >= S:
|
||||
dst[row, col] = sobel_grad[row, col]
|
||||
|
||||
elif (5 * PI / 8 <= direction < 7 * PI / 8) or (13 * PI / 8 <= direction < 15 * PI / 8):
|
||||
elif (5 * PI / 8 <= direction < 7 * PI / 8) or (
|
||||
13 * PI / 8 <= direction < 15 * PI / 8
|
||||
):
|
||||
NW = sobel_grad[row - 1, col - 1]
|
||||
SE = sobel_grad[row + 1, col + 1]
|
||||
if sobel_grad[row, col] >= NW and sobel_grad[row, col] >= SE:
|
||||
@ -82,14 +92,14 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
|
||||
for col in range(1, image_col):
|
||||
if dst[row, col] == weak:
|
||||
if 255 in (
|
||||
dst[row, col + 1],
|
||||
dst[row, col - 1],
|
||||
dst[row - 1, col],
|
||||
dst[row + 1, col],
|
||||
dst[row - 1, col - 1],
|
||||
dst[row + 1, col - 1],
|
||||
dst[row - 1, col + 1],
|
||||
dst[row + 1, col + 1],
|
||||
dst[row, col + 1],
|
||||
dst[row, col - 1],
|
||||
dst[row - 1, col],
|
||||
dst[row + 1, col],
|
||||
dst[row - 1, col - 1],
|
||||
dst[row + 1, col - 1],
|
||||
dst[row - 1, col + 1],
|
||||
dst[row + 1, col + 1],
|
||||
):
|
||||
dst[row, col] = strong
|
||||
else:
|
||||
@ -98,10 +108,10 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255):
|
||||
return dst
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
# read original image in gray mode
|
||||
lena = cv2.imread(r'../image_data/lena.jpg', 0)
|
||||
lena = cv2.imread(r"../image_data/lena.jpg", 0)
|
||||
# canny edge detection
|
||||
canny_dst = canny(lena)
|
||||
cv2.imshow('canny', canny_dst)
|
||||
cv2.imshow("canny", canny_dst)
|
||||
cv2.waitKey(0)
|
||||
|
Reference in New Issue
Block a user