-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from 12 commits
7672d1a
642af23
5e6100c
3819098
5a5a84d
2cbbe3a
f233a54
19a7879
d8040f5
6c5b418
ea541e6
86dc5a9
de92d3d
8325b64
e6fda58
cccf2a9
5f9331a
35f8b7a
aa0da39
d451d62
dabd6c9
4a118f3
39b3363
8656afd
552a873
641c063
2fd6024
667055a
0d83b21
2af89bc
b2e2308
1c72a65
0ce40a2
34942b3
891da5e
f52f3b5
24d265b
435efe6
e36fa6a
84c0273
9550244
3718979
e30c558
5824fcb
1bcbd84
532e385
e09582f
2d184c5
7159ffe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# @Author : ojas-wani | ||
# @File : laplacian_filter.py | ||
# @Time : 10/04/2023 | ||
|
||
from cv2 import BORDER_DEFAULT, cvtColor, CV_64F, COLOR_BGR2GRAY, filter2D, GaussianBlur, imread, imshow, waitKey | ||
import numpy as np | ||
|
||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def my_laplacian(src, ddepth=-1, ksize=3, scale=1, delta=0, bordertype='default') -> np.ndarray: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
|
||
""" | ||
:param src: the source image, which should be a grayscale or color image. | ||
:param ddepth: the desired depth of the destination image, | ||
-1 or one of np.uint8, np.uint16, np.int16, np.float32 or np.float64. | ||
:param ksize: the size of the kernel used to compute the Laplacian filter, | ||
which can be 1, 3, 5 or 7. | ||
:param scale: an optional scaling factor applied to the computed Laplacian values, | ||
which can be used to enhance or reduce the effect of the filter. | ||
:param delta: an optional value added to the computed Laplacian values, | ||
which can be used to shift the output image intensity range. | ||
:param bordertype: an optional flag that specifies how to handle the image borders, | ||
which can be one of 'default', 'reflect', or 'constant'. | ||
|
||
""" | ||
|
||
# Convert the source image to a numpy array | ||
src = np.array(src) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 28:1.
parser error: error at 27:4: expected one of (, *, +, -, ..., AWAIT, DEDENT, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
src = np.array(src)
^ |
||
# Get the shape and depth of the source image | ||
src_depth = src.dtype | ||
|
||
# If ddepth is -1, use the same depth as the source image | ||
if ddepth == -1: | ||
ddepth = src_depth | ||
|
||
# Create a Laplacian kernel matrix according to the ksize | ||
ojas-wani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ksize == 1: | ||
kernel = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) | ||
elif ksize == 3: | ||
kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) | ||
elif ksize == 5: | ||
kernel = 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], | ||
] | ||
) | ||
elif ksize == 7: | ||
kernel = 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], | ||
] | ||
) | ||
|
||
# Apply the Laplacian kernel using convolution | ||
laplacian_result = filter2D( | ||
src, ddepth, kernel, delta, borderType=BORDER_DEFAULT, anchor=(0, 0) | ||
) | ||
|
||
return laplacian_result | ||
|
||
|
||
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 = GaussianBlur(gray, (3, 3), 0, 0) | ||
|
||
# Apply multiple Kernel to detect edges | ||
laplacian_image = my_laplacian(blur_image, ddepth=CV_64F, ksize=3) | ||
|
||
imshow("Original image", img) | ||
imshow("Deteced edges using laplacian filter", laplacian_image) | ||
|
||
waitKey(0) |
Uh oh!
There was an error while loading. Please reload this page.