AliCV A2
AliCV A2
AliCV A2
# ID: F2020266307
Question # 1:
Step 1: Importing Libraries and Upload the Image
<IPython.core.display.HTML object>
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
# Manual Erosion
kernel_size = 3
eroded = np.zeros((height, width), dtype=np.uint8)
# Dilation
dilated = np.zeros((height, width), dtype=np.uint8)
Question # 2:
Step 1: Set Up Google Colab and Mount Google Drive
plt.show()
# kernel size
k = 3
# 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)
plt.show()
# 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]])
# Magnitude of gradients
gradient_magnitude = np.sqrt(gradient_x**2 + gradient_y**2)
gradient_magnitude = np.clip(gradient_magnitude, 0,
255).astype(np.uint8)