Project 2
Laplacian filtered image provides better image profile. Laplacian filter traces the edges with precision. In Prewitt filtered image the some edges are thick and edge detection is not uniform through out the image. So Laplacian filtered image is better than Prewitt filtered image.
import cv2
import numpy as np
#Edge detection using Prewitt filter
def prewitt_filter(image):
gaussian_image = cv2.GaussianBlur(image,(3,3),0)
vertical_filter = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
horizontal_filter = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
vertical_filtered = cv2.filter2D(image,-1,vertical_filter)
horizontal_filtered = cv2.filter2D(image,-1,horizontal_filter)
abs_grad_x = cv2.convertScaleAbs(vertical_filtered)
abs_grad_y = cv2.convertScaleAbs(horizontal_filtered)
grad = cv2.addWeighted(abs_grad_x,1,abs_grad_y,1,0)
return grad
#Edge detection using Laplacian filter
def laplacian_filter(image):
gaussian_image = cv2.GaussianBlur(image,(3,3),0)
laplacian_image = cv2.Laplacian(image,ddepth,kernel_size)
abs_grad = cv2.convertScaleAbs(laplacian_image)
return abs_grad
#Reading the image and converting it to gray_image and passing to respective filters
image = cv2.imread('Ch2.png')
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
prewitt_edges = prewitt_filter(gray_image)
ddepth = cv2.CV_16S
kernel_size = 3
laplacian_edges = laplacian_filter(gray_image)
#Saving the images
cv2.imwrite('prewitt_image.png',prewitt_edges)
cv2.imwrite('laplacian_image.png',laplacian_edges)