import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread(r"p8.jpg")
if image is None:
print("Error: Image not found!")
exit()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
while True:
print(" SPATIAL FILTERING MENU")
print("========================================")
print("1. Apply Spatial Low-Pass Filter")
print("2. Apply Spatial High-Pass Filter")
print("3. Apply Both Low-Pass and High-Pass Filters")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == '1':
low_pass = cv2.blur(gray, (3, 3))
cv2.imwrite("low_pass.png", low_pass)
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.imshow(gray, cmap="gray")
plt.title("Original Grayscale Image")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(low_pass, cmap="gray")
plt.title("Spatial Low-Pass Filter")
plt.axis("off")
plt.tight_layout()
plt.show()
print("Low-pass filtering completed.")
print("Output saved as low_pass.png")
elif choice == '2':
high_pass_kernel = np.array([[-1, -1, -1],[-1, 8, -1],[-1, -1, -1]])
high_pass = cv2.filter2D(gray,-1,high_pass_kernel)
cv2.imwrite("high_pass.png", high_pass)
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.imshow(gray, cmap="gray")
plt.title("Original Grayscale Image")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(high_pass, cmap="gray")
plt.title("Spatial High-Pass Filter")
plt.axis("off")
plt.tight_layout()
plt.show()
print("High-pass filtering completed.")
print("Output saved as high_pass.png")
elif choice == '3':
low_pass = cv2.blur(gray, (3, 3))
high_pass_kernel = np.array([[-1, -1, -1],[-1, 8, -1],[-1, -1, -1]])
high_pass = cv2.filter2D(gray,-1,high_pass_kernel)
cv2.imwrite("low_pass.png", low_pass)
cv2.imwrite("high_pass.png", high_pass)
plt.figure(figsize=(12, 4))
plt.subplot(1, 3, 1)
plt.imshow(image_rgb)
plt.title("Original Image")
plt.axis("off")
plt.subplot(1, 3, 2)
plt.imshow(low_pass, cmap="gray")
plt.title("Low-Pass Filter")
plt.axis("off")
plt.subplot(1, 3, 3)
plt.imshow(high_pass, cmap="gray")
plt.title("High-Pass Filter")
plt.axis("off")
plt.tight_layout()
plt.show()
print("Both filters applied successfully.")
print("Low-pass output saved as low_pass.png")
print("High-pass output saved as high_pass.png")
elif choice == '4':
print("Program terminated.")
break
else:
print("Invalid choice!")
print("Please enter a number between 1 and 4.")⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.