-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoise_corruption.py
152 lines (138 loc) · 5.41 KB
/
noise_corruption.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import numpy as np
### tools ##
''' Fill vacant intesity of new pc if new points exist, by KNN-distance clustering '''
def fill_intensity(pc, pc_cor_xyz, n_b=5):
# pc:N x 4(xyz+intensity)
# pc_cor_xyz: N+d x 3(xyz)
N, _ = pc.shape
N_all, _ = pc_cor_xyz.shape
if N == N_all:
return np.hstack((pc_cor_xyz, pc[:, 3].reshape([-1,1])))
else:
pc_cor = np.hstack((pc_cor_xyz, np.vstack((pc[:,3].reshape([-1,1]), np.zeros((N_all-N,1))))))
for i in range(N, N_all):
dist = np.sum((pc[:,:3] - pc_cor[i,:3])**2, axis=1, keepdims=True)
idx = np.argpartition(dist, n_b, axis=0)[:n_b]
pc_cor[i,3] = np.sum(pc_cor[idx,3])/n_b
return pc_cor
''' Delete outliers by zscore'''
def del_outlier_axis(data, num=10):
# by samples
z_abs = np.abs(data - data.mean()) / (data.std())
list_ = []
for _ in range(num):
index_max = np.argmax(z_abs)
list_.append(index_max)
z_abs[index_max] = 0
return np.delete(data, list_)
'''convert cartesian coordinates into spherical ones'''
def car2sph_pc(pointcloud):
'''
args:
points: N x 3 : x, y, and z
return:
points: N x 3 : r, phi, and theta
'''
r_sph = np.sqrt(pointcloud[:,0]**2 + pointcloud[:,1]**2 + pointcloud[:,2]**2)
phi = np.arctan2(pointcloud[:,1],pointcloud[:,0])
the = the = np.arccos(pointcloud[:,2]/r_sph)
return np.hstack((r_sph.reshape(-1,1), phi.reshape(-1,1), the.reshape(-1,1)))
'''convert spherical coordinates into cartesian ones'''
def sph2car_pc(pointcloud):
'''
args:
points: N x 3 : r, phi, and theta
return:
points: N x 3 : x, y, and z
'''
x = pointcloud[:,0]*np.sin(pointcloud[:,2])*np.cos(pointcloud[:,1])
y = pointcloud[:,0]*np.sin(pointcloud[:,2])*np.sin(pointcloud[:,1])
z = pointcloud[:,0]*np.cos(pointcloud[:,2])
return np.hstack((x.reshape(-1,1), y.reshape(-1,1), z.reshape(-1,1)))
### Noise ###
'''
Add Uniform noise to point cloud
'''
def uniform_noise(pointcloud, severity):
N, C = pointcloud.shape
c = [0.02, 0.04, 0.06, 0.08, 0.1][severity-1]
jitter = np.random.uniform(-c,c,(N, C))
new_pc = (pointcloud + jitter).astype('float32')
return new_pc
'''
Add Gaussian noise to point cloud
'''
def gaussian_noise(pointcloud, severity):
N, C = pointcloud.shape
c = [0.02, 0.03, 0.04, 0.05, 0.06][severity-1]
jitter = np.random.normal(size=(N, C)) * c
new_pc = (pointcloud + jitter).astype('float32')
return new_pc
'''
Add noise to the edge-length-2 cude
'''
def background_noise(pointcloud, severity):
N, C = pointcloud.shape
c = [N//45, N//40, N//35, N//30, N//20][severity-1]
noise_pc = np.zeros((c, C))
# noise_pc[:,0] = np.random.uniform(np.min(del_outlier_axis(pointcloud[:,0], 20)), np.max(del_outlier_axis(pointcloud[:,0], 20)),(c,))
noise_pc[:,0] = np.random.uniform(np.min(pointcloud[:,0]), np.max(pointcloud[:,0]),(c,))
# noise_pc[:,1] = np.random.uniform(np.min(del_outlier_axis(pointcloud[:,1], 20)), np.max(del_outlier_axis(pointcloud[:,1], 20)),(c,))
noise_pc[:,1] = np.random.uniform(np.min(pointcloud[:,1]), np.max(pointcloud[:,1]),(c,))
# delete outliers on z-direction because the hard boundary of pc_z range
temp_z = del_outlier_axis(pointcloud[:,2], 200)
noise_pc[:,2] = np.random.uniform(np.min(temp_z), np.max(temp_z),(c,))
new_pc = np.concatenate((pointcloud,noise_pc),axis=0).astype('float32')
return new_pc
'''
Upsampling
'''
def upsampling(pointcloud, severity):
N, C = pointcloud.shape
c = [N//10, N//8, N//6, N//4, N//2][severity-1]
index = np.random.choice(N, c, replace=False)
add = pointcloud[index] + np.random.uniform(-0.1,0.1,(c, C))
new_pc = np.concatenate((pointcloud,add),axis=0).astype('float32')
return new_pc
'''
Add impulse noise
'''
def impulse_noise(pointcloud, severity):
N, C = pointcloud.shape
c = [N//30, N//25, N//20, N//15, N//10][severity-1]
index = np.random.choice(N, c, replace=False)
pointcloud[index] += np.random.choice([-1,1], size=(c,C)) * 0.2
return pointcloud
'''
Add Uniform noise to point cloud radially (sensor-based)
'''
def uniform_noise_radial(pointcloud, severity):
N, C = pointcloud.shape
c = [0.04, 0.08, 0.12, 0.16, 0.2][severity-1]
jitter = np.random.uniform(-c,c,N)
new_pc = car2sph_pc(pointcloud)
new_pc[:, 0] = new_pc[:, 0] + jitter * np.sqrt(3)
new_pc = sph2car_pc(new_pc).astype('float32')
return new_pc
'''
Add Gaussian noise to point cloud radially (sensor-based)
'''
def gaussian_noise_radial(pointcloud, severity):
N, C = pointcloud.shape
c = [0.04, 0.06, 0.08, 0.10, 0.12][severity-1]
jitter = np.random.normal(size=N) * c
new_pc = car2sph_pc(pointcloud)
new_pc[:, 0] = new_pc[:, 0] + jitter * np.sqrt(3)
new_pc = sph2car_pc(new_pc).astype('float32')
return new_pc
'''
Add impulse noise radially (sensor-based)
'''
def impulse_noise_radial(pointcloud, severity):
N, C = pointcloud.shape
c = [N//30, N//25, N//20, N//15, N//10][severity-1]
index = np.random.choice(N, c, replace=False)
pointcloud = car2sph_pc(pointcloud)
pointcloud[index, 0] += np.random.choice([-1,1], size=c) * 0.4 * np.sqrt(3)
pointcloud = sph2car_pc(pointcloud).astype('float32')
return pointcloud