Skip to content

Instantly share code, notes, and snippets.

@sumit2312
Created December 12, 2020 04:37
Show Gist options
  • Select an option

  • Save sumit2312/5501f531b15cd3cbac78d95f38710b61 to your computer and use it in GitHub Desktop.

Select an option

Save sumit2312/5501f531b15cd3cbac78d95f38710b61 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from math import log10, sqrt
img = cv2.imread('original_boat_img.gif')
def PSNR(original, compressed):
mse = np.mean((original - compressed) ** 2)
if(mse == 0): # MSE is zero means no noise is present in the signal .
# Therefore PSNR have no importance.
return 100
max_pixel = 255.0
psnr = 20 * log10(max_pixel / sqrt(mse))
return psnr
def sp_noise(image,prob):
'''
Add salt and pepper noise to image
prob: Probability of the noise
'''
output = np.zeros(image.shape,np.uint8)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = image[i][j]
return output
image = sp_noise(img,0.20)
# apply the 3x3 mean filter on the image
kernel = np.ones((3,3),np.float32)/9
processed_image = cv2.filter2D(image,-1,kernel)
Y = np.square(np.subtract(image,processed_image)).mean()
print("MSE:", Y)
value = PSNR(image, processed_image)
print("PSNR: " ,value)
# display image
cv2.imshow('Mean Filter Processing', processed_image)
# save image to disk
cv2.imwrite('processed_image.png', processed_image)
# pause the execution of the script until a key on the keyboard is pressed
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment