AliCV A2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Name: ALI RAZZAQ

# ID: F2020266307

Assignment: 2 Computer Vision

Question # 1:
Step 1: Importing Libraries and Upload the Image

from google.colab import files


from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

# Upload an image file


uploaded = files.upload()
filename = next(iter(uploaded)) # Get the filename of the uploaded
image

<IPython.core.display.HTML object>

Saving images.jpeg to images.jpeg

Step 2: Image to a Grayscale Array

# Open the original image


with Image.open(filename) as img:
# Convert to grayscale
gray = img.convert('L')
img_array = np.array(gray)

# Display both original and grayscale images


fig, axs = plt.subplots(1, 2, figsize=(12, 6))

# Display the original image


axs[0].imshow(img) # img is the original image
axs[0].set_title('Original Image')
axs[0].axis('off') # Turn off axis numbers and labels

# Display the grayscale image


axs[1].imshow(img_array, cmap='gray')
axs[1].set_title('Grayscale Image')
axs[1].axis('off') # Turn off axis numbers and labels

plt.show()
Step 4: Applying Thresholding

# Thresholding
thresh_value = 128
height, width = img_array.shape
thresholded = np.zeros((height, width), dtype=np.uint8)

for i in range(height):
for j in range(width):
thresholded[i, j] = 255 if img_array[i, j] > thresh_value else
0

Step 5: Applying Erosion

# Manual Erosion
kernel_size = 3
eroded = np.zeros((height, width), dtype=np.uint8)

for i in range(height - kernel_size + 1):


for j in range(width - kernel_size + 1):
if np.all(thresholded[i:i + kernel_size, j:j + kernel_size] ==
255):
eroded[i + kernel_size // 2, j + kernel_size // 2] = 255

Step 6: Applying Dilation

# Dilation
dilated = np.zeros((height, width), dtype=np.uint8)

for i in range(height - kernel_size + 1):


for j in range(width - kernel_size + 1):
if np.any(thresholded[i:i + kernel_size, j:j + kernel_size] ==
255):
dilated[i + kernel_size // 2, j + kernel_size // 2] = 255

Step 7: Displaying the Processed Images


# Display the original, thresholded, eroded, and dilated images
fig, axs = plt.subplots(1, 4, figsize=(20, 5))
axs[0].imshow(img, cmap='gray')
axs[0].set_title('Original Image')
axs[1].imshow(thresholded, cmap='gray')
axs[1].set_title('Thresholded Image')
axs[2].imshow(eroded, cmap='gray')
axs[2].set_title('Eroded Image')
axs[3].imshow(dilated, cmap='gray')
axs[3].set_title('Dilated Image')
plt.show()

Question # 2:
Step 1: Set Up Google Colab and Mount Google Drive

from google.colab import drive


drive.mount('/content/drive')

Drive already mounted at /content/drive; to attempt to forcibly


remount, call drive.mount("/content/drive", force_remount=True).

Step 2: Read the Image and Convert to Grayscale

from PIL import Image


import numpy as np
import matplotlib.pyplot as plt

img_path = ('/content/drive/My Drive/images.jpeg')

# Open the image and convert to grayscale


with Image.open(img_path) as img:
grayscale = img.convert('L')
image_array = np.array(grayscale)

# Display both original and grayscale images


fig, axs = plt.subplots(1, 2, figsize=(12, 6))

# Display the original image


axs[0].imshow(img) # img is the original image
axs[0].set_title('Original Image')
axs[0].axis('off') # Turn off axis numbers and labels

# Display the grayscale image


axs[1].imshow(image_array, cmap='gray')
axs[1].set_title('Grayscale Image')
axs[1].axis('off') # Turn off axis numbers and labels

plt.show()

Step 3: Apply Smoothing and Blurring

# kernel size
k = 3

height, width = image_array.shape


blurred_image = np.zeros((height, width), dtype=np.uint8)

# Averaging filter
for i in range(k, height - k):
for j in range(k, width - k):
# Average of neighborhood
sum_pixels = 0
for di in range(-k, k + 1):
for dj in range(-k, k + 1):
sum_pixels += image_array[i + di, j + dj]
blurred_image[i, j] = sum_pixels // (kernel_size *
kernel_size)

# Display both original and blurred image


fig, axs = plt.subplots(1, 2, figsize=(12, 6))

# Display the original image


axs[0].imshow(img) # img is the original image
axs[0].set_title('Original Image')
axs[0].axis('off') # Turn off axis numbers and labels
# Display the grayscale image
axs[1].imshow(blurred_image, cmap='gray')
axs[1].set_title('Blured image')
axs[1].axis('off') # Turn off axis numbers and labels

plt.show()

Step 4: Edge Detection

# Sobel kernels
sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])

# Initialize the gradient arrays


gradient_x = np.zeros_like(blurred_image, dtype=np.int32)
gradient_y = np.zeros_like(blurred_image, dtype=np.int32)

# Compute gradient in x and y direction


for i in range(1, height - 1):
for j in range(1, width - 1):
region = blurred_image[i - 1:i + 2, j - 1:j + 2]
gradient_x[i, j] = np.sum(region * sobel_x)
gradient_y[i, j] = np.sum(region * sobel_y)

# Magnitude of gradients
gradient_magnitude = np.sqrt(gradient_x**2 + gradient_y**2)
gradient_magnitude = np.clip(gradient_magnitude, 0,
255).astype(np.uint8)

# Display both original image and edge-detected image


fig, axs = plt.subplots(1, 2, figsize=(12, 6))

# Display the original image


axs[0].imshow(img) # img is the original image
axs[0].set_title('Original Image')
axs[0].axis('off') # Turn off axis numbers and labels
# Display the Edge Detection
plt.imshow(gradient_magnitude, cmap='gray')
plt.title('Edge Detected Image')
plt.axis('off')
plt.show()

You might also like