-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnoise_image_generate.py
33 lines (27 loc) · 972 Bytes
/
noise_image_generate.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
import numpy as np
import imageio
def im2double(im):
info = np.iinfo(im.dtype)
return im.astype(np.double) / info.max
def write(im, filename):
img = np.copy(im)
img = img.squeeze()
if img.dtype == np.double:
#img = np.array(img*255, dtype=np.uint8)
img = img * np.iinfo(np.uint8).max
img = img.astype(np.uint8)
imageio.imwrite(filename, img)
samples = {'A': 0.6}
for imgName, noiseRatio in samples.items():
Img = im2double(imageio.imread('{}.png'.format(imgName)))
Img[(Img == 0)] = 0.01
rows, cols, channels = Img.shape
noiseMask = np.ones((rows, cols, channels))
subNoiseNum = round(noiseRatio * cols)
for k in range(channels):
for i in range(rows):
tmp = np.random.permutation(cols)
noiseIdx = np.array(tmp[:subNoiseNum])
noiseMask[i, noiseIdx, k] = 0
noiseImg = Img * noiseMask
write(im=noiseImg, filename='{}_noise.png'.format(imgName))