mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
added laplacian_filter file (#9783)
* added laplacian_filter file * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated laplacian.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated laplacian_py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * required changes to laplacian file * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changed laplacian_filter.py * changed laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changed laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changed laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update laplacian_filter.py * update laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changed laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changed laplacian_filter.py * changed laplacian_filter.py * changed laplacian_filter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update laplacian_filter.py * Add a test --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
9200c64464
commit
19fc788197
81
digital_image_processing/filters/laplacian_filter.py
Normal file
81
digital_image_processing/filters/laplacian_filter.py
Normal file
|
@ -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,
|
||||||
|
)
|
||||||
|
|
||||||
|
from digital_image_processing.filters.gaussian_filter import gaussian_filter
|
||||||
|
|
||||||
|
|
||||||
|
def my_laplacian(src: np.ndarray, ksize: int) -> np.ndarray:
|
||||||
|
"""
|
||||||
|
: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)
|
Loading…
Reference in New Issue
Block a user