-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadding_noise.py
80 lines (62 loc) · 2.2 KB
/
adding_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
72
73
74
75
76
77
78
79
80
import matplotlib.pyplot as plt
import numpy as np
from skimage import data, img_as_float
path = '/home/habray/Image_Processing/Working_Folder/image/'
img = plt.imread(path + 'panda.jpg')
def gaussian_noise(image):
row,col = image.shape
mean = 0
var = 500
sigma = var**0.5
gauss = np.random.normal(mean,sigma,(row,col))
gauss = gauss.reshape(row,col)
noisy = image + gauss
return noisy
def salt_pepper_noise(image):
s_vs_p = 0.5
amount = 0.2
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
def plot_img_and_hist(image, axes, bins=256):
# Plot an image along with its histogram and cumulative histogram.
image = img_as_float(image)
ax_img, ax_hist = axes
# Display image
ax_img.imshow(image, cmap="gray")
ax_img.set_axis_off()
# Display histogram
ax_hist.hist(image.ravel(), bins=bins, histtype='barstacked', color='black')
ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
ax_hist.set_xlabel('Pixel intensity')
ax_hist.set_xlim(0, 1)
ax_hist.set_yticks([])
return ax_img, ax_hist
fig = plt.figure(figsize=(8, 5))
axes = np.zeros((2, 4), dtype=np.object)
axes[0, 0] = fig.add_subplot(2, 3, 1)
for i in range(1, 3):
axes[0, i] = fig.add_subplot(2, 3, 1+i, sharex=axes[0,0], sharey=axes[0,0])
for i in range(0, 3):
axes[1, i] = fig.add_subplot(2, 3, 4+i)
ax_img, ax_hist = plot_img_and_hist(img, axes[:, 0])
ax_img.set_title('Original image')
y_min, y_max = ax_hist.get_ylim()
ax_hist.set_ylabel('Number of pixels')
ax_hist.set_yticks(np.linspace(0, y_max, 5))
ax_img, ax_hist = plot_img_and_hist(gaussian_noise(img), axes[:, 1])
ax_img.set_title('Gaussian Noise')
ax_img, ax_hist = plot_img_and_hist(salt_pepper_noise(img), axes[:, 2])
ax_img.set_title('Salt & Pepper Noise')
# prevent overlap of y-axis labels
fig.tight_layout()
plt.show()