Kushal Mehta - 60003220069 - IT2 - IPCVExp5
Kushal Mehta - 60003220069 - IT2 - IPCVExp5
EXPERIMENT NO. 5
CO/LO: Apply Image Enhancement Techniques.
AIM / OBJECTIVE: To apply Spatial Filtering Enhancement Operations on a given image.
INPUT/DATASET:
PROCEDURE / ALGORITHM:
Describe the procedure that is used to carry out the experiment step-by-step. Describe
the features of any programs you developed.
CODE:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import pandas as pd
def read_grayscale_image(path):
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
return image
def histogram_equalization(image):
hist, bins = np.histogram(image.flatten(), 256, [0, 256])
cdf = hist.cumsum()
cdf_normalized = cdf * 255 / cdf[-1]
equalized_image = np.interp(image.flatten(), bins[:-1],
cdf_normalized)
return equalized_image.reshape(image.shape).astype(np.uint8), hist,
cdf
def apply_filters(image):
image = image.astype(np.float64) / 255.0
A = 1.5
high_boost_kernel = A * np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) -
kernel_hpf
high_boost_image = cv2.filter2D(image, -1, high_boost_kernel)
# Apply filters
lpf_avg, lpf_median, hpf_image, high_boost_image =
apply_filters(gray_image)
display_image(lpf_avg, "LPF - Average")
display_image(lpf_median, "LPF - Median")
display_image(hpf_image, "HPF - Edge Detection")
display_image(high_boost_image, "High Boost Filter")
# Plot histograms
plot_histogram(gray_image, "Histogram - Original Image")
plot_histogram(bright_image, "Histogram - Brightened Image")
plot_histogram(dark_image, "Histogram - Darkened Image")
plot_histogram(high_contrast_image, "Histogram - High Contrast Image")
plot_histogram(equalized_image, "Histogram - Histogram Equalized Image")
equalized_color_image = cv2.merge(equalized_channels)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(color_image)
plt.title("Original Color Image")
plt.subplot(1, 2, 2)
plt.imshow(equalized_color_image)
plt.title("Histogram Equalized Color Image")
plt.show()
Filtering Techniques:
Low-Pass Filters (LPF): The averaging and median filters reduce noise and smooth the
image, but they can blur edges.
High-Pass Filter (HPF): Enhances edges by emphasizing high-frequency details, useful
for detecting outlines.
High-Boost Filtering: Enhances sharpness while preserving details by combining the
original image with high-pass filtering.
Histograms: Brightened images shift the histogram right, darkened images shift left, and
high-contrast images stretch the histogram. Histogram equalization flattens the distribution,
improving contrast.
PDF & CDF Tables: Probability Density Function (PDF) represents pixel intensity
distribution, while Cumulative Distribution Function (CDF) helps visualize intensity
accumulation, showing the effectiveness of contrast enhancement.
REFERENCES:
Website References:
1. Kaggle, “Spatial Filtering OpenCV,” Available:
https://www.kaggle.com/code/bhavinmoriya/spatial-filtering-opencv.
2. OpenCV “Image Filtering,” OpenCV Documentation. Available:
https://docs.opencv.org/4.x/dd/d6a/tutorial_js_filtering.html#:~:text=As%20in%20one%2Ddi
mensional%20signals,finding%20edges%20in%20the%20images..