Computer vision filter implemetation

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Assignment 1

Computer Vision

from google.colab import files


from IPython.display import Image
import numpy as np
import cv2
from matplotlib import pyplot as plt
import math

uploaded = files.upload()

figsize = (10, 10)


def plot_im(img, title):
plt.figure(figsize=figsize)
plt.imshow(img)
plt.title(title)
plt.xticks([])
plt.yticks([])
plt.show()
img = cv2.imread("new Passport pic.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plot_im(img, "original image")

def s_and_p_noise(image, s_p_ratio=0.04):


# this implementation is not entirely correct because it assumes that
# only 0 OR 255 values are S&P noise.
out = np.copy(image)

# Salt mode
mask = np.random.rand(image.shape[0], image.shape[1]) <= s_p_ratio / 2
out[mask] = 255

# Pepper mode
mask = np.random.rand(image.shape[0], image.shape[1]) <= s_p_ratio / 2
out[mask] = 0
return out

def gauss_noise(image, gauss_var=1000):


mean = 0
sigma = gauss_var ** 0.5
gauss = np.random.normal(mean, sigma, image.shape)

res = image + gauss


noisy = np.clip(res, 0, 255).astype(np.uint8)
return noisy
#############################################
#############################################
#following Code is for Average Filter
#############################################
#############################################

avg_filter = np.array([[ 1/25,1/25,1/25,1/25,1/25,],


[ 1/25,1/25,1/25,1/25,1/25,],
[ 1/25,1/25,1/25,1/25,1/25,],
[ 1/25,1/25,1/25,1/25,1/25,],
[ 1/25,1/25,1/25,1/25,1/25,]], np.float32)

#############################################
#############################################
#following Code is for Weighted Average Filter
#############################################
#############################################

add_W_avg_fil = np.array([[1, 4, 6, 4, 1],


[4, 16, 24, 16, 4],
[6, 24, 36, 24, 6],
[4, 16, 24, 16, 4],
[1, 4, 6, 4, 1]], np.float32)/256

#############################################
#############################################
#following Code is for Gaussian Filter
#############################################
#############################################

def gausian(X, y, siguma):


# exp(-(x2 + y2)/2σ2)
h2 = math.e**(-1*((X**2) + (y**2))/(2*(siguma**2)))
# 2πσ2
h3 = 2*math.pi*(siguma**2)
h = h2/h3
return h

def gausian_kernel(size):
if size%2==0:
print('kernel size should be odd')
return
sigma = (size-1)/2

# [0,size]→[-sigma, sigma]Shift
x = y = np.arange(0,size) - sigma
X,Y = np.meshgrid(x,y)
print(sigma)

mat = gausian(X,Y,sigma)

#So that the sum is 1


kernel = mat / np.sum(mat)
return mat

g_fil_hand = gausian_kernel(3)
#Filter shape
#[[0.05854983 0.09653235 0.05854983]
#[0.09653235 0.15915494 0.09653235]
#[0.05854983 0.09653235 0.05854983]]

#############################################
#############################################
#following Code is for Median Filter
#############################################
#############################################

def median_filter(img, k_sz):


res = cv2.medianBlur(img, k_sz)
plot_im(res, "median filtered Image")
#applying Salt and pepper noise
s_p_noise_img = s_and_p_noise(img, s_p_ratio=0.04)
plot_im(s_p_noise_img, "S&P image")
#applying Average(Mean) Filter:-
# depth = -1, means destination image has depth same as input image
avg_filtered_img = cv2.filter2D(s_p_noise_img, -1, avg_filter)
plot_im(avg_filtered_img, "avg filtered image")
#applying Weighted Average Filter:-
Weighted_avg_filter_img = cv2.filter2D(s_p_noise_img, -1, add_W_avg_fil)
plot_im(Weighted_avg_filter_img, "Weighted avg filtered image")
#applying Gaussian Filter:-
Gaussian_filtered_img = cv2.filter2D(s_p_noise_img, -1, g_fil_hand)
plot_im(Gaussian_filtered_img, "Gausian filtered image")
#applying Median Filter:-
median_filter(s_p_noise_img, 5)

Figure 1 Salt & Pepper Noise Image Figure 2 Average Filtered image Figure 3 Weighted Average Filtered Image
Figure 4 Gaussian Filtered Image Figure 5 Median Filtered Image

Comparison

#applying Gaussian noise


G_noise_img = gauss_noise(img, gauss_var=500)
plot_im(G_noise_img, "G image")

#applying Average(Mean) Filter:-

# depth = -1, means destination image has depth same as input image
avg_filtered_img = cv2.filter2D(G_noise_img, -1, avg_filter)
plot_im(avg_filtered_img, "avg filtered image")

#applying Weighted Average Filter:-

Weighted_avg_filter_img = cv2.filter2D(G_noise_img, -1, add_W_avg_fil)


plot_im(Weighted_avg_filter_img, "Weighted avg filtered image")

#applying Gaussian Filter:-

Gaussian_filtered_img = cv2.filter2D(G_noise_img, -1, g_fil_hand)


plot_im(Gaussian_filtered_img, "Gausian filtered image")

#applying Median Filter:-


median_filter(G_noise_img, 5)
Comparison:-

Pruwit:

pulu_fil = np.array([[1, 0, -1],


[1, 0, -1],
[1, 0, -1]])

Pruwit_filtered_img = cv2.filter2D(img, -1, 2*pulu_fil)

Sobel:

sobel_fil = np.array([[1, 0, -1],


[2, 0, -2],
[1, 0, -1]])

Sobel_filtered_img = cv2.filter2D(img, -1, sobel_fil)


Canny:

Cannyedges = cv2.Canny(img,0.3, 100, 200)

plot_im(Pruwit_filtered_img, "Pruwit Edge Detector")


plot_im(Sobel_filtered_img, "Sobel Edge Detector")
plot_im(Cannyedges, "Canny Edge Detector")

Comparison:

You might also like