Project 2
#First i have imported the needed libraries.
import cv2 as cv
import numpy as np
#I Declared the Needed variables.
ddebth = -1
ksize = 3
data_type = "uint8"
image = cv.imread("/home/dreamer/Pictures/BMW-White.jpg")
print(image.shape)
sp_noise_window = "Salt & Peper Noise"
mean_window = "Original Image with Noisy Image & Mean Filter Image"
median_window = "Original Image with Noisy Image & Median Filter Image"
gaussian_window = "Original Image with Noisy Image & gaussian Filter Image"
laplacian_window = "Original Image & gaussian Filter Image with Laplacian Operator"
sobel_window = "Original Image & gaussian Filter Image with Sobel Edge Detection"
prewitt_window = "Original Image & gaussian Filter Image with Prewit Edge Detection"
#created the salt & peper noise
def sp_noise(image):
output = image.copy()
if len(image.shape) == 2:
black = 0
white = 255
else:
black = np.array([0,0,0], dtype = data_type)
white = np.array([255,255,255], dtype = data_type)
random_threshold = np.random.random(image.shape[:2])
output[random_threshold<(random_threshold/2)] = black
output[random_threshold>1-(random_threshold/2)] = white
return output
salt_and_peper_noisy_image = sp_noise(image)
noisy_image = salt_and_peper_noisy_image
cv.imshow(sp_noise_window, np.hstack((image, noisy_image)))
cv.waitKey(0)
cv.destroyAllWindows()
#mean Filter
def mean_filter(image,filter_size):
mean_filter = np.ones((filter_size,filter_size),np.float32)/filter_size*filter_size
mean_filtered_image = cv.filter2D(image,ddebth,mean_filter)
return mean_filtered_image
mean_filtered_image = mean_filter(noisy_image,3)
cv.imshow(mean_window,np.hstack((image,noisy_image,mean_filtered_image)))
cv.waitKey(0)
cv.destroyAllWindows()
# median filter
def median_filter(image,filter_size):
median_filtered_image = cv.medianBlur(image,filter_size)
return median_filtered_image
median_filtered_image = median_filter(noisy_image,5)
cv.imshow(median_window,np.hstack((image,noisy_image,median_filtered_image)))
cv.waitKey(0)
cv.destroyAllWindows()
#gaussian filter
def gaussian_filter(image,filter_size,sigma):
gaussian_filtered_image = cv.GaussianBlur(image,(filter_size,filter_size),sigma)
return gaussian_filtered_image
gaussian_filtered_image = gaussian_filter(noisy_image,3,5)
cv.imshow(gaussian_window,np.hstack((image,noisy_image,gaussian_filtered_image)))
cv.waitKey(0)
cv.destroyAllWindows()
#sobel operator
def sobel_edge(image):
vertical_filter = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
horizontal_filter = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
vertical_filtered = cv.filter2D(image,ddebth,vertical_filter)
horizontal_filtered = cv.filter2D(image,ddebth,horizontal_filter)
abs_grad_x = cv.convertScaleAbs(vertical_filtered)
abs_grad_y = cv.convertScaleAbs(horizontal_filtered)
grad = cv.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
return grad
gaussian_filtered_image_without_noise = gaussian_filter(image,3,5)
gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
sobel_edge_detector = sobel_edge(gray_image)
cv.imshow(sobel_window,np.hstack((gray_image,sobel_edge_detector)))
cv.waitKey(0)
cv.destroyAllWindows()
#laplace operator
def laplace_operator(image,ddebth,ksize):
laplacian_edge_detector = cv.Laplacian(image,ddebth,ksize)
return laplacian_edge_detector
gaussian_filtered_image = gaussian_filter(image,3,5)
laplacian_edge_detector = laplace_operator(gaussian_filtered_image,ddebth,ksize)
cv.imshow(laplacian_window,np.hstack((noisy_image,gaussian_filtered_image,laplacian_edge_detector)))
cv.waitKey(0)
cv.destroyAllWindows()
#prewitt operator
def prewitt_edge(image):
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 = cv.filter2D(image,ddebth,vertical_filter)
horizontal_filtered = cv.filter2D(image,ddebth,horizontal_filter)
abs_grad_x = cv.convertScaleAbs(vertical_filtered)
abs_grad_y = cv.convertScaleAbs(horizontal_filtered)
grad = cv.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
return grad
gaussian_filtered_image = gaussian_filter(image,3,5)
gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
prewitt_edge_detector = prewitt_edge(gray_image)
cv.imshow(prewitt_window,np.hstack((gray_image,prewitt_edge_detector)))
cv.waitKey(0)
cv.destroyAllWindows()
first i have imported the needed libraries. then i decleared the needed variables.
#created salt and peper noise
The code creates a black and white noise image. The code creates the black and white noise by copying the input image, then setting all pixels to 0 for black or 255 for white.The code creates a random threshold, which is between 0 and 1, to determine what color to use for each pixel. If the value of the random_threshold is less than half of its maximum value, then it will be black; otherwise, it will be white.
#mean filter
This code takes the image and returns an mean filtered image of it.The code takes the input image, filter it with a particular size, and returns the filtered image.

#median filter
This function is takes an image and a filter size as input. The code first creates the median filtered image by using cv.medianBlur() on the original image, then returns it as the result of the function.

#gaussian filter
This function that takes an image and a filter size as input, then applies the Gaussian Blur filter to the image. The code returns the result of applying the Gaussian blur filter to an image.

#sobel filter
The code starts by creating two arrays, vertical filter and horizontal filter.These are then used to create a 2D convolutional filter which is applied to the original image.Next, gradients are calculated from each filtered image and added together to get the final result.
The first Two lines are creates an array.This is then used as a vertical filter and horizontal filter for our convolutional layer.Finally we use these filters on our original image using cv's convertScaleAbs method to calculate gradients from each filtered version of the image before adding them together into one value at last step.Next, it adds these two values together and multiplies them by 0.5 to create a weighted sum with a weighting of 0.5 on both axes.
#laplace operator
this function that takes in three parameters: image, ddebth and ksize.The first parameter is the input image and the second parameter is the image debth of color and third parameter is size of the Laplacian kernel. The code filtering the image using Laplacian metho and returns it.

#prewitt filter
this filter is same as sobel filter. The code starts by creating two arrays, vertical filter and horizontal filter. These arrays are used to create the filter for the image that will be passed into cv.filter2D(). Next, this new matrix of gradients is added together using cv.addWeighted().Finally, these filtered images are added together to create one final filtered image that has been filtered by both sets of prewitt edges.

#Comparision of laplacian filter and prewit filter
Prewitt operator is similar to the Sobel operator. The edge obtained by the prewitt operator is slightly clearer. There is no significant change when increasing the Gaussian filter size. The contour obtained by the Laplacian operator appears thin. Also, when increasing the size of the Gaussian filter, the edge of the image becomes less obvious. Therefore, it appears that the Prewitt filter is better than the Laplacian filter.