import cv2
import numpy as np
import matplotlib.pyplot as plt
# ----------------------------------------
# Read Image
# ----------------------------------------
image = cv2.imread("flower3.jpg")
if image is None:
print("Error: Image not found!")
exit()
# Convert BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# ----------------------------------------
# Function to Display Image
# ----------------------------------------
def show_image(title, image):
plt.figure(figsize=(7, 5))
plt.imshow(image)
plt.title(title)
plt.axis("off")
plt.show()
plt.close()
input("\nPress Enter to return to the menu...")
# ----------------------------------------
# Add Salt & Pepper Noise
# ----------------------------------------
def add_salt_pepper_noise(image):
sp_noise = image.copy()
prob = 0.02
# Salt noise
num_salt = int(prob * image.size * 0.5)
coords = [
np.random.randint(0, i, num_salt)
for i in image.shape[:2]
]
sp_noise[coords[0], coords[1]] = [255, 255, 255]
# Pepper noise
num_pepper = int(prob * image.size * 0.5)
coords = [
np.random.randint(0, i, num_pepper)
for i in image.shape[:2]
]
sp_noise[coords[0], coords[1]] = [0, 0, 0]
return sp_noise
# ----------------------------------------
# Add Gaussian Noise
# ----------------------------------------
def add_gaussian_noise(image):
mean = 0
sigma = 25
gaussian = np.random.normal(
mean,
sigma,
image.shape
)
gaussian_noise = image + gaussian
gaussian_noise = np.clip(
gaussian_noise,
0,
255
).astype(np.uint8)
return gaussian_noise
# ----------------------------------------
# Menu Driven Program
# ----------------------------------------
while True:
print("\n====================================================")
print("PROGRAM 7 : IMAGE NOISE MODELS AND RESTORATION")
print("====================================================")
print("1. Image Restoration")
print("2. Remove Salt & Pepper Noise")
print("3. Minimize Gaussian Noise")
print("4. Median Filter")
print("5. Exit")
choice = input("\nEnter your choice (1-5): ")
# ----------------------------------------
# 1. Image Restoration
# ----------------------------------------
if choice == "1":
gaussian_noise = add_gaussian_noise(image)
restored = cv2.GaussianBlur(
gaussian_noise,
(5, 5),
0
)
print("\nDisplaying Restored Image...")
show_image("Restored Image", restored)
# ----------------------------------------
# 2. Remove Salt & Pepper Noise
# ----------------------------------------
elif choice == "2":
sp_noise = add_salt_pepper_noise(image)
salt_removed = cv2.medianBlur(
sp_noise,
5
)
print("\nDisplaying Salt & Pepper Noise Removed Image...")
show_image(
"Salt & Pepper Noise Removed",
salt_removed
)
# ----------------------------------------
# 3. Minimize Gaussian Noise
# ----------------------------------------
elif choice == "3":
gaussian_noise = add_gaussian_noise(image)
gaussian_removed = cv2.GaussianBlur(
gaussian_noise,
(5, 5),
1
)
print("\nDisplaying Gaussian Noise Removed Image...")
show_image(
"Gaussian Noise Removed",
gaussian_removed
)
# ----------------------------------------
# 4. Median Filter
# ----------------------------------------
elif choice == "4":
sp_noise = add_salt_pepper_noise(image)
median = cv2.medianBlur(
sp_noise,
5
)
print("\nDisplaying Median Filtered Image...")
show_image(
"Median Filter",
median
)
# ----------------------------------------
# 5. Exit
# ----------------------------------------
elif choice == "5":
print("\nProgram Ended Successfully.")
break
# ----------------------------------------
# Invalid Choice
# ----------------------------------------
else:
print("\nInvalid Choice! Please enter a number between 1 and 5.")⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.