-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_noise.py
47 lines (36 loc) · 1.12 KB
/
add_noise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import numpy as np
def salt_and_pepper(image,var):
row,col,ch = image.shape
s_vs_p = 0.5
amount = var
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] = 255
# 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
def gaussian_noise(image,var):
row,col,ch = image.shape
#print("here",row,col)
mean = 0
sigma = var**0.5
gauss = np.random.normal(mean,sigma,(row,col,ch))
gauss = gauss.reshape(row,col,ch)
noisy = image + gauss
return noisy
def img_noise(img,var):
out1 = gaussian_noise(img, var)
out2 = salt_and_pepper(img, var)
cv2.imwrite("img/gauss_"+str(var)+".jpg", out1)
cv2.imwrite("img/snp_"+str(var)+".jpg", out2)
import cv2
img = cv2.imread('example.jpg')
arr = [0.001,0.01,0.1,0.2]
for i in arr:
img_noise(img,i)