From f5c272cb6136ecd53a3b1c1f6ebf8229d1e10b21 Mon Sep 17 00:00:00 2001 From: iliya <iliya.saroukhanian@etu.hesge.ch> Date: Sat, 13 Apr 2024 16:17:53 +0200 Subject: [PATCH] feat: sobel fully done lab4 --- labo4.py | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/labo4.py b/labo4.py index 32d1c6a..ab7010f 100644 --- a/labo4.py +++ b/labo4.py @@ -8,6 +8,17 @@ from enum import Enum Img = npt.NDArray[np.uint8] +class Isotropy(Enum): + ISO_90 = 1 + ISO_45 = 2 + + +class Orientation(Enum): + HORIZONTAL = 1 + VERTICAL = 2 + X_Y = 3 + + def load_img(path: str) -> Img: img = np.array(Image.open(path)) @@ -79,11 +90,6 @@ def rgb_to_gray(img: Img) -> Img: return new_image -class Isotropy(Enum): - ISO_90 = 1 - ISO_45 = 2 - - def laplace(img: Img, isotropy: Isotropy) -> Img: match(isotropy): case isotropy.ISO_90: @@ -118,10 +124,26 @@ def gauss_kernel(length: int = 3, sigma: float = 1.0) -> Img: return kernel / np.sum(kernel) +def sobel_filter(img: Img, orientation: Orientation) -> Img: + kernel = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) + + match(orientation): + case orientation.VERTICAL: + return xcorr(img, kernel) + case orientation.HORIZONTAL: + return xcorr(img, kernel.T) + case orientation.X_Y: + return xcorr(img, kernel + kernel.T) + case _: + print("Invalid orientation value") + + if __name__ == "__main__": img = load_img("resources/noisy_hotel.png") gray = rgb_to_gray(img) + # show_image(sobel_filter(gray, Orientation.HORIZONTAL)) + laplacian = laplace(xcorr(gray, gauss_kernel()), Isotropy.ISO_45) thresholded = thresholding(laplacian, 50) @@ -130,14 +152,17 @@ if __name__ == "__main__": fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(12, 6)) - axs[0].imshow(gray, cmap="gray", vmin=0, vmax=255) - axs[0].set_title('Original image') + axs[0].imshow(sobel_filter(gray, Orientation.VERTICAL), + cmap="gray", vmin=0, vmax=255) + axs[0].set_title('Vertical Sobel filter') - axs[1].imshow(sharp_iso_90, cmap="gray", vmin=0, vmax=255) - axs[1].set_title('Sharpened (Laplacian filter, isotropy 90)') + axs[1].imshow(sobel_filter(gray, Orientation.HORIZONTAL), + cmap="gray", vmin=0, vmax=255) + axs[1].set_title('Horizontal Sobel filter') - axs[2].imshow(sharp_iso_45, cmap="gray", vmin=0, vmax=255) - axs[2].set_title('Sharpened (Laplacian filter, isotropy 45)') + axs[2].imshow(sobel_filter(gray, Orientation.X_Y), + cmap="gray", vmin=0, vmax=255) + axs[2].set_title('Sobel filter (both axis)') plt.tight_layout() plt.show() -- GitLab