Kernal implementaion Exp-7
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('butterfly.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# Robert Edge Detection
kernelxx = np.array([[1, 0], [0, -1]])
kernelyy = np.array([[0, 1], [-1, 0]])
img_robertx = cv2.filter2D(img, -1, kernelxx)
img_roberty = cv2.filter2D(img, -1, kernelyy)
grad = cv2.addWeighted(img_robertx, 0.5, img_roberty, 0.5, 0)
plt.imshow(grad,cmap='gray')
plt.title("Robert Edge Detection")
plt.axis('off')
# Laplacian
laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(src=blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=3)
sobely = cv2.Sobel(src=blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=3)
fig, axs = plt.subplots(2, 2)
titles = ['Original', 'Sobel along X-axis', 'Sobel along Y-axis', 'Laplacian']
axs[0, 0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
axs[0, 0].set_title(titles[0])
axs[0, 0].axis('off')
axs[0, 1].imshow(laplacian, cmap='gray')
axs[0, 1].set_title(titles[3])
axs[0, 1].axis('off')
axs[1, 0].imshow(sobelx, cmap='gray')
axs[1, 0].set_title(titles[1])
axs[1, 0].axis('off')
axs[1, 1].imshow(sobely, cmap='gray')
axs[1, 1].set_title(titles[2])
axs[1, 1].axis('off')
Comments
Post a Comment