import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the image
image_path = r'C:\Users\dell\Desktop\test.png' # Change this path
image = cv2.imread(image_path)
# Check if the image loaded successfully
if image is None:
print("Error: Could not load image.")
else:
# Split the image into its Red, Green, and Blue channels
b, g, r = cv2.split(image)
# Apply median filtering to each channel separately
r_filtered = cv2.medianBlur(r, 5)
g_filtered = cv2.medianBlur(g, 5)
b_filtered = cv2.medianBlur(b, 5)
# Merge the filtered channels back together
filtered_image = cv2.merge((b_filtered, g_filtered, r_filtered))
# Display the original and filtered images
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title('Filtered Image')
plt.imshow(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
# Optionally save the filtered image
cv2.imwrite(r'C:\Users\dell\Desktop\test1.png', filtered_image) # Change this path