-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add gabor filter #5289
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
Add gabor filter #5289
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa90cf4
add gabor_filter.py
Mozartuss ec3eed4
Update gabor_filter.py
Mozartuss 82ab594
update gabor_filter.py
Mozartuss b198cd5
add doctest
Mozartuss 97bf960
change import order
Mozartuss 08d703e
Update digital_image_processing/filters/gabor_filter.py
Mozartuss ed0f359
Update gabor_filter.py
poyea 97dd0d4
fix gabor filter calculation
Mozartuss 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
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,85 @@ | ||
# Implementation of the Gaborfilter | ||
# https://en.wikipedia.org/wiki/Gabor_filter | ||
import numpy as np | ||
from cv2 import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filter2D, imread, imshow, waitKey | ||
|
||
|
||
def gabor_filter_kernel( | ||
ksize: int, sigma: int, theta: int, lambd: int, gamma: int, psi: int | ||
) -> np.ndarray: | ||
""" | ||
:param ksize: The kernelsize of the convolutional filter (ksize x ksize) | ||
:param sigma: standard deviation of the gaussian bell curve | ||
:param theta: The orientation of the normal to the parallel stripes | ||
of Gabor function. | ||
:param lambd: Wavelength of the sinusoidal component. | ||
:param gamma: The spatial aspect ratio and specifies the ellipticity | ||
of the support of Gabor function. | ||
:param psi: The phase offset of the sinusoidal function. | ||
|
||
>>> gabor_filter_kernel(3, 8, 0, 10, 0, 0).tolist() | ||
[[0.8027212023735046, 1.0, 0.8027212023735046], [0.8027212023735046, 1.0, \ | ||
0.8027212023735046], [0.8027212023735046, 1.0, 0.8027212023735046]] | ||
|
||
""" | ||
|
||
# prepare kernel | ||
# the kernel size have to be odd | ||
if (ksize % 2) == 0: | ||
ksize = ksize + 1 | ||
gabor = np.zeros((ksize, ksize), dtype=np.float32) | ||
|
||
# each value | ||
for y in range(ksize): | ||
for x in range(ksize): | ||
# distance from center | ||
px = x - ksize // 2 | ||
py = y - ksize // 2 | ||
|
||
# degree to radiant | ||
_theta = theta / 180 * np.pi | ||
cos_theta = np.cos(_theta) | ||
sin_theta = np.sin(_theta) | ||
|
||
# get kernel x | ||
_x = cos_theta * px + sin_theta * py | ||
|
||
# get kernel y | ||
_y = -sin_theta * px + cos_theta * py | ||
|
||
# fill kernel | ||
gabor[y, x] = np.exp( | ||
-(_x ** 2 + gamma ** 2 * _y ** 2) / (2 * sigma ** 2) | ||
) * np.cos(2 * np.pi * _x / lambd + psi) | ||
|
||
return gabor | ||
|
||
|
||
if __name__ == "__main__": | ||
Mozartuss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import doctest | ||
|
||
doctest.testmod() | ||
# read original image | ||
img = imread("../image_data/lena.jpg") | ||
# turn image in gray scale value | ||
gray = cvtColor(img, COLOR_BGR2GRAY) | ||
|
||
# Apply multiple Kernel to detect edges | ||
out = np.zeros(gray.shape[:2]) | ||
for theta in [0, 30, 60, 90, 120, 150]: | ||
""" | ||
ksize = 10 | ||
sigma = 8 | ||
lambd = 10 | ||
gamma = 0 | ||
psi = 0 | ||
""" | ||
kernel_10 = gabor_filter_kernel(10, 8, theta, 10, 0, 0) | ||
out += filter2D(gray, CV_8UC3, kernel_10) | ||
out = out / out.max() * 255 | ||
out = out.astype(np.uint8) | ||
|
||
imshow("Original", gray) | ||
imshow("Gabor filter with 20x20 mask and 6 directions", out) | ||
|
||
waitKey(0) |
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.
Uh oh!
There was an error while loading. Please reload this page.