-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise.py
executable file
·71 lines (58 loc) · 1.75 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/env python
import numpy as np
import imageio
def gaussian(image):
"""
Adds gaussian noise to the image.
"""
row, col = image.shape
mean = 0
var = 0.1
sigma = var**0.5
gauss = np.random.normal(mean, sigma, (row, col))
gauss = gauss.reshape(row, col)
noisy = image + gauss
return noisy
def poisson(image):
vals = len(np.unique(image))
vals = 2 ** np.ceil(np.log2(vals))
noisy = np.random.poisson(image * vals) / float(vals)
return noisy
# this one adds too much noise
def speckle(image):
row, col = image.shape
gauss = np.random.randn(row, col)
gauss = gauss.reshape(row, col)
noisy = image + image * gauss
return noisy
def saltPepper(image):
row, col = 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
from os import listdir
def augmentDir(path):
for filename in listdir(path):
im_base = imageio.imread(path + filename)
im_1 = np.uint8(gaussian(im_base))
im_2 = np.uint8(poisson(im_base))
im_3 = np.uint8(saltPepper(im_base))
imageio.imwrite(path + 'aug1_' + filename, im_1)
imageio.imwrite(path + 'aug2_' + filename, im_2)
imageio.imwrite(path + 'aug3_' + filename, im_3)
return None
# augment the data present in the training folder
augmentDir('data/train/pos/')
augmentDir('data/train/neg/')
print('Done')