Skip to content
Snippets Groups Projects
Verified Commit f5c272cb authored by iliya.saroukha's avatar iliya.saroukha :first_quarter_moon:
Browse files

feat: sobel fully done lab4

parent 2c7849db
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,17 @@ from enum import Enum ...@@ -8,6 +8,17 @@ from enum import Enum
Img = npt.NDArray[np.uint8] 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: def load_img(path: str) -> Img:
img = np.array(Image.open(path)) img = np.array(Image.open(path))
...@@ -79,11 +90,6 @@ def rgb_to_gray(img: Img) -> Img: ...@@ -79,11 +90,6 @@ def rgb_to_gray(img: Img) -> Img:
return new_image return new_image
class Isotropy(Enum):
ISO_90 = 1
ISO_45 = 2
def laplace(img: Img, isotropy: Isotropy) -> Img: def laplace(img: Img, isotropy: Isotropy) -> Img:
match(isotropy): match(isotropy):
case isotropy.ISO_90: case isotropy.ISO_90:
...@@ -118,10 +124,26 @@ def gauss_kernel(length: int = 3, sigma: float = 1.0) -> Img: ...@@ -118,10 +124,26 @@ def gauss_kernel(length: int = 3, sigma: float = 1.0) -> Img:
return kernel / np.sum(kernel) 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__": if __name__ == "__main__":
img = load_img("resources/noisy_hotel.png") img = load_img("resources/noisy_hotel.png")
gray = rgb_to_gray(img) gray = rgb_to_gray(img)
# show_image(sobel_filter(gray, Orientation.HORIZONTAL))
laplacian = laplace(xcorr(gray, gauss_kernel()), Isotropy.ISO_45) laplacian = laplace(xcorr(gray, gauss_kernel()), Isotropy.ISO_45)
thresholded = thresholding(laplacian, 50) thresholded = thresholding(laplacian, 50)
...@@ -130,14 +152,17 @@ if __name__ == "__main__": ...@@ -130,14 +152,17 @@ if __name__ == "__main__":
fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(12, 6)) fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(12, 6))
axs[0].imshow(gray, cmap="gray", vmin=0, vmax=255) axs[0].imshow(sobel_filter(gray, Orientation.VERTICAL),
axs[0].set_title('Original image') 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].imshow(sobel_filter(gray, Orientation.HORIZONTAL),
axs[1].set_title('Sharpened (Laplacian filter, isotropy 90)') 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].imshow(sobel_filter(gray, Orientation.X_Y),
axs[2].set_title('Sharpened (Laplacian filter, isotropy 45)') cmap="gray", vmin=0, vmax=255)
axs[2].set_title('Sobel filter (both axis)')
plt.tight_layout() plt.tight_layout()
plt.show() plt.show()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment