-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
add canny edge detection algorithm and modify sobel_filter #991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
81b6352
add gaussian filter algorithm and lena.jpg
lighttxu e263160
add img_convolve algorithm and sobel_filter
lighttxu 506a77d
Merge pull request #1 from TheAlgorithms/master
lighttxu f008a59
Merge pull request #3 from TheAlgorithms/master
lighttxu 62f5e07
add canny edge detection algorithm and modify sobel_filter
lighttxu 2e1539c
format to avoid the backslashes
lighttxu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import cv2 | ||
import numpy as np | ||
from digital_image_processing.filters.convolve import img_convolve | ||
from digital_image_processing.filters.sobel_filter import sobel_filter | ||
|
||
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))) | ||
return g | ||
|
||
|
||
def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255): | ||
image_row, image_col = image.shape[0], image.shape[1] | ||
# gaussian_filter | ||
gaussian_out = img_convolve(image, gen_gaussian_kernel(9, sigma=1.4)) | ||
# get the gradient and degree by sobel_filter | ||
sobel_grad, sobel_theta = sobel_filter(gaussian_out) | ||
gradient_direction = np.rad2deg(sobel_theta) | ||
gradient_direction += PI | ||
|
||
dst = np.zeros((image_row, image_col)) | ||
|
||
""" | ||
Non-maximum suppression. If the edge strength of the current pixel is the largest compared to the other pixels | ||
in the mask with the same direction, the value will be preserved. Otherwise, the value will be suppressed. | ||
""" | ||
for row in range(1, image_row - 1): | ||
for col in range(1, image_col - 1): | ||
direction = gradient_direction[row, col] | ||
|
||
if ( | ||
0 <= direction < 22.5 | ||
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): | ||
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): | ||
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): | ||
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: | ||
dst[row, col] = sobel_grad[row, col] | ||
|
||
""" | ||
High-Low threshold detection. If an edge pixel’s gradient value is higher than the high threshold | ||
value, it is marked as a strong edge pixel. If an edge pixel’s gradient value is smaller than the high | ||
threshold value and larger than the low threshold value, it is marked as a weak edge pixel. If an edge | ||
pixel's value is smaller than the low threshold value, it will be suppressed. | ||
""" | ||
if dst[row, col] >= threshold_high: | ||
dst[row, col] = strong | ||
elif dst[row, col] <= threshold_low: | ||
dst[row, col] = 0 | ||
else: | ||
dst[row, col] = weak | ||
|
||
""" | ||
Edge tracking. Usually a weak edge pixel caused from true edges will be connected to a strong edge pixel while | ||
noise responses are unconnected. As long as there is one strong edge pixel that is involved in its 8-connected | ||
neighborhood, that weak edge point can be identified as one that should be preserved. | ||
""" | ||
for row in range(1, image_row): | ||
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] = strong | ||
else: | ||
dst[row, col] = 0 | ||
|
||
return dst | ||
|
||
|
||
if __name__ == '__main__': | ||
# read original image in gray mode | ||
lena = cv2.imread(r'../image_data/lena.jpg', 0) | ||
# canny edge detection | ||
canny_dst = canny(lena) | ||
cv2.imshow('canny', canny_dst) | ||
cv2.waitKey(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid the backslashes with something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, concise and clear!