Assignment 2
Assignment 2
Total Marks: 04
Obtained Marks:
Computer Vision
Assignment # 02
Last Date of Submission: 25th October 2024
Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.
Note: Every coding block output must include your name as print function
must submit Jupyter and Pdf files
Q1: Take any image and perform smoothing using averaging mean median max
and min filters show the difference(Marks=1.5)
def print_name():
print("Haisam Abbas: 23108406")
def apply_smoothing(image_path):
print_name()
# Read and convert image
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Apply filters
# Average filter
avg_kernel = np.ones((5,5))/25
avg_filtered = cv2.filter2D(img, -1, avg_kernel)
# Mean filter
mean_filtered = cv2.blur(img, (5,5))
# Median filter
median_filtered = cv2.medianBlur(img, 5)
# Max filter
max_filtered = ndimage.maximum_filter(img, size=5)
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University
# Min filter
min_filtered = ndimage.minimum_filter(img, size=5)
# Display results
plt.figure(figsize=(20, 10))
for i in range(len(images)):
plt.subplot(2, 3, i+1)
plt.imshow(images[i])
plt.title(titles[i])
plt.axis('off')
plt.tight_layout()
plt.show()
# Run Q1
print_name()
# Replace 'your_image1.jpg' with your image path
apply_smoothing('/content/boats-in-lagoon-with-noise.jpg')
Output:
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University
Q2: Take a single image of your choice and perform sharpening using pewit,
and sobel operators and DoG(Marks=1.5)
Answer:
# Q2: Image Sharpening Operations
import cv2
import numpy as np
import matplotlib.pyplot as plt
def print_name():
print("Haisam Abbas: 23108406")
def apply_sharpening(image_path):
print_name()
# Read and convert image
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Prewitt operator
kernelx = np.array([[-1,0,1], [-1,0,1], [-1,0,1]])
kernely = np.array([[-1,-1,-1], [0,0,0], [1,1,1]])
prewitt_x = cv2.filter2D(gray, -1, kernelx)
prewitt_y = cv2.filter2D(gray, -1, kernely)
prewitt = cv2.addWeighted(np.absolute(prewitt_x), 0.5,
np.absolute(prewitt_y), 0.5, 0)
# Sobel operator
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
sobel = np.sqrt(sobelx**2 + sobely**2)
sobel = np.uint8(sobel)
# Display results
plt.figure(figsize=(20, 5))
for i in range(len(images)):
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University
plt.subplot(1, 4, i+1)
plt.imshow(images[i], cmap='gray')
plt.title(titles[i])
plt.axis('off')
plt.tight_layout()
plt.show()
# Run Q2
print_name()
# Replace 'your_image2.jpg' with your image path
apply_sharpening('/content/Valve_original_(1).png')
Output:
Q3: After applying the above operations describe the conclusion of each
operation what changes are observed after applying each operation. (Marks=1)
Answer:
Smoothing Filters Analysis
1. Average Filter
2. Mean Filter
o Computationally efficient
o Good for Gaussian noise reduction
o Better edge preservation than average filter
Cons:
o Still causes some edge blurring
o May not preserve fine details
Best Use Case: Quick noise reduction with moderate edge preservation
Observations:
o Faster processing compared to more complex filters
o Balanced approach between noise reduction and detail preservation
3. Median Filter
4. Max Filter
5. Min Filter
2. Sobel Operator
Summary of Changes
After applying these operations, the following key changes were observed:
1. Smoothing Operations:
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University
The End