You are on page 1of 3

import cv2

import numpy as np
import matplotlib.pyplot as plt

# Load the sample image


image_path = '/content/image.jpg'
original_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

if original_image is None:
print("Error: Unable to load the image.")
exit()

# Function to display images side by side


def display_images(original, processed, processed_name):
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(original, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(processed, cmap='gray')
plt.title(processed_name)
plt.axis('off')

plt.show()

# Smoothing
smoothed_image = cv2.GaussianBlur(original_image, (5, 5), 0)
display_images(original_image, smoothed_image, 'Smoothed Image')

# Sharpening with high-pass filter


kernel_sharpening = np.array([[-1,-1,-1],
[-1, 9,-1],
[-1,-1,-1]])
sharpened_image = cv2.filter2D(original_image, -1, kernel_sharpening)
display_images(original_image, sharpened_image, 'Sharpened Image')

# High-boost filtering
k = 1.5 # Boosting factor
high_boost_kernel = np.array([[-1, -1, -1],
[-1, k+8, -1],
[-1, -1, -1]]) / k
high_boost_image = cv2.filter2D(original_image, -1, high_boost_kernel)
display_images(original_image, high_boost_image, 'High-boost Filtered
Image')

# Gradient filtering
sobelx = cv2.Sobel(original_image, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(original_image, cv2.CV_64F, 0, 1, ksize=3)
gradient_image = np.sqrt(sobelx**2 + sobely**2)
display_images(original_image, gradient_image, 'Gradient Filtered
Image')

You might also like