Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save toshihiroryuu/6518ad57cf9c69b0d046b833618166c7 to your computer and use it in GitHub Desktop.
Save toshihiroryuu/6518ad57cf9c69b0d046b833618166c7 to your computer and use it in GitHub Desktop.
# Parameters
# image : ndarray
# Input image data. Will be converted to float.
# mode : str
# One of the following strings, selecting the type of noise to add:
# 'gauss' Gaussian-distributed additive noise.
# 'poisson' Poisson-distributed noise generated from the data.
# 'saltandpepper' Replaces random pixels with 0 or 1.
# 'speckle' Multiplicative noise using out = image + n*image,where
# n is uniform noise with specified mean & variance.
import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
from PIL import Image
def noisy(noise_typ,image):
if noise_typ == "gauss":
row,col,ch= image.shape
mean = 0
var = 0.1
sigma = var**0.5
gauss = np.random.normal(mean,sigma,(row,col,ch))
gauss = gauss.reshape(row,col,ch)
noisy = image + gauss
return noisy
elif noise_typ == "saltandpepper":
row,col,ch = image.shape
s_vs_p = 0.5
amount = 0.004
out = np.copy(image)
# Salt mode
num_salt = np.ceil(amount * image.size * s_vs_p)
coords = [np.random.randint(0, i - 1, int(num_salt))
for i in image.shape]
out[coords] = 1
# Pepper mode
num_pepper = np.ceil(amount* image.size * (1. - s_vs_p))
coords = [np.random.randint(0, i - 1, int(num_pepper))
for i in image.shape]
out[coords] = 0
return out
elif noise_typ == "poisson":
vals = len(np.unique(image))
vals = 2 ** np.ceil(np.log2(vals))
noisy = np.random.poisson(image * vals) / float(vals)
return noisy
elif noise_typ =="speckle":
row,col,ch = image.shape
gauss = np.random.randn(row,col,ch)
gauss = gauss.reshape(row,col,ch)
noisy = image + image * gauss
return noisy
img = Image.open('/home/cat.jpg')
img = np.array(img)
img_noisy=noisy("saltandpepper",img)
plt.imshow(img_noisy)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment