-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_noise.py
64 lines (51 loc) · 1.63 KB
/
data_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
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 28 18:54:04 2019
@author: 95370
"""
import numpy as np
import math
from matplotlib import pyplot as plt
import cv2
from sklearn.cluster import KMeans
import random
import skimage
def sp_noise(image,prob):
'''
添加椒盐噪声
prob:噪声比例
'''
output = np.zeros(image.shape,np.uint8)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = image[i][j]
return output
data = np.load('./xdata_128x128.npy')
#------------------------------ data_sp ------------------------------#
data2 = np.zeros(shape=(13434,128,128,1))
for i in range(13434):
print(i)
data2[i,:,:,:] = sp_noise(data[i,:,:,:],0.01)
np.save('xdata_128x128_sp_0.01.npy',data2)
#------------------------------ data_gaussian ------------------------------#
data2 = np.zeros(shape=(13434,128,128,1))
data = data/255
for i in range(13434):
print(i)
img_noise=skimage.util.random_noise(data[i,:,:,0], mode='gaussian', seed=None, var=(1/255.0)**2)
data2[i,:,:,0]=img_noise*255
np.save('xdata_128x128_gaussian_1.npy',data2)
#------------------------------ data_poisson ------------------------------#
data2 = np.zeros(shape=(13434,128,128,1))
for i in range(13434):
print(i)
img_noise=skimage.util.random_noise(data[i,:,:,0], mode='poisson', seed=None,clip=True)
data2[i,:,:,0]= img_noise
np.save('xdata_128x128_poisson.npy',data2)