0% found this document useful (0 votes)
2 views

Assignment 2

Uploaded by

Haisam Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 2

Uploaded by

Haisam Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

ROBOTICS & ARTIFICIAL INTELLIGENCE DEPARTMENT

Total Marks: 04

Obtained Marks:

Computer Vision
Assignment # 02
Last Date of Submission: 25th October 2024

Submitted To: Dr. Muhammad Junaid Umer

Student Name: Haisam Abbas

Reg. Number: 23108406


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

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

Take images of your choice and perform the following task


Q1: Take any image and perform smoothing using averaging mean median max
and min filters show the difference(Marks=1.5)
Q2: Take a single image of your choice and perform sharpening using pewit,
and sobel operators and DoG(Marks=1.5)
Q3: After applying the above operations describe the conclusion of each
operation what changes are observed after applying each operation. (Marks=1)
Answer:

Q1: Take any image and perform smoothing using averaging mean median max
and min filters show the difference(Marks=1.5)

# Q1: Image Smoothing Operations


import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage

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))

images = [img, avg_filtered, mean_filtered, median_filtered,


max_filtered, min_filtered]
titles = ['Original', 'Average Filter', 'Mean Filter', 'Median
Filter', 'Max Filter', 'Min Filter']

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)

# Convert to grayscale for edge detection


gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

# 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)

# Difference of Gaussians (DoG)


gaussian1 = cv2.GaussianBlur(gray, (3,3), 0)
gaussian2 = cv2.GaussianBlur(gray, (15,15), 0)
dog = gaussian1 - gaussian2

# Display results
plt.figure(figsize=(20, 5))

images = [gray, prewitt, sobel, dog]


titles = ['Original Grayscale', 'Prewitt Operator', 'Sobel Operator',
'Difference of Gaussians']

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

 Effect: Creates uniform blur across the image


 Pros:
o Effective for general noise reduction
o Simple implementation
o Uniform smoothing effect
 Cons:
o Can significantly blur edges
o Loss of fine details
 Best Use Case: When uniform smoothing is desired
 Observations:
o Reduces random noise effectively
o Works well for images with subtle texture variations

2. Mean Filter

 Effect: Similar to average filter but with better edge preservation


 Pros:
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

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

 Effect: Removes noise while preserving edges better than mean/average


 Pros:
o Excellent for salt-and-pepper noise removal
o Better edge preservation
o Good for removing outliers
 Cons:
o Can remove fine image details
o Slower than mean filter
 Best Use Case: Images with impulse noise (salt-and-pepper)
 Observations:
o Very effective for removing random sharp noise
o Maintains sharp edges better than linear filters

4. Max Filter

 Effect: Brightens image and enlarges bright regions


 Pros:
o Good for removing dark noise
o Enhances bright features
o Useful for peak detection
 Cons:
o Can cause bright regions to expand
o May create unwanted bright artifacts
 Best Use Case: Removing dark spots or pepper noise
 Observations:
o Particularly effective for dark noise removal
o Can help in highlighting bright features

5. Min Filter

 Effect: Darkens image and enlarges dark regions


 Pros:
o Good for removing bright noise
o Enhances dark features
o Useful for valley detection
 Cons:
o Can cause dark regions to expand
o May create unwanted dark artifacts
 Best Use Case: Removing bright spots or salt noise
 Observations:
o Effective for bright noise removal
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

o Can help in highlighting dark features

Sharpening Operators Analysis


1. Prewitt Operator

 Effect: Detects edges in horizontal and vertical directions


 Pros:
o Simple and effective edge detection
o Computationally efficient
o Good for basic edge enhancement
 Cons:
o More sensitive to noise than Sobel
o Less accurate in diagonal directions
 Best Use Case: Basic edge detection in low-noise images
 Observations:
o Produces thicker edges compared to Sobel
o Works well for clear, well-defined edges

2. Sobel Operator

 Effect: Enhanced edge detection with better noise handling


 Pros:
o Better noise resistance than Prewitt
o More accurate edge detection
o Good directional sensitivity
 Cons:
o May miss some subtle edges
o Slightly more complex computation
 Best Use Case: General-purpose edge detection
 Observations:
o Produces cleaner edges than Prewitt
o Better performance in noisy conditions

3. Difference of Gaussians (DoG)

 Effect: Enhances edges at different scales


 Pros:
o Can detect edges at multiple scales
o Good for feature detection
o Less sensitive to noise
 Cons:
o May produce thicker edges
o Results depend on chosen Gaussian parameters
 Best Use Case: Feature detection and blob detection
 Observations:
o Effective for detecting features of various sizes
o Good balance between edge detection and noise suppression

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

oReduced image noise


oDecreased fine detail visibility
oVarious degrees of edge preservation
oDifferent effectiveness for different types of noise
2. Sharpening Operations:
o Enhanced edge visibility
o Increased contrast at edges
o Different sensitivity to noise and directions
o Various scales of feature detection

The End

You might also like