Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views1 page

Image Filtering with OpenCV

The document contains a Python script that uses OpenCV to load an image, apply median filtering to its color channels, and display both the original and filtered images using Matplotlib. It checks if the image loads successfully and merges the filtered channels back together for display. Additionally, the script includes an option to save the filtered image to a specified path.

Uploaded by

Radiel Kassa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Image Filtering with OpenCV

The document contains a Python script that uses OpenCV to load an image, apply median filtering to its color channels, and display both the original and filtered images using Matplotlib. It checks if the image loads successfully and merges the filtered channels back together for display. Additionally, the script includes an option to save the filtered image to a specified path.

Uploaded by

Radiel Kassa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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

You might also like