Project 2
#read the image
#define the noise
#apply the noise
#create and apply the noise filter
#read the image to detect the edges
#convert into grey scale image
#apply the first order filter(prewit filter)
#apply the second order filter (laplacian filter)
import cv2 #importing cv2 and numpy libraries
import numpy as np
def prewit_filter(image): #defining prewit filter with vertical and horizontal masks
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)
print(vertical_filtered)
print(abs_grad_x)
abs_grad_y = cv2.convertScaleAbs(horizontal_filtered) #converting the convoluted values into integers
grad = cv2.addWeighted(abs_grad_x,1,abs_grad_y,0.5,0) #combining vertical and horizontal filtered image
cv2.imshow('x',abs_grad_x)
cv2.imshow('y',abs_grad_y)
return grad
image = cv2.imread('bmw.jpg')
gray_image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
filtered_gray_image=prewit_filter(gray_image)
cv2.imshow('prewit_image',filtered_gray_image)
cv2.waitKey(0)
image = cv2.imread('bmw.jpg')
gray_image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
filtered_image = cv2.GaussianBlur(gray_image,(3,3,),0)
edged_image = cv2.Laplacian(filtered_image,3,0)
abs_grad_edged_image = cv2.convertScaleAbs(edged_image)
cv2.imshow('alaplace',abs_grad_edged_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Comparison:
By using Prewit filter , noise in the background is captured more. using the single derivative operator the magnitude variation in the image is also captured instead of edges.
by using laplace filter more noise is removed and edges are captured properly, doesnt require two filters to combine with respect to horizontal and vertical.