22aie313 Lab2 22125
22aie313 Lab2 22125
Prewitt Filters
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('/content/einstein-8041625_1280.webp')
image = cv2.resize(image, (500, 500))
for i, ax in enumerate(axes):
if len(images[i].shape) == 2:
ax.imshow(images[i], cmap='gray')
else:
ax.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
ax.set_title(titles[i])
ax.axis("off")
plt.tight_layout()
plt.show()
Sobel Filter
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('/content/einstein-8041625_1280.webp')
image = cv2.resize(image, (500, 500))
for i, ax in enumerate(axes):
if len(images[i].shape) == 2:
ax.imshow(images[i], cmap='gray')
else:
ax.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
ax.set_title(titles[i])
ax.axis("off")
plt.tight_layout()
plt.show()
Laplacian Filter
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('/content/einstein-8041625_1280.webp')
image = cv2.resize(image, (500, 500))
for i, ax in enumerate(axes):
if len(images[i].shape) == 2:
ax.imshow(images[i], cmap='gray')
else:
ax.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
ax.set_title(titles[i])
ax.axis("off")
plt.tight_layout()
plt.show()
Robinson Compass
import cv2
import numpy as np
import matplotlib.pyplot as plt
robinson_masks = [
np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]),
np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]),
np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]),
np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]),
np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]),
np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]),
np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]),
np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
]
image = cv2.imread('/content/einstein-8041625_1280.webp')
image = cv2.resize(image, (500, 500))
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
filtered_images = []
for mask in robinson_masks:
filtered_image = cv2.filter2D(gray_image, -1, mask)
filtered_images.append(filtered_image)
for i, ax in enumerate(axes):
if i < len(images):
if len(images[i].shape) == 2:
ax.imshow(images[i], cmap="gray")
else:
ax.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
ax.set_title(titles[i])
ax.axis("off")
else:
ax.axis("off")
plt.tight_layout()
plt.show()
Kirsch Compass
import cv2
import numpy as np
import matplotlib.pyplot as plt
kirsch_masks = [
np.array([[ 5, 5, 5], [-3, 0, -3], [-3, -3, -3]]),
np.array([[ 5, 5, -3], [ 5, 0, -3], [-3, -3, -3]]),
np.array([[ 5, -3, -3], [ 5, 0, -3], [ 5, -3, -3]]),
np.array([[-3, -3, -3], [ 5, 0, -3], [ 5, 5, -3]]),
np.array([[-3, -3, -3], [-3, 0, -3], [ 5, 5, 5]]),
np.array([[-3, -3, -3], [-3, 0, 5], [-3, 5, 5]]),
np.array([[-3, -3, 5], [-3, 0, 5], [-3, -3, 5]]),
np.array([[-3, 5, 5], [-3, 0, 5], [-3, -3, -3]])
]
image = cv2.imread('/content/einstein-8041625_1280.webp')
image = cv2.resize(image, (500, 500))
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
filtered_images = []
for mask in kirsch_masks:
filtered_image = cv2.filter2D(gray_image, -1, mask)
filtered_images.append(filtered_image)
for i, ax in enumerate(axes):
if i < len(images):
if len(images[i].shape) == 2:
ax.imshow(images[i], cmap="gray")
else:
ax.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
ax.set_title(titles[i])
ax.axis("off")
else:
ax.axis("off")
plt.tight_layout()
plt.show()
Canny's Edge Detection
import numpy as np
import cv2
import matplotlib.pyplot as plt
image = cv2.imread('/content/einstein-8041625_1280.webp')
image = cv2.resize(image, (500, 500))
STEP 1: Smoothening
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
guassian_blur = cv2.GaussianBlur(gray_image, (5,5), 1)
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
for i, ax in enumerate(axes):
if len(images[i].shape) == 2:
ax.imshow(images[i], cmap='gray')
else:
ax.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
ax.set_title(titles[i])
ax.axis("off")
plt.tight_layout()
plt.show()
for i, ax in enumerate(axes):
if len(images[i].shape) == 2:
ax.imshow(images[i], cmap='gray')
else:
ax.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
ax.set_title(titles[i])
ax.axis("off")
plt.tight_layout()
plt.show()
for i, ax in enumerate(axes):
if i == 2:
ax.imshow(images[i], cmap='hsv')
elif len(images[i].shape) == 2:
ax.imshow(images[i], cmap='gray')
ax.set_title(titles[i])
ax.axis("off")
plt.tight_layout()
plt.show()
return suppressed
suppressed_image = non_max_suppression(gradient_magnitude,
gradient_direction)
def hysteresis_thresholding(suppressed_image,
low_threshold_ratio=0.01, high_threshold_ratio=0.09):
high_threshold = suppressed_image.max() * high_threshold_ratio
low_threshold = high_threshold * low_threshold_ratio
result[strong_edges] = 255
for i in range(1, rows - 1):
for j in range(1, cols - 1):
if weak_edges[i, j]:
if np.any(strong_edges[i-1:i+2, j-1:j+2]):
result[i, j] = 255
return result
hysteresis_image = hysteresis_thresholding(suppressed_image)