diff --git a/labo4.py b/labo4.py index 32d1c6a0ea01ae5f923d65a5d82bd647a2731858..ab7010fca92f70102f8db9bb158057f56febfe18 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()