-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
executable file
·719 lines (552 loc) · 28.5 KB
/
utilities.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import os
import numpy as np
import time
from myClasses import Vec3
from myClasses import Plane
import math
from scipy.spatial import ConvexHull
import cv2
from scipy.interpolate import griddata
import tifffile
def make_if_not_exists(dirPath):
if not os.path.exists(dirPath):
os.makedirs(dirPath)
"""
** return Interior/Exterior Orientations of corresponding image
"""
def get_INTER_and_EXTER_Orientations(file_ori):
temp = []
with open(file_ori, "r") as fp:
for line in fp:
temp.append(line)
for i in range(len(temp)):
if "FocalLength" in temp[i].split("_"): # [mm->m]
f = - float(temp[i + 1]) / 1000 # different sign for aerial image
# print("f: {0}\n".format(f))
elif "PixelSize" in temp[i].split("_"): # [mm->m]
pixel_size_x = float(temp[i + 1].split("\t")[1]) / 1000
pixel_size_y = float(temp[i + 1].split("\t")[2]) / 1000
# print("pixel_size_x: {0}\n".format(pixel_size_x))
# print("pixel_size_y: {0}\n".format(pixel_size_y))
elif "SensorSize" in temp[i].split("_"): # [pixel]
img_width = int(temp[i + 1].split("\t")[1])
img_height = int(temp[i + 1].split("\t")[2])
# print("img_width: {0}\n".format(img_width))
# print("img_height: {0}\n".format(img_height))
elif "PrincipalPoint" in temp[i].split("_"): # [pixel]
x0 = float(temp[i + 1].split("\t")[1])
y0 = float(temp[i + 1].split("\t")[2])
# print("x0: {0}\n".format(x0))
# print("y0: {0}\n".format(y0))
elif "CameraMatrix" in temp[i].split("_"): # [ImageCoordinateSystem]
K = np.matrix([[float(temp[i + 1].split("\t")[1]), float(temp[i + 1].split("\t")[2]),
float(temp[i + 1].split("\t")[3])],
[float(temp[i + 2].split("\t")[1]), float(temp[i + 2].split("\t")[2]),
float(temp[i + 2].split("\t")[3])],
[float(temp[i + 3].split("\t")[1]), float(temp[i + 3].split("\t")[2]),
float(temp[i + 3].split("\t")[3])]])
# print("K: {0}\n".format(K))
elif "RotationMatrix" in temp[i].split("_"): # [World->ImageCoordinateSystem]
R = np.matrix([[float(temp[i + 1].split("\t")[1]), float(temp[i + 1].split("\t")[2]),
float(temp[i + 1].split("\t")[3])],
[float(temp[i + 2].split("\t")[1]), float(temp[i + 2].split("\t")[2]),
float(temp[i + 2].split("\t")[3])],
[float(temp[i + 3].split("\t")[1]), float(temp[i + 3].split("\t")[2]),
float(temp[i + 3].split("\t")[3])]])
# print("R: {0}\n".format(R))
elif "TranslationVector" in temp[i].split("_"): # [WorldCoordinateSystem]
Xc = float(temp[i + 1].split("\t")[1])
Yc = float(temp[i + 1].split("\t")[2])
Zc = float(temp[i + 1].split("\t")[3])
# print("Xc: {0}\n".format(Xc))
# print("Yc: {0}\n".format(Yc))
# print("Zc: {0}\n".format(Zc))
return f, pixel_size_x, img_width, img_height, K, R, Xc, Yc, Zc
# for nadir image
def get_exterior_orientation(img_name, extOri_file):
print("Looking for exterior orientations of the image: {0} \n".format(img_name))
with open(extOri_file, "r") as fp:
for line in fp:
if line.split("\t")[0] == img_name.split(".")[0]:
return line
"""
** Geometric Approach for View Frustum Culling
"""
def setCamInternals(nearD, farD, img_height, img_width, pixel_size):
# compute width and height of the near and far plane sections
nh = img_height * pixel_size
nw = img_width * pixel_size
fh = nh * farD / nearD
fw = nw * farD / nearD
return nh, nw, fh, fw
def setCamDef(nh, nw, fh, fw, nearD, farD, p=Vec3(), l=Vec3(), u=Vec3()):
# Input Parameters:
# * p: the position of the camera,
# * l: a point to where the camera is pointing
# * u: the up vector
# Vec3 dir,nc,fc,X,Y,Z;
Z = p - l
Z.normalized()
X = Vec3.cross(u, Z)
X.normalized()
Y = Vec3.cross(Z, X)
nc = p - Vec3.__mul__(Z, nearD)
fc = p - Vec3.__mul__(Z, farD)
ntl = nc + Vec3.__mul__(Y, nh) - Vec3.__mul__(X, nw)
ntr = nc + Vec3.__mul__(Y, nh) + Vec3.__mul__(X, nw)
nbl = nc - Vec3.__mul__(Y, nh) - Vec3.__mul__(X, nw)
nbr = nc - Vec3.__mul__(Y, nh) + Vec3.__mul__(X, nw)
ftl = fc + Vec3.__mul__(Y, fh) - Vec3.__mul__(X, fw)
ftr = fc + Vec3.__mul__(Y, fh) + Vec3.__mul__(X, fw)
fbl = fc - Vec3.__mul__(Y, fh) - Vec3.__mul__(X, fw)
fbr = fc - Vec3.__mul__(Y, fh) + Vec3.__mul__(X, fw)
pl_TOP = Plane(ntr, ntl, ftl)
pl_BOTTOM = Plane(nbl, nbr, fbr)
pl_LEFT = Plane(ntl, nbl, fbl)
pl_RIGHT = Plane(nbr, ntr, fbr)
pl_NEARP = Plane(ntl, ntr, nbr)
pl_FARP = Plane(ftr, ftl, fbl)
return pl_TOP, pl_BOTTOM, pl_LEFT, pl_RIGHT, pl_NEARP, pl_FARP
point_status = {
"INSIDE": 1,
"OUTSIDE": 0
}
def pointInFrustum(p=Vec3(), pl_TOP=Plane(), pl_BOTTOM=Plane(), pl_LEFT=Plane(), pl_RIGHT=Plane(), pl_NEARP=Plane(),
pl_FARP=Plane()):
result = "INSIDE"
if pl_TOP.distance(p) < 0 \
or pl_BOTTOM.distance(p) < 0 \
or pl_LEFT.distance(p) < 0 \
or pl_RIGHT.distance(p) < 0 \
or pl_NEARP.distance(p) < 0 \
or pl_FARP.distance(p) < 0:
result = "OUTSIDE"
return point_status[result]
def frustum_culling(Xc, Yc, Zc, f, img_height, img_width, pixel_size, pt_xyz, index):
print("Culling frustum... ")
start_time = time.time()
nearD = abs(f)
farD = Zc
nh, nw, fh, fw = setCamInternals(nearD, farD, img_height, img_width, pixel_size)
pl_TOP, pl_BOTTOM, pl_LEFT, pl_RIGHT, pl_NEARP, pl_FARP = \
setCamDef(nh, nw, fh, fw, nearD, farD, Vec3(0, 0, 0), Vec3(0, 0, 1), Vec3(0, 1, 0))
xyz_temp = []
index_temp = []
for i in range(0, pt_xyz.shape[0]):
pt = Vec3(pt_xyz[i, 0] - Xc, pt_xyz[i, 1] - Yc, pt_xyz[i, 2] - Zc)
flag = pointInFrustum(pt,
pl_TOP, pl_BOTTOM, pl_LEFT,
pl_RIGHT, pl_NEARP, pl_FARP)
if flag == 1:
xyz_temp.append([pt_xyz[i, 0], pt_xyz[i, 1], pt_xyz[i, 2]])
index_temp.append(index[i])
xyz_temp = np.matrix(xyz_temp)
index_temp = np.asarray(index_temp)
duration = time.time() - start_time
print(duration, "s\n")
return xyz_temp, index_temp
"""
** Hidden Point Removal
"""
def sphericalFlip(points, center, param):
# Function used to Perform Spherical Flip on the Original Point Cloud
n = points.shape[0] # total n points
points = points - np.repeat(center, n, axis=0) # Move C to the origin
normPoints = np.linalg.norm(points, axis=1) # Normed points, sqrt(x^2 + y^2 + (z-100)^2)
R = np.repeat(max(normPoints) * np.power(10.0, param), n, axis=0) # Radius of Sphere
flippedPointsTemp = 2 * np.multiply(np.repeat((R - normPoints).reshape(n, 1), len(points[0]), axis=1), points)
flippedPoints = np.divide(flippedPointsTemp, np.repeat(normPoints.reshape(n, 1), len(points[0]),
axis=1)) # Apply Equation to get Flipped Points
flippedPoints += points
return flippedPoints
def convexHull(points):
# Function used to Obtain the Convex hull
points = np.append(points, [[0, 0, 0]], axis=0) # All points plus origin
hull = ConvexHull(points) # Visibal points plus possible origin. Use its vertices property.
return hull
def HPR(Xc, Yc, Zc, xyz_temp, index_temp):
print("Hidden point removal... ")
start_time = time.time()
flag = np.zeros(len(xyz_temp), int) # 0 - Invisible; 1 - Visible.
C = np.array([[Xc, Yc, Zc]]) # Center
flippedPoints = sphericalFlip(xyz_temp, C, math.pi)
myHull = convexHull(flippedPoints)
visibleVertex = myHull.vertices[:-1] # indexes of visible points
flag[visibleVertex] = 1
visibleId = np.where(flag == 1)[0] # indexes of the visible points
myPoints = []
myIndex = []
for i in visibleId:
myPoints.append([xyz_temp[i, 0], xyz_temp[i, 1], xyz_temp[i, 2]])
myIndex.append(index_temp[i])
myPoints = np.matrix(myPoints)
myIndex = np.asarray(myIndex)
duration = time.time() - start_time
print(duration, "s\n")
return myPoints, myIndex
"""
** 3d to 2d projection
"""
def pointcloud2pixelcoord(R, K, Xc, Yc, Zc, myPoints):
print("projection... ")
start_time = time.time()
X0 = np.matrix([Xc, Yc, Zc]).T
Rt = np.concatenate((R, -np.dot(R, X0)), axis=1)
P = np.dot(K, Rt)
# calculate pixel points
myPoints = myPoints.T
# using homogeneous coord (4, 14129889)
myPoints = np.concatenate((np.mat(myPoints), np.full((1, myPoints.shape[1]), 1)), axis=0)
Pix_coor = np.dot(P, myPoints)
# Normalization of pixel points
px = Pix_coor[0, :] / Pix_coor[2, :]
py = Pix_coor[1, :] / Pix_coor[2, :]
duration = time.time() - start_time
print(duration, "s\n")
return px, py
"""
** Generation of synthetic images
"""
palette = { # BGR
0: (255, 0, 0), # Powerline
1: (255, 255, 255), # Low Vegetation
2: (255, 255, 0), # Impervious Surface
3: (255, 0, 255), # Vehicles
4: (0, 255, 255), # Urban Furniture
5: (0, 255, 0), # Roof
6: (0, 0, 255), # Facade
7: (239, 120, 76), # Bush/Hedge
8: (247, 238, 179), # Tree
9: (0, 18, 114), # Dirt/Gravel
10: (63, 34, 15), # Vertical Surface
11: (0, 0, 0) # Void
}
# the original function
def generation_syntheticImg(px, py, myIndex, pt_labels, pt_features, img_name, save_path, img_width, img_height):
print("Generation of synthetic image... \n")
start_time = time.time()
img_mask = np.zeros((img_height, img_width, 1), np.uint8) # used for making a mask only
img_temp = np.zeros((img_height, img_width, 3), np.uint8) # used for point level labeled image only
label_value = []
points = []
id = []
count = 0
for i in range(0, px.shape[1]):
if img_width > px[0, i] > 0 and img_height > py[0, i] > 0:
# filter the points which are not in the FOV of this image
points.append([px[0, i], py[0, i]])
label_value.append(int(pt_labels[myIndex[i]]))
id.append(myIndex[i])
r, g, b = palette[int(pt_labels[myIndex[i]])]
img_temp[int(py[0, i]), int(px[0, i]), 0] = r
img_temp[int(py[0, i]), int(px[0, i]), 1] = g
img_temp[int(py[0, i]), int(px[0, i]), 2] = b
count += 1
cv2.circle(img_mask, (int(px[0, i]), int(py[0, i])), 3, (255,255,255), -1)
points = np.array(points)
label_value = np.array(label_value)
id = np.array(id)
if count > 100: # I suppose at least 20% of pixels in the image should receive 3d point
# save the point level label image for checking
folder_path = os.path.join(save_path, "1_pointlabel")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), img_temp)
# mask, where no point is projected to pixel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))
closing_img_temp = cv2.morphologyEx(img_mask, cv2.MORPH_CLOSE, kernel).astype(np.uint8)
folder_path = os.path.join(save_path, "2_mask")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), closing_img_temp)
mask = (closing_img_temp[:,:] != 0)
# Generation of synthetic image based on different feature
X, Y = np.meshgrid(np.arange(0, img_width, 1), np.arange(0, img_height, 1))
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
# * labeled image (grey, as ground truth for training)
int_im = griddata(points, label_value, (X, Y), method='nearest').astype(np.uint8)
closing = cv2.morphologyEx(int_im, cv2.MORPH_CLOSE, kernel).astype(np.uint8)
closing[mask[:, :] == False] = 255 # label value 255 for unlabeled pixel
folder_path = os.path.join(save_path, "3_greylabel")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# * labeled image (color, only for visualization)
img_color_labeled = np.zeros((img_height, img_width, 3), np.uint8)
for i in range(0, img_height):
for j in range(0, img_width):
if int(int_im[i,j]) == 11 or int(int_im[i,j]) == 12 or int(int_im[i,j])==13:
int_im[i,j] = 10
if mask[i,j] == True:
r, g, b = palette[int(int_im[i, j])]
img_color_labeled[i, j, 0] = r
img_color_labeled[i, j, 1] = g
img_color_labeled[i, j, 2] = b
folder_path = os.path.join(save_path, "4_colorlabel")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name), img_color_labeled)
# * index image
index_im = griddata(points, id, (X, Y), method='nearest').astype(np.float32)
folder_path = os.path.join(save_path, "5_index")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), index_im)
# * feature map
# here, 20 features will be calculated
num_features = 20
features = np.zeros((id.shape[0], num_features), np.float32)
for i in range(0, features.shape[1]):
for j in range(0, features.shape[0]):
features[j, i] = pt_features[id[j], -(i + 1)]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
for i in range(0, features.shape[1]):
f_im = griddata(points, features[:,i], (X, Y), method='linear').astype(np.float32)
closing = cv2.morphologyEx(f_im, cv2.MORPH_CLOSE, kernel)
closing[mask[:, :] == False] = np.nan
folder_path = os.path.join(save_path, "f_"+str(i))
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
duration = time.time() - start_time
print(duration, "s\n")
# used in "main2", cropping image strategy of nadir image
def generation_syntheticImg_nadir(px, py, myIndex, pt_labels, pt_features, img_name, save_path, img_width, img_height):
img_temp = np.zeros((img_height, img_width, 3), np.uint8)
label_value = []
points = []
id = []
f_nDOM = []
f_I = []
f_Q_Pulse = []
f_prior = []
f_Q_N1 = []
f_Q_N2 = []
f_Q_N3 = []
f_Q_ES_1 = []
f_Q_ES_2 = []
f_Q_ES_3 = []
count = 0
for i in range(0, px.shape[1]):
if img_width > px[0, i] > 0 and img_height > py[0, i] > 0:
# filter the points which are not in the FOV of this image
points.append([px[0, i], py[0, i]])
label_value.append(int(pt_labels[myIndex[i]]))
id.append(myIndex[i])
f_nDOM.append(pt_features[myIndex[i], -4])
f_I.append(pt_features[myIndex[i], -3])
f_Q_Pulse.append(pt_features[myIndex[i], -2])
f_prior.append(pt_features[myIndex[i], -1])
f_Q_N3.append(pt_features[myIndex[i], -5])
f_Q_N2.append(pt_features[myIndex[i], -6])
f_Q_N1.append(pt_features[myIndex[i], -7])
f_Q_ES_3.append(pt_features[myIndex[i], -8])
f_Q_ES_2.append(pt_features[myIndex[i], -9])
f_Q_ES_1.append(pt_features[myIndex[i], -10])
c = palette[int(pt_labels[myIndex[i]])]
cv2.circle(img_temp, (int(px[0, i]), int(py[0, i])), 5, c, -1)
count += 1
points = np.array(points)
label_value = np.array(label_value)
id = np.array(id)
f_nDOM = np.array(f_nDOM)
f_I = np.array(f_I)
f_Q_Pulse = np.array(f_Q_Pulse)
f_prior = np.array(f_prior)
f_Q_N3 = np.array(f_Q_N3)
f_Q_N2 = np.array(f_Q_N2)
f_Q_N1 = np.array(f_Q_N1)
f_Q_ES_3 = np.array(f_Q_ES_3)
f_Q_ES_2 = np.array(f_Q_ES_2)
f_Q_ES_1 = np.array(f_Q_ES_1)
if count > 100: # I suppose at least 20% of pixels in the image should receive 3d point
folder_path = os.path.join(save_path, "1_pointlabel")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), img_temp)
# Generation of synthetic image based on different feature
X, Y = np.meshgrid(np.arange(0, img_width, 1), np.arange(0, img_height, 1))
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
# * labeled image (grey, as ground truth for training)
int_im = griddata(points, label_value, (X, Y), method='nearest').astype(np.uint8)
closing = cv2.morphologyEx(int_im, cv2.MORPH_CLOSE, kernel).astype(np.uint8)
folder_path = os.path.join(save_path, "3_greylabel")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# * labeled image (color, only for visualization)
img_color_labeled = np.zeros((img_height, img_width, 3), np.uint8)
for i in range(0, img_height):
for j in range(0, img_width):
if int(int_im[i,j]) == 11 or int(int_im[i,j]) == 12 or int(int_im[i,j])==13:
int_im[i,j] = 10
r, g, b = palette[int(int_im[i, j])]
img_color_labeled[i, j, 0] = r
img_color_labeled[i, j, 1] = g
img_color_labeled[i, j, 2] = b
folder_path = os.path.join(save_path, "4_colorlabel")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name), img_color_labeled)
# * index image
index_im = griddata(points, id, (X, Y), method='nearest').astype(np.float32)
folder_path = os.path.join(save_path, "5_index")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), index_im)
# # <entry val="_nDOM" format="3.4" invalidValue="0" externalType="float" />
f_nDOM_im = griddata(points, f_nDOM, (X, Y), method='linear').astype(np.float32)
closing = cv2.morphologyEx(f_nDOM_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_nDOM")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# <entry val="_I" format="4.4" invalidValue="0" externalType="float" />
f_I_im = griddata(points, f_I, (X, Y), method='nearest').astype(np.float32)
closing = cv2.morphologyEx(f_I_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_I")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# <entry val="_Q_Pulse" format="1.4" invalidValue="1" externalType="float" />
f_Q_Pulse_im = griddata(points, f_Q_Pulse, (X, Y), method='nearest').astype(np.float32)
closing = cv2.morphologyEx(f_Q_Pulse_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_Q_Pulse")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# <entry val="_prior" format="1.4" invalidValue="1" externalType="float" />
f_prior_im = griddata(points, f_prior, (X, Y), method='nearest').astype(np.float32)
closing = cv2.morphologyEx(f_prior_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_prior")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# <entry val="_Q_N3" format="1.4" invalidValue="1" externalType="float" />
f_Q_N3_im = griddata(points, f_Q_N3, (X, Y), method='nearest').astype(np.float32)
closing = cv2.morphologyEx(f_Q_N3_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_Q_N3")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# <entry val="_Q_N2" format="1.4" invalidValue="1" externalType="float" />
f_Q_N2_im = griddata(points, f_Q_N2, (X, Y), method='nearest').astype(np.float32)
closing = cv2.morphologyEx(f_Q_N2_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_Q_N2")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# <entry val="_Q_N1" format="1.4" invalidValue="1" externalType="float" />
f_Q_N1_im = griddata(points, f_Q_N1, (X, Y), method='nearest').astype(np.float32)
closing = cv2.morphologyEx(f_Q_N1_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_Q_N1")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# <entry val="_Q_ES_3" format="1.4" invalidValue="0" externalType="float" />
f_Q_ES_3_im = griddata(points, f_Q_ES_3, (X, Y), method='nearest').astype(np.float32)
closing = cv2.morphologyEx(f_Q_ES_3_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_Q_ES_3")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# entry val="_Q_ES_2" format="1.4" invalidValue="0" externalType="float" />
f_Q_ES_2_im = griddata(points, f_Q_ES_2, (X, Y), method='nearest').astype(np.float32)
closing = cv2.morphologyEx(f_Q_ES_2_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_Q_ES_2")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# <entry val="_Q_ES_1" format="1.4" invalidValue="0" externalType="float" />
f_Q_ES_1_im = griddata(points, f_Q_ES_1, (X, Y), method='nearest').astype(np.float32)
closing = cv2.morphologyEx(f_Q_ES_1_im, cv2.MORPH_CLOSE, kernel)
folder_path = os.path.join(save_path, "f_Q_ES_1")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# * the 2 functions blow are used in "main3", generation of 5cm-based label image, 10cm-based feature map
# 5cm only generate index image and label image
def generation_syntheticImg_5cmbased(px, py, myIndex, pt_labels, img_name, save_path, img_width, img_height):
img_mask = np.zeros((img_height, img_width, 1), np.uint8) # used for making a mask only
img_temp = np.zeros((img_height, img_width, 3), np.uint8) # used for point level labeled image only
# index_im2 = np.zeros((img_height, img_width), np.float32) # point index, without interpolation
label_value = []
points = []
id = []
count = 0
for i in range(0, px.shape[1]):
if img_width > px[0, i] > 0 and img_height > py[0, i] > 0:
# filter the points which are not in the FOV of this image
points.append([px[0, i], py[0, i]])
label_value.append(int(pt_labels[myIndex[i]]))
id.append(myIndex[i])
r, g, b = palette[int(pt_labels[myIndex[i]])]
img_temp[int(py[0, i]), int(px[0, i]), 0] = r
img_temp[int(py[0, i]), int(px[0, i]), 1] = g
img_temp[int(py[0, i]), int(px[0, i]), 2] = b
count += 1
cv2.circle(img_mask, (int(px[0, i]), int(py[0, i])), 3, (255,255,255), -1) # level3
# cv2.circle(img_mask, (int(px[0, i]), int(py[0, i])), 6, (255, 255, 255), -1) # level0
# index_im2[int(py[0, i]), int(px[0, i])] = myIndex[i] # point index, without interpolation
points = np.array(points)
label_value = np.array(label_value)
id = np.array(id)
if count > 100: # I suppose at least 20% of pixels in the image should receive 3d point
# save the point level label image for checking
folder_path = os.path.join(save_path, "1_pointlabel")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), img_temp)
# mask, where no point is projected to pixel
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10)) # level3
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 20)) # level0
closing_img_temp = cv2.morphologyEx(img_mask, cv2.MORPH_CLOSE, kernel).astype(np.uint8)
folder_path = os.path.join(save_path, "2_mask")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), closing_img_temp)
mask = (closing_img_temp[:,:] != 0)
# Generation of synthetic image based on different feature
X, Y = np.meshgrid(np.arange(0, img_width, 1), np.arange(0, img_height, 1))
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2)) # level3
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) # level0
# * labeled image (grey, as ground truth for training)
int_im = griddata(points, label_value, (X, Y), method='nearest').astype(np.uint8)
closing = cv2.morphologyEx(int_im, cv2.MORPH_CLOSE, kernel).astype(np.uint8)
closing[mask[:, :] == False] = 255 # label value 255 for unlabeled pixel
folder_path = os.path.join(save_path, "3_greylabel")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name.split("/")[-1]), closing)
# * labeled image (color, only for visualization)
img_color_labeled = np.zeros((img_height, img_width, 3), np.uint8)
for i in range(0, img_height):
for j in range(0, img_width):
if int(int_im[i,j]) == 11 or int(int_im[i,j]) == 12 or int(int_im[i,j])==13:
int_im[i,j] = 10
print("!!!!")
if mask[i,j] == True:
r, g, b = palette[int(int_im[i, j])]
img_color_labeled[i, j, 0] = r
img_color_labeled[i, j, 1] = g
img_color_labeled[i, j, 2] = b
folder_path = os.path.join(save_path, "4_colorlabel")
make_if_not_exists(folder_path)
cv2.imwrite(os.path.join(folder_path, img_name), img_color_labeled)
# * index image
index_im = griddata(points, id, (X, Y), method='nearest').astype(np.float32)
folder_path = os.path.join(save_path, "5_index")
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), index_im)
# # * index image, without interpolation
# folder_path = os.path.join(save_path, "6_pointindex")
# make_if_not_exists(folder_path)
# tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), index_im2)
return mask
# 10cm only generate feature map, here, 72 feature maps
def generation_syntheticImg_10cmbased(mask, px2, py2, myIndex2, pt_features, img_name, save_path, img_width, img_height):
points2 = []
id2 = []
count = 0
for i in range(0, px2.shape[1]):
if img_width > px2[0, i] > 0 and img_height > py2[0, i] > 0:
# filter the points which are not in the FOV of this image
points2.append([px2[0, i], py2[0, i]])
id2.append(myIndex2[i])
count += 1
points2 = np.array(points2)
id2 = np.array(id2)
if count > 100: # I suppose at least 20% of pixels in the image should receive 3d point
# Generation of synthetic image based on different feature
X, Y = np.meshgrid(np.arange(0, img_width, 1), np.arange(0, img_height, 1))
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# * feature map
features = np.zeros((id2.shape[0], pt_features.shape[1]), np.float32)
for i in range(0, features.shape[1]):
for j in range(0, features.shape[0]):
features[j, i] = pt_features[id2[j], i]
for i in range(0, features.shape[1]):
f_im = griddata(points2, features[:,i], (X, Y), method='linear').astype(np.float32)
closing = cv2.morphologyEx(f_im, cv2.MORPH_CLOSE, kernel)
closing[mask[:, :] == False] = np.nan
folder_path = os.path.join(save_path, "f_"+str(i))
make_if_not_exists(folder_path)
tifffile.imsave(os.path.join(folder_path, img_name.split("/")[-1]), closing)