Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 51k
added laplacian_filter file#9783
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
7672d1a642af235e6100c38190985a5a84d2cbbe3af233a5419a7879d8040f56c5b418ea541e686dc5a9de92d3d8325b64e6fda58cccf2a95f9331a35f8b7aaa0da39d451d62dabd6c94a118f339b33638656afd552a873641c0632fd6024667055a0d83b212af89bcb2e23081c72a650ce40a234942b3891da5ef52f3b524d265b435efe6e36fa6a84c027395502443718979e30c5585824fcb1bcbd84532e385e09582f2d184c57159ffeFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # @Author : ojas-wani | ||
| # @File : laplacian_filter.py | ||
| # @Date : 10/04/2023 | ||
| import numpy as np | ||
| from cv2 import ( | ||
| BORDER_DEFAULT, | ||
| COLOR_BGR2GRAY, | ||
| CV_64F, | ||
| cvtColor, | ||
| filter2D, | ||
| imread, | ||
| imshow, | ||
| waitKey, | ||
| ) | ||
cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| from digital_image_processing.filters.gaussian_filter import gaussian_filter | ||
cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def my_laplacian(src: np.ndarray, ksize: int) -> np.ndarray: | ||
cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| :param src: the source image, which should be a grayscale or color image. | ||
| :param ksize: the size of the kernel used to compute the Laplacian filter, | ||
| which can be 1, 3, 5, or 7. | ||
| >>> my_laplacian(src=np.array([]), ksize=0) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: ksize must be in (1, 3, 5, 7) | ||
| """ | ||
| kernels = { | ||
| 1: np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]), | ||
| 3: np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]), | ||
| 5: np.array( | ||
| [ | ||
| [0, 0, -1, 0, 0], | ||
| [0, -1, -2, -1, 0], | ||
| [-1, -2, 16, -2, -1], | ||
| [0, -1, -2, -1, 0], | ||
| [0, 0, -1, 0, 0], | ||
| ] | ||
| ), | ||
| 7: np.array( | ||
| [ | ||
| [0, 0, 0, -1, 0, 0, 0], | ||
| [0, 0, -2, -3, -2, 0, 0], | ||
| [0, -2, -7, -10, -7, -2, 0], | ||
| [-1, -3, -10, 68, -10, -3, -1], | ||
| [0, -2, -7, -10, -7, -2, 0], | ||
| [0, 0, -2, -3, -2, 0, 0], | ||
| [0, 0, 0, -1, 0, 0, 0], | ||
| ] | ||
| ), | ||
| } | ||
| if ksize not in kernels: | ||
| msg = f"ksize must be in {tuple(kernels)}" | ||
| raise ValueError(msg) | ||
| # Apply the Laplacian kernel using convolution | ||
| return filter2D( | ||
| src, CV_64F, kernels[ksize], 0, borderType=BORDER_DEFAULT, anchor=(0, 0) | ||
| ) | ||
| if __name__ == "__main__": | ||
| # read original image | ||
| img = imread(r"../image_data/lena.jpg") | ||
| # turn image in gray scale value | ||
| gray = cvtColor(img, COLOR_BGR2GRAY) | ||
| # Applying gaussian filter | ||
| blur_image = gaussian_filter(gray, 3, sigma=1) | ||
| # Apply multiple Kernel to detect edges | ||
| laplacian_image = my_laplacian(ksize=3, src=blur_image) | ||
| imshow("Original image", img) | ||
| imshow("Detected edges using laplacian filter", laplacian_image) | ||
| waitKey(0) | ||
Uh oh!
There was an error while loading. Please reload this page.