-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_tif_json.py
285 lines (213 loc) · 9.88 KB
/
generate_tif_json.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import json
import os.path as osp
import os
from generate_data import *
from skimage.external import tifffile
import numpy as np
from copy import deepcopy
colors = np.array([0,2000],np.uint16)
def write_json(obj,fpath):
dir = osp.dirname(fpath)
#osutils.mkdir_if_missing(dir)
with open(fpath,'w') as f:
json.dump(obj,f,indent=4)
def generate_tif(filetif_dir, tif_name, ins_num=3):
ins_num = ins_num
tif_shape = [300, 300, 300]
tif = np.zeros(shape=tif_shape, dtype=np.int32)
#debug
#tif_vis = np.zeros(shape=tif_shape, dtype=np.int32)
coords_info = dict()
i_ins = 1
for n in range(ins_num):
gl_all = gene_n_degree_branch3D(num_degree=[1, 10])
# 抖动
for gl in gl_all:
for i in range(gl.num_points):
mu = 0
s = 1
deltax = 0.45 * random.gauss(mu=mu, sigma=s)
deltay = 0.45 * random.gauss(mu=mu, sigma=s)
deltaz = 0.45 * random.gauss(mu=mu, sigma=s)
gl.x_values[i] = gl.x_values[i] + deltax
gl.y_values[i] = gl.y_values[i] + deltay
gl.z_values[i] = gl.z_values[i] + deltaz
gl_all, data = pre_process(gl_all)
tif_narr = np.zeros(shape=tif_shape, dtype=np.int32)
tif_narr_vis = np.zeros(shape=tif_shape, dtype=np.int32)
for i, d in zip(range(len(data)), data):
for j in range(len(d)):
tif_narr[tuple(d[j].astype(np.int))] = i+1
tif_narr_vis[tuple(d[j].astype(np.int))] = 1
coords_info[i_ins] = [int(i) for i in d[0]]#list(d[0].astype(np.int))
tif[tuple(d[j].astype(np.int))] = i_ins
#debug
#tif_vis[tuple(d[j].astype(np.int32))] = 1
i_ins += 1
tifffile.imsave(osp.join(filetif_dir, tif_name+".tif"), tif.astype(np.int16))
#debug
#tifffile.imsave(osp.join(filetif_dir, tif_name+"_vis.tif"), colors[tif_vis])
print("{} have been saved.\n".format(tif_name+".tif"))
json_path = osp.join(filetif_dir, tif_name+".json")
write_json(coords_info, json_path)
def generate_tif_v1(filetif_dir, tif_name, ins_num=3):
ins_num = ins_num
tif_shape = [300, 300, 300]
tif = np.zeros(shape=tif_shape, dtype=np.int32)
#debug
tif_vis = np.zeros(shape=tif_shape, dtype=np.int32)
coords_info = dict()
i_ins = 1
for n in range(ins_num):
degree_2th_num = 3 #int(random.gauss(10, 3))
gl_all = gene_n_degree_branch3D(num_degree=[1, degree_2th_num])
# 抖动
for gl in gl_all:
for i in range(gl.num_points):
mu = 0
s = 1
deltax = 0.9 * random.gauss(mu=mu, sigma=s)
deltay = 0.9 * random.gauss(mu=mu, sigma=s)
deltaz = 0.9 * random.gauss(mu=mu, sigma=s)
gl.x_values[i] = gl.x_values[i] + deltax
gl.y_values[i] = gl.y_values[i] + deltay
gl.z_values[i] = gl.z_values[i] + deltaz
# 毛刺
size_num = int(random.gauss(100, 10))
ind = list(np.random.choice(gl_all[0].num_points, size=size_num, replace=False))
init_coords = [np.array(gl_all[0].x_values)[ind], np.array(gl_all[0].y_values)[ind], np.array(gl_all[0].z_values)[ind]]
init_thetas = np.array(gl_all[0].theta_)[ind]
init_phis = np.array(gl_all[0].phi_)[ind]
for k in range(size_num):
ini_coord = [init_coords[0][k], init_coords[1][k], init_coords[2][k]]
gl_i = GenerateLine_2Direct_3D(init_coords=ini_coord,
init_theta=init_thetas[k]+random.uniform(0, PI/2),
init_phi=init_phis[k]+random.uniform(0, PI/2),
num_points=int(random.gauss(mu=30, sigma=3)))
gl_i.fill_points()
gl_all[0].x_values += gl_i.x_values
gl_all[0].y_values += gl_i.y_values
gl_all[0].z_values += gl_i.z_values
gl_all[0].phi_ += gl_i.phi_
gl_all[0].theta_ += gl_i.theta_
gl_all, data = pre_process(gl_all)
tif_narr = np.zeros(shape=tif_shape, dtype=np.int32)
tif_narr_vis = np.zeros(shape=tif_shape, dtype=np.int32)
for i, d in zip(range(len(data)), data):
for j in range(len(d)):
tif_narr[tuple(d[j].astype(np.int))] = i+1
tif_narr_vis[tuple(d[j].astype(np.int))] = 1
coords_info[i_ins] = [int(i) for i in d[0]]#list(d[0].astype(np.int))
tif[tuple(d[j].astype(np.int))] = i_ins
#debug
tif_vis[tuple(d[j].astype(np.int32))] = 1
i_ins += 1
tifffile.imsave(osp.join(filetif_dir, tif_name+".tif"), tif.astype(np.int16))
#debug
tifffile.imsave(osp.join(filetif_dir, tif_name+"_vis.tif"), colors[tif_vis])
print("{} have been saved.\n".format(tif_name+".tif"))
json_path = osp.join(filetif_dir, tif_name+".json")
write_json(coords_info, json_path)
def generate_tif_v2(filetif_dir, tif_name, ins_num=3):
# 添加背景噪声
ins_num = ins_num
tif_shape = [300, 300, 300]
tif = np.zeros(shape=tif_shape, dtype=np.int32)
#debug
tif_vis = np.zeros(shape=tif_shape, dtype=np.int32)
coords_info = dict()
i_ins = 1
for n in range(ins_num):
degree_2th_num = 3 #int(random.gauss(10, 3))
gl_all = gene_n_degree_branch3D(num_degree=[1, degree_2th_num])
# 抖动
for gl in gl_all:
for i in range(gl.num_points):
mu = 0
s = 1
deltax = 0.9 * random.gauss(mu=mu, sigma=s)
deltay = 0.9 * random.gauss(mu=mu, sigma=s)
deltaz = 0.9 * random.gauss(mu=mu, sigma=s)
gl.x_values[i] = gl.x_values[i] + deltax
gl.y_values[i] = gl.y_values[i] + deltay
gl.z_values[i] = gl.z_values[i] + deltaz
# 毛刺
size_num = int(random.gauss(100, 10))
ind = list(np.random.choice(gl_all[0].num_points, size=size_num, replace=False))
init_coords = [np.array(gl_all[0].x_values)[ind], np.array(gl_all[0].y_values)[ind], np.array(gl_all[0].z_values)[ind]]
init_thetas = np.array(gl_all[0].theta_)[ind]
init_phis = np.array(gl_all[0].phi_)[ind]
for k in range(size_num):
ini_coord = [init_coords[0][k], init_coords[1][k], init_coords[2][k]]
gl_i = GenerateLine_2Direct_3D(init_coords=ini_coord,
init_theta=init_thetas[k]+random.uniform(0, PI/2),
init_phi=init_phis[k]+random.uniform(0, PI/2),
num_points=int(random.gauss(mu=30, sigma=3)))
gl_i.fill_points()
gl_all[0].x_values += gl_i.x_values
gl_all[0].y_values += gl_i.y_values
gl_all[0].z_values += gl_i.z_values
gl_all[0].phi_ += gl_i.phi_
gl_all[0].theta_ += gl_i.theta_
gl_all, data = pre_process(gl_all)
tif_narr = np.zeros(shape=tif_shape, dtype=np.int32)
tif_narr_vis = np.zeros(shape=tif_shape, dtype=np.int32)
for i, d in zip(range(len(data)), data):
for j in range(len(d)):
tif_narr[tuple(d[j].astype(np.int))] = i+1
tif_narr_vis[tuple(d[j].astype(np.int))] = 1
coords_info[i_ins] = [int(i) for i in d[0]]#list(d[0].astype(np.int))
tif[tuple(d[j].astype(np.int))] = i_ins
#debug
tif_vis[tuple(d[j].astype(np.int32))] = 1
i_ins += 1
# 背景噪声
noise_mask = np.zeros(shape=tif_shape, dtype=np.int32)
index = np.random.choice(tif_shape[0]*tif_shape[1]*tif_shape[2], size=int(20*random.gauss(0, 1)+180), replace=False)
coords = np.array(np.unravel_index(index, dims=tif_shape))
noise_mask[coords[0], coords[1], coords[2]] = 1
tif_index = tif > 0
tif_ = deepcopy(tif)
tif[noise_mask>0] = 1000
tif[tif_index] = tif_[tif_index]
tif_vis[noise_mask>0] = 1
tifffile.imsave(osp.join(filetif_dir, tif_name+".tif"), tif.astype(np.int16))
#debug
tifffile.imsave(osp.join(filetif_dir, tif_name+"_vis.tif"), colors[tif_vis])
print("{} have been saved.\n".format(tif_name+".tif"))
json_path = osp.join(filetif_dir, tif_name+".json")
write_json(coords_info, json_path)
def pre_process(gl=None):
gl_all = gl
data3D = []
x_max_v = -1000; x_min_v = 10000
y_max_v = -1000; y_min_v = 10000
z_max_v = -1000; z_min_v = 10000
for gl in gl_all:
x_max_v = max(x_max_v, max(gl.x_values))
x_min_v = min(x_min_v, min(gl.x_values))
y_max_v = max(y_max_v, max(gl.y_values))
y_min_v = min(y_min_v, min(gl.y_values))
z_max_v = max(z_max_v, max(gl.z_values))
z_min_v = min(z_min_v, min(gl.z_values))
# 缩放到(300, 300)
#x_max = max(gl.x_values); x_min = min(gl.x_values)
for gl in gl_all:
gl.x_values = ((np.array(gl.x_values) - x_min_v) / (x_max_v - x_min_v)) * 299
#y_max = max(gl.y_values); y_min = min(gl.y_values)
gl.y_values = ((np.array(gl.y_values) - y_min_v) / (y_max_v - y_min_v)) * 299
gl.z_values = ((np.array(gl.z_values) - z_min_v) / (z_max_v - z_min_v)) * 299
#print(gl.z_values.shape)
coords = np.stack([gl.x_values, gl.y_values, gl.z_values], axis=0).T
data3D.append(coords)
return gl_all, data3D
def main():
tif_num = 5
fpath = "./SyntheticData/1x3glitch"
if not osp.exists(fpath):
os.mkdir(fpath)
for i in range(tif_num):
tif_name = "SD_{:0>3d}".format(i)
generate_tif_v2(filetif_dir=fpath, tif_name=tif_name, ins_num=1)
if __name__ == "__main__":
main()