forked from vevenom/RoomLayout3D_RandC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoomLayoutEstimator.py
1568 lines (1202 loc) · 63.8 KB
/
RoomLayoutEstimator.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import cv2
import matplotlib.pyplot as plt
from shapely.geometry import Polygon, MultiPolygon
import open3d as o3d
import pulp
from fit_ransac import fit_plane_RANSAC, fit_line_RANSAC
from rl_config import *
from layout_structs import *
from search_polygons import *
from utils import *
class RoomLayoutEstimator:
def __init__(self, example):
# Initialize randomness
np.random.seed(seed=seed)
# Load color image
img_path = example_folder + "color/" + str(example) + ".jpg"
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (640, 480))
# Load planes masks
planes_masks_path = example_folder + "planercnn_seg/" + str(example) + ".npy"
planes_masks = np.load(planes_masks_path)
# Load depth
depth_path = example_folder + "depth/" + str(example) + ".png"
depth = cv2.imread(depth_path, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH) / 1000.
depth = cv2.resize(depth, (640, 480)).astype(np.float32)
depth[np.greater(depth,max_depth_value)] = 0
# Load filled depth
depth_filled_path = example_folder + "depth_filled/" + str(example) + ".png"
depth_filled = cv2.imread(depth_filled_path, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH) / 1000.
depth_filled = cv2.resize(depth_filled, (640, 480)).astype(np.float32)
depth_filled[np.greater(depth_filled,max_depth_value)] = 0
# Load semantic segmentation
segmentation_path = example_folder + "mseg_labels/" + str(example) + ".png"
segmentation_orig = cv2.imread(segmentation_path, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
segmentation_conf_path = example_folder + "mseg_confidence/" + str(example) + ".png"
seg_confidence = cv2.imread(segmentation_conf_path, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH) / 2 ** 8
segmentation = np.zeros_like(segmentation_orig)
wall_indices_bool = np.isin(segmentation_orig, wall_indices)
floor_indices_bool = np.isin(segmentation_orig, floor_indices)
ceil_indices_bool = np.isin(segmentation_orig, ceiling_indices)
segmentation[wall_indices_bool] = wall_type * (seg_confidence[wall_indices_bool] > seg_confidence_wall_thresh)
segmentation[floor_indices_bool] = floor_type * (seg_confidence[floor_indices_bool] > seg_confidence_floor_thresh)
segmentation[ceil_indices_bool] = ceil_type * (seg_confidence[ceil_indices_bool] > seg_confidence_ceil_thresh)
# Create image boundary mask (predictions around image border are often bad)
boundary_mask = np.zeros_like(img[:, :, 0])
cv2.rectangle(boundary_mask, (0, 0), (depth.shape[1] - 1, depth.shape[0] - 1), 1.,
thickness=boundary_mask_thickness)
boundary_mask = 1. - boundary_mask
height, width = img.shape[:2]
if plot_intermediate_results:
plt.figure()
plt.subplot(141)
plt.imshow(img)
plt.subplot(142)
plt.imshow(depth, vmin=0, vmax=10)
plt.subplot(143)
plt.imshow(segmentation)
plt.subplot(144)
vis_plane_seg = np.zeros_like(segmentation)
for i, plane_mask in enumerate(planes_masks):
vis_plane_seg += (plane_mask * (i + 1)).astype(np.uint8)
plt.imshow(vis_plane_seg)
plt.show()
# ScanNet intrinsics
sx = width / 1296
sy = height / 968
K = np.reshape(np.array([1170.187988 * sx, 0., 647.750000 * sx, 0., 1170.187988 * sy, 483.75 * sy, 0., 0., 1.]), newshape=(3, 3))
self.example = example
self.img = img / 255
self.depth = depth
self.depth_filled = depth_filled
self.planes_masks = planes_masks
self.segmentation = segmentation
layout_seg_mask = np.isin(segmentation, floor_wall_ceil)
self.layout_seg_mask = layout_seg_mask
# self.layout_seg_no_refl_mask = layout_seg_no_refl_mask
self.depth_layout_masked = layout_seg_mask * depth
# self.depth_layout_no_refl_masked = layout_seg_no_refl_mask * depth
self.val_seg_indices = floor_wall_ceil
self.boundary_mask = boundary_mask
self.h, self.w = h, w = self.depth.shape[:2]
self.set_helpers(K)
# Calculate flattened one hot segmentation
seg = cv2.resize(segmentation.astype(np.uint8), (w, h))
self.seg_flat = seg.reshape(-1)
self.seg_one_hot_flat = np.eye(4)[self.seg_flat].T
self.val_ind = floor_wall_ceil
self.use_virtual_floor_plane = False
def set_helpers(self, K):
'''
Set helpers
:param K:
:return:
'''
h, w = self.h, self.w
self.K = K
self.K_inv = np.linalg.inv(K)
self.coordinate_grid, self.coordinate_grid_2d = get_coord_grid(h, w)
self.K_dot_coord = np.dot(self.K_inv, self.coordinate_grid)
depth_flat = np.reshape(self.depth, newshape=(1, -1))
self.depth_mask_flat = np.greater(depth_flat, min_depth)
self.depth_mask = self.depth_mask_flat.reshape((h, w))
self.depth_K_dot_coord = depth_flat * self.K_dot_coord
self.K_dot_coord = np.dot(self.K_inv, self.coordinate_grid)
depth_filled_flat = np.reshape(self.depth_filled, newshape=(1, -1))
self.depth_filled_mask_flat = np.greater(depth_filled_flat, min_depth)
self.depth_filled_mask = self.depth_filled_mask_flat.reshape((h, w))
self.depth_filled_K_dot_coord = depth_filled_flat * self.K_dot_coord
self.K_dot_coord_2d = np.dot(self.coordinate_grid_2d, np.transpose(self.K_inv))
self.depth_K_dot_coord_2d = self.depth[:,:,None] * self.K_dot_coord_2d
def calc_frustum_planes(self):
'''
Calculate frustum planes
:return: A list of frustum planes (LayoutPlane)
'''
h, w = self.h, self.w
K_inv = self.K_inv
c1 = np.array([0, 0, 1])
c2 = np.array([w - 1, 0, 1])
c3 = np.array([0, h - 1, 1])
c4 = np.array([w - 1, h - 1, 1])
v1 = K_inv.dot(c1)
v2 = K_inv.dot(c2)
v3 = K_inv.dot(c3)
v4 = K_inv.dot(c4)
n12 = np.cross(v1, v2)
n12 = n12 / np.sqrt(n12[0] ** 2 + n12[1] ** 2 + n12[2] ** 2)
n13 = -np.cross(v1, v3)
n13 = n13 / np.sqrt(n13[0] ** 2 + n13[1] ** 2 + n13[2] ** 2)
n24 = -np.cross(v2, v4)
n24 = n24 / np.sqrt(n24[0] ** 2 + n24[1] ** 2 + n24[2] ** 2)
n34 = -np.cross(v3, v4)
n34 = n34 / np.sqrt(n34[0] ** 2 + n34[1] ** 2 + n34[2] ** 2)
plane1 = LayoutPlane(plane=np.concatenate((n12, [0])), mask=np.ones((h, w)), type=-1)
plane2 = LayoutPlane(plane=np.concatenate((n13, [0])), mask=np.ones((h, w)), type=-1)
plane3 = LayoutPlane(plane=np.concatenate((n24, [0])), mask=np.ones((h, w)), type=-1)
plane4 = LayoutPlane(plane=np.concatenate((n34, [0])), mask=np.ones((h, w)), type=-1)
frustum_planes = [plane1, plane2, plane3, plane4]
return frustum_planes
def merge_planes_iteratively(self, layout_planes):
'''
Merge planes iteratively
:param layout_planes:
:return: list of merged planes (LayoutPlane)
'''
print("Merging planes...")
while True:
merged_layout_planes = self.merge_planes(layout_planes)
if len(merged_layout_planes) == len(layout_planes):
break
else:
layout_planes = merged_layout_planes
return merged_layout_planes
def merge_planes(self, layout_planes):
'''
Merge planes
:param layout_planes:
:return: list of merged planes (LayoutPlane)
'''
depth_K_dot_coord_2d = self.depth_K_dot_coord_2d
depth_mask = self.depth_mask
are_planes_merged = [False] * len(layout_planes)
# Prepare planes masks
planes_masks = []
for plane_index, layout_plane in enumerate(layout_planes):
planes_masks.append(layout_plane.mask)
# Merge planes
merged_layout_planes = []
for plane_index, layout_plane in enumerate(layout_planes):
plane_params, plane_mask, plane_type = layout_plane.plane, layout_plane.mask, layout_plane.type
is_plane_merged = are_planes_merged[plane_index]
if is_plane_merged:
continue
merged_plane_mask = plane_mask
merged_plane_params = plane_params
merged_plane_type = plane_type
# Compare the plane to all of the other planes
for plane_index2, layout_plane2 in enumerate(layout_planes):
if plane_index2 <= plane_index:
continue
plane_params2, plane_mask2, plane_type2 = layout_plane2.plane, layout_plane2.mask, layout_plane2.type
n1 = plane_params[:3]
n2 = plane_params2[:3]
# Compare the planes parameters
if np.all(np.less(np.abs(np.cross(n1, n2)), merge_parallel_threshold)) and \
np.abs(plane_params[3] - plane_params2[3]) < merge_offset_threshold:
# Check whether the two planes are neighbours
if self.check_if_planes_neighbs(planes_masks, plane_params, plane_mask, plane_params2, plane_mask2):
is_plane_merged = True
merged_plane_mask = np.clip(merged_plane_mask + plane_mask2, a_min=0., a_max=1.)
are_planes_merged[plane_index2] = True
depth_K_dot_coord_masked1 = depth_K_dot_coord_2d[(depth_mask * merged_plane_mask).astype(np.bool)]
depth_K_dot_coord_sampled = depth_K_dot_coord_masked1
ones = np.ones((depth_K_dot_coord_sampled.shape[0], 1))
depth_K_dot_coord_sampled_ones = np.concatenate((depth_K_dot_coord_sampled, ones), axis=1)
plane_params = self.calc_plane_params_from_pcd(depth_K_dot_coord_sampled_ones)
merged_plane_params = plane_params
merged_layout_plane = LayoutPlane(
plane=merged_plane_params, mask=merged_plane_mask, type=merged_plane_type)
merged_layout_planes.append(merged_layout_plane)
return merged_layout_planes
def calc_valid_planes(self):
'''
Calculate valid planes
:return: list of LayoutPlanes
'''
h = self.h
w = self.w
layout_planes = self.calc_valid_planes_ransac()
layout_planes = self.merge_planes_iteratively(layout_planes)
if plot_intermediate_results:
planes_filtered_segs_image, normals_image_overlay = self.vis_layout_planes(layout_planes)
plt.figure()
plt.subplot(121)
plt.imshow(planes_filtered_segs_image)
plt.subplot(122)
plt.imshow(normals_image_overlay)
plt.show()
return layout_planes
def calc_valid_planes_ransac(self):
'''
Calculate valid planes
:return: list of planes_params
'''
h = self.h
w = self.w
# Flatten planes masks
planes_masks_flat = np.reshape(self.planes_masks, newshape=(-1, h * w)) * np.reshape(self.boundary_mask, (-1))
# Find layout planes
planes = []
for plane_ind in range(len(planes_masks_flat)):
layout_plane = self.calc_plane_params(plane_ind)
if layout_plane is not None:
planes.append(layout_plane)
return planes
def calc_plane_params_from_pcd(self, sampled_pc):
'''
Calculate plane from given PCD
:param sampled_pc: point cloud (N, 4)
:return: plane parameters
'''
plane_params, inlier_list1, outlier_list1 = fit_plane_RANSAC(sampled_pc, inlier_thresh=ransac_thresh)
plane_params = norm_plane_params(plane_params)
return plane_params
def calc_plane_params(self, i, validate=True):
'''
Calculate planes parameters
:param i: plane index
:param validate: whether to validate the feasibility of plane
:return: LayoutPlane
'''
# Get helper attributes
h, w = self.h, self.w
depth_mask_flat = self.depth_mask_flat
depth_K_dot_coord = self.depth_K_dot_coord
# Calculate flattened one hot segmentation
seg_one_hot_flat = self.seg_one_hot_flat
val_ind = self.val_seg_indices
# Flatten planes masks
planes_masks_flat = np.reshape(self.planes_masks, newshape=(-1, h * w)) * np.reshape(self.boundary_mask, (-1))
valid_seg = (1. - (self.segmentation == 0))
valid_seg_flat = valid_seg.reshape((-1))
plane_mask_flat = planes_masks_flat[i]
# Calculate overlap of valid semantic segmentations and given plane mask
plane_seg_flat = valid_seg_flat * plane_mask_flat * seg_one_hot_flat
plane_seg_flat_sum_classes = np.sum(plane_seg_flat, axis=1)
plane_seg_overlap_joint = np.vstack([np.sum(plane_seg_flat_sum_classes[wall_type]),
np.sum(plane_seg_flat_sum_classes[floor_type]),
np.sum(plane_seg_flat_sum_classes[ceil_type])
]).sum(axis=0) / np.sum(plane_mask_flat)
# Validate the plane based on val_thresholf
if (validate and plane_seg_overlap_joint < val_threshold):
return None
# Determine the plane type
plane_type = calc_plane_type(plane_mask_flat, plane_seg_flat_sum_classes, val_ind)
if plane_type == -1:
return None
# Calculate plane parameters from 3D points of the given plane using RANSAC
val_plane_indices = valid_layout_ind_dict[plane_type]
plane_seg_mask_flat = np.sum(plane_seg_flat[val_plane_indices], axis=0)
depth_K_dot_coord_masked1 = depth_K_dot_coord[:, (depth_mask_flat[0] * plane_seg_mask_flat).astype(np.bool)]
depth_K_dot_coord_sampled = depth_K_dot_coord_masked1
ones = np.ones((1, depth_K_dot_coord_sampled.shape[1]))
depth_K_dot_coord_sampled_ones = np.concatenate((depth_K_dot_coord_sampled, ones), axis=0).T
if depth_K_dot_coord_sampled_ones.shape[0] < min_plane_size:
return None
plane_params = self.calc_plane_params_from_pcd(depth_K_dot_coord_sampled_ones)
layout_plane = LayoutPlane(
plane=plane_params, mask=np.reshape(plane_seg_mask_flat, (h, w)), type=plane_type)
return layout_plane
def add_virtual_floor_plane(self, layout_planes):
'''
Add virtual floor plane if floor is not visible
:param layout_planes:
:return: LayoutPlane
'''
# Get Helpers
h, w = self.h, self.w
self.use_virtual_floor_plane = True
self.use_virtual_ceil_plane = True
for layout_plane in layout_planes:
plane_params, plane_type = layout_plane.plane, layout_plane.type
if plane_type == floor_type:
floor_plane_params = plane_params
self.use_virtual_floor_plane = False
if plane_type == ceil_type:
self.use_virtual_ceil_plane = False
one_valid_floor_virtual = False
if self.use_virtual_floor_plane:
floor_perp_cand_list = []
# Add virtual floor that is orthogonal to the walls in the scene
for plane_ind, layout_plane in enumerate(layout_planes):
plane_params, plane_mask, plane_type = layout_plane.plane, \
layout_plane.mask, layout_plane.type
if plane_type == wall_type:
n1 = plane_params[:3]
orth_n1 = np.array([-n1[1], n1[0], 0.])
orth_n2 = np.array([0, -n1[2], n1[1]])
if np.abs(orth_n1[1]) > np.abs(orth_n2[1]):
orth_n = orth_n1
else:
orth_n = orth_n2
orth_n = norm_plane_params(orth_n)
if orth_n[1] > 0.:
orth_n *= -1
if np.abs(orth_n[1]) < 0.5:
continue
floor_perp_cand_list.append(orth_n)
one_valid_floor_virtual = True
floor_plane_params = np.mean(np.array(floor_perp_cand_list), axis=0)
if np.any(np.isnan(floor_plane_params)):
self.use_virtual_floor_plane = False
return None, self.use_virtual_floor_plane
floor_plane_params = np.append(floor_plane_params, [[camera_height]])
print("Virt. floor plane params", floor_plane_params)
floor_plane_params = norm_plane_params(floor_plane_params)
if one_valid_floor_virtual:
floor_plane = LayoutPlane(plane=floor_plane_params, mask=np.zeros((h, w)), type=floor_type)
return floor_plane
else:
self.use_virtual_floor_plane = False
return None
def get_floor_planes(self, layout_planes):
found_floor_plane = False
floor_plane_params = None
for layout_plane in layout_planes:
plane_params, plane_type = layout_plane.plane, layout_plane.type
d = plane_params[-1]
if plane_type == floor_type:
if not found_floor_plane or d < floor_plane_params[-1]:
floor_plane_params = plane_params
found_floor_plane = True
return floor_plane_params
def check_if_planes_neighbs(self, planes_masks, plane_params, plane_mask, plane_params2, plane_mask2, plot=True):
'''
Check if planes are neighbours
:param planes_masks: planes mask
:param plane_mask:
:param plane_mask2:
:param plot:
:return:
'''
def calc_grad_mask(mask):
grad_y = np.abs(mask[1:] - mask[:-1])
grad_x = np.abs(mask[:, 1:] - mask[:, :-1])
grad_mask = np.zeros_like(mask)
grad_mask[1:] = grad_y
grad_mask[:,1:] = np.maximum(grad_mask[:,1:], grad_x)
return grad_mask
depth = self.depth_filled
depth_mask = self.depth_filled_mask
K_dot_coord_2d = self.K_dot_coord_2d
# Two planes are neighbours if there are no other planes between them
other_masks_joined = np.sum(np.array(planes_masks), axis=0) - plane_mask - plane_mask2
plane_mask2_grad_mask = calc_grad_mask(plane_mask2).astype(np.uint8)
plane_mask2_dist_transf = cv2.distanceTransform(1 - plane_mask2_grad_mask, cv2.DIST_L2, 3)
plane_mask2_dist_transf[np.logical_not(plane_mask.astype(np.bool))] = 1e4
plane_mask_point = np.argmin(plane_mask2_dist_transf)
plane_mask_point = np.unravel_index(plane_mask_point, plane_mask.shape)
plane_mask1_grad_mask = calc_grad_mask(plane_mask).astype(np.uint8)
plane_mask1_dist_transf = cv2.distanceTransform(1 - plane_mask1_grad_mask, cv2.DIST_L2, 3)
plane_mask1_dist_transf[np.logical_not(plane_mask2.astype(np.bool))] = 1e4
plane_mask2_point = np.argmin(plane_mask1_dist_transf)
plane_mask2_point = np.unravel_index(plane_mask2_point, plane_mask2.shape)
line12_mask = np.zeros_like(other_masks_joined)
cv2.line(line12_mask, (plane_mask_point[1], plane_mask_point[0]),
(plane_mask2_point[1], plane_mask2_point[0]), 1., thickness=5)
n = plane_params[:3]
d = plane_params[3]
z1 = line12_mask * (-d * K_dot_coord_2d[:, :, 2] / (K_dot_coord_2d.dot(n) + 1e-6))
planes_dist = (line12_mask * depth_mask * np.maximum(depth - z1, 0)).sum() / np.maximum((depth_mask * line12_mask).sum(), 1.)
return np.all(np.less(other_masks_joined + line12_mask, 2)) and planes_dist < merge_offset_threshold
def intersect_3_planes(self, layout_planes, i, j, k, floor_plane_params):
'''
Intersect 3 planes
:param layout_planes:
:param i: index of plane 1
:param j: index of plane 2
:param k: index of plane 3
:return:
'''
# Get helper attributes
h, w = self.h, self.w
K = self.K
j_is_frustum = layout_planes[j].type == 0
k_is_frustum = layout_planes[k].type == 0
plane1 = layout_planes[i].plane
plane2 = layout_planes[j].plane
plane3 = layout_planes[k].plane
plane1_type = layout_planes[i].type
plane2_type = layout_planes[j].type
plane3_type = layout_planes[k].type
plane1_normal, d1 = plane1[:3], plane1[3]
plane2_normal, d2 = plane2[:3], plane2[3]
plane3_normal, d3 = plane3[:3], plane3[3]
denom = np.dot(plane1_normal, np.cross(plane2_normal, plane3_normal))
# None of the planes should be parallel
if not((j_is_frustum or np.abs(np.cross(plane1_normal, plane2_normal)).mean() > par_thresh) and \
(k_is_frustum or np.abs(np.cross(plane1_normal, plane3_normal)).mean() > par_thresh) and \
((j_is_frustum and k_is_frustum) or np.abs(np.cross(plane2_normal, plane3_normal)).mean() > par_thresh)):
return None
nom = -d1 * np.cross(plane2_normal, plane3_normal) - \
d2 * np.cross(plane3_normal, plane1_normal) - \
d3 * np.cross(plane1_normal, plane2_normal)
intersection_3d = nom / denom
# Check if the intersection is under the floor plane (Check explicitly if the intersection includes floor
# due to frustum planes inconsistencies]
if not floor_type in [plane1_type, plane2_type, plane3_type] and floor_plane_params is not None:
floor_plane_rel = np.dot(floor_plane_params[:3], intersection_3d) + floor_plane_params[3]
if floor_plane_rel < 0:
return None
intersection_image_plane = np.dot(K, np.reshape(intersection_3d, (3, 1)))
intersection_norm = (intersection_image_plane / (intersection_image_plane[2] + 1e-6))
x = intersection_norm[0][0]
y = intersection_norm[1][0]
x = np.nan_to_num(x)
y = np.nan_to_num(y)
x = np.clip(x, a_min=-1e6, a_max=1e6)
y = np.clip(y, a_min=-1e6, a_max=1e6)
x = int(np.round(x))
y = int(np.round(y))
if (intersection_3d[2] > max_depth_thresh or intersection_3d[2] < min_depth_thresh):
# print("Intersection point is far away")
return None
if not((0 <= x < w) and (0 <= y < h)):
return None
line12_u = np.cross(plane1_normal, plane2_normal)
line12_u = line12_u / np.linalg.norm(line12_u, ord=2)
line13_u = np.cross(plane1_normal, plane3_normal)
line13_u = line13_u / np.linalg.norm(line13_u, ord=2)
line23_u = np.cross(plane2_normal, plane3_normal)
line23_u = line23_u / np.linalg.norm(line23_u, ord=2)
intersection_dict = {}
intersection_dict['inter'] = intersection_3d
intersection_dict['inter_image'] = [x, y]
intersection_dict['inter_edges'] = [line12_u, line13_u, line23_u]
intersection_dict['inter_planes'] = [i, j, k]
intersection_dict['inter_planes_frustum'] = [False,
j > len(layout_planes) - 5,
k > len(layout_planes) - 5]
intersection_dict['inter_frustum'] = k > len(layout_planes) - 5
return intersection_dict
def intersect_planes(self, layout_planes):
'''
Intersect layout planes
:param layout_planes:
:return: list of xs, ys, intersection dictionary
'''
xs = []
ys = []
intersection_dict_list = []
if plot_intermediate_results:
plt.figure()
# Planes below floor will be ignored
floor_plane_params = self.get_floor_planes(layout_planes)
# Intersect plane triplets
plot_counter = 1
for i, layout_plane1 in enumerate(layout_planes):
if layout_plane1.type == 0:
continue
if layout_plane1.type == -1:
continue
for j, layout_plane2 in enumerate(layout_planes):
if j > len(layout_planes) - 1:
continue
if j <= i:
continue
for k, layout_plane3 in enumerate(layout_planes):
if k <= i or k <= j:
continue
intersection_dict = self.intersect_3_planes(layout_planes, i, j, k, floor_plane_params)
if intersection_dict is None:
continue
x, y = intersection_dict['inter_image']
xs.append(x)
ys.append(y)
intersection_dict_list.append(intersection_dict)
if plot_intermediate_results:
plane_mask1 = layout_plane1.mask
plane_mask2 = layout_plane2.mask
plane_mask3 = layout_plane3.mask
im = np.copy(self.img) * 255
ch_r = (plane_mask1 * 255)[:, :, None]
ch_g = (plane_mask2 * 255)[:, :, None]
ch_b = (plane_mask3 * 255)[:, :, None]
image_plus_3surf = np.minimum(0.3 * np.concatenate((ch_r, ch_g, ch_b), axis=2) + 0.7 * im,
255).astype(np.uint8)
if plot_counter < 50:
plt.subplot(5,10,plot_counter)
plt.title(str(i) + "," + str(j) + "," + str(k))
plot_counter += 1
implot = plt.imshow(
(plane_mask1[:, :, None] +
plane_mask2[:, :, None] +
plane_mask3[:,:, None] * im).astype(np.uint8))
plt.scatter(x=[x], y=[y], c='r', s=40)
implot = plt.imshow(image_plus_3surf)
if plot_intermediate_results:
plt.show()
print("Number of intersections found: " + str(len(intersection_dict_list)))
self.rl_intersection_dict_list = intersection_dict_list
return (xs, ys), intersection_dict_list
def find_candidate_edges(self, intersection_dict_list, layout_planes):
'''
Find candidate edges
:param intersection_dict_list: List of intersection dict
:param layout_planes: List of layout planes
:return: list of candidate edges, dict of candidate edges index by plane
'''
candidate_edges = []
planes_n = len(layout_planes)
planes_candidate_edges = {}
for plane_index in range(planes_n):
planes_candidate_edges[plane_index] = []
for i, intersection_dict_i in enumerate(intersection_dict_list):
intersection_dict_i_planes = intersection_dict_i['inter_planes']
for j, intersection_dict_j in enumerate(intersection_dict_list):
if j <= i:
continue
# Consider only intersections that were created by same plane
intersection_dict_j_planes = intersection_dict_j['inter_planes']
# Check if two intersections share two planes
check_i_j_plane0 = intersection_dict_i_planes[0] in intersection_dict_j_planes[:3]
check_i_j_plane1 = intersection_dict_i_planes[1] in intersection_dict_j_planes[:3]
check_i_j_plane2 = intersection_dict_i_planes[2] in intersection_dict_j_planes[:3]
if (check_i_j_plane0 and check_i_j_plane1) or (check_i_j_plane0 and check_i_j_plane2) or \
(check_i_j_plane1 and check_i_j_plane2):
# If two planes are common for two intersections, there are some lines that coincide, hence edge
candidate_edges.append([i, j])
if check_i_j_plane0:
planes_candidate_edges[intersection_dict_i_planes[0]].append([i, j])
if check_i_j_plane1:
planes_candidate_edges[intersection_dict_i_planes[1]].append([i, j])
if check_i_j_plane2:
planes_candidate_edges[intersection_dict_i_planes[2]].append([i, j])
return candidate_edges, planes_candidate_edges
def get_poly_mask(self, poly):
'''
Get polygon mask
:param poly: list of polygon vertices
:return: polygon mask (H, W)
'''
h, w = self.h, self.w
pts = np.array(poly, np.int32)
pts = pts.reshape((-1, 1, 2))
polygon_mask = np.zeros((h, w))
cv2.fillPoly(polygon_mask, [pts], 1.)
return polygon_mask
def calc_new_plane_masks(self, planes_polygons):
'''
Calculate polygon masks for each of the given polygons
:param planes_polygons: List of polygons
:return: List of polygons masks
'''
h, w = self.h, self.w
new_planes_masks = []
for plane_ind, plane_polygon in enumerate(planes_polygons):
polygon_mask = self.get_poly_mask(plane_polygon)
new_planes_masks.append(polygon_mask)
return new_planes_masks
def calc_layout_edges(self, layout_components, thickness=1):
'''
Calculate layout edges from the given components
:param layout_components: list of layout components
:param thickness: edge thickness
:return: Layout edge mask (H,W)
'''
h, w = self.h, self.w
layout_edge_mask = np.zeros((h, w))
for plane_ind, layout_comp in enumerate(layout_components):
comp_type = layout_comp.type
if comp_type in floor_wall_ceil:
pts = np.array(layout_comp.poly, np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(layout_edge_mask, [pts], False, 1., thickness=thickness)
return layout_edge_mask
def solve_with_pulp(self, layout_candidates):
'''
Layout solver, PULP implementation
:param layout_candidates: list of layout candidate components
:return: list of optimal layout components, Solver status: 'Optimal', 'Infeasible'
'''
print("-------------Solver------------------")
print("Solving binary discrete optimization....")
cand_incompatible_mat, cand_neighbour_mat = self.getCandidateCompatibilityMatrices(layout_candidates)
# Instantiate our problem class
model = pulp.LpProblem("Layout_solver", pulp.LpMinimize)
polys_vars = pulp.LpVariable.dicts("plane_poly_bool",
((i) for i in range(len(layout_candidates))),
cat=pulp.LpBinary)
# Objective Function
cost_function = [layout_candidates[i].cost * polys_vars[i]
for i in range(len(layout_candidates))]
model += pulp.lpSum(cost_function)
# Constraints
# One polygon per plane
print("Adding Constraints: One polygon per plane constraint...")
layout_planes = [layout_candidate.plane for layout_candidate in layout_candidates]
layout_planes = np.unique(np.array(layout_planes), axis=0)
for plane in layout_planes:
one_poly_per_plane_constr = [polys_vars[i] for i in range(len(layout_candidates)) if np.all(layout_candidates[i].plane == plane)]
model += pulp.lpSum(one_poly_per_plane_constr) == 1
# No polygons intersect
print("Adding Constraints: Compatible components only...")
for i in range(len(layout_candidates)):
for j in range(i + 1, len(layout_candidates)):
if cand_incompatible_mat[i, j]:
incomp_constr = polys_vars[i] + polys_vars[j] <= 1
model.__iadd__(incomp_constr)
print("Adding Constraint: Sum of polygon areas should cover 100% of the image...")
# sum of polygon areas should cover 100% of the image
# Area seems to be not exactly equal to the number of pixels in the image (probably a rounding error in shapely)
shapely_poly = Polygon([(0., 0.), (self.w - 1., 0.), (self.w - 1., self.h - 1.), (0., self.h - 1.), (0., 0.)])
image_poly_area = shapely_poly.area
areas_sums = [layout_candidates[i].area * polys_vars[i] for i in range(len(layout_candidates))]
area_thresh = 1. #0.999
model += pulp.lpSum(areas_sums) >= image_poly_area * area_thresh
print("Number of polygon variables: ", len(polys_vars.keys()))
# Solve the problem
print("Solving...")
model.solve()
solver_status = pulp.LpStatus[model.status]
print("Solver status: ", pulp.LpStatus[model.status])
print("Cost: ", pulp.lpSum(cost_function).value())
solution_components = []
for var in polys_vars:
var_value = polys_vars[var].varValue
if var_value != 0:
solution_comp = layout_candidates[var]
solution_components.append(solution_comp)
if plot_intermediate_results:
plt.figure()
num_plots = len(solution_components)
for i, solution_comp in enumerate(solution_components):
img_overlay = np.clip(0.5 * self.img + 0.5 * solution_comp.mask[:, :, None], a_min=0, a_max=255)
# img_cpy = copy.deepcopy(self.img)
pts = np.array(solution_comp.poly, np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(img_overlay, [pts], True, (0, 255, 0), thickness=10)
plt.subplot(1, num_plots, i + 1)
plt.imshow(img_overlay)
plt.show()
return solution_components, solver_status
def getCandidateCompatibilityMatrices(self, candidates):
'''
Get Compatibility matrices
:param candidates: List of candidate layout components
:return: Incompability matrix, Neighbourhood matrix
'''
cand_incompatible_mat = np.eye(len(candidates))
cand_neighbour_mat = np.zeros((len(candidates), len(candidates)))
for i in range(len(candidates)):
print("Compatibility Candidate %d of %d \r" % (i + 1, len(candidates)), end="", flush=True)
for j in range(i, len(candidates)):
# Get first candidate
poly1 = candidates[i].poly
poly1_edges = [np.concatenate([vtx, poly1[vtx_ind + 1]], axis=0) for vtx_ind, vtx in enumerate(poly1[:-1])]
poly1_edges = np.array(poly1_edges)
plane1 = candidates[i].plane
# Get second candidate
poly2 = candidates[j].poly
poly2_edges = [np.concatenate([vtx, poly2[vtx_ind + 1]], axis=0) for vtx_ind, vtx in enumerate(poly2[:-1])]
poly2_edges = np.array(poly2_edges)
poly2_edges_rev = [np.concatenate([poly2[vtx_ind + 1], vtx], axis=0) for vtx_ind, vtx in enumerate(poly2[:-1])]
poly2_edges_rev = np.array(poly2_edges_rev)
plane2 = candidates[j].plane
same_plane = np.all(plane1 == plane2)
if same_plane:
continue
# Check if poly1 and poly2 share an edge
poly1_diff_poly2 = np.any(np.all(np.abs(poly1_edges[:, None, :] - poly2_edges).reshape(-1,4) < 1e-2, axis=1))
poly1_diff_poly2_rev = np.any(np.all(np.abs(poly1_edges[:, None, :] - poly2_edges_rev).reshape(-1,4) < 1e-2, axis=1))
poly1_poly2_share_an_edge = poly1_diff_poly2 or poly1_diff_poly2_rev
incompatible_polys = False
if len(poly1) >= 3 and len(poly2) >= 3:
poly1_sh = Polygon(
[(float(vtx[0]), float(vtx[1])) for vtx in poly1])
poly2_sh = Polygon(
[(float(vtx[0]), float(vtx[1])) for vtx in poly2])
poly1_sh_buff = poly1_sh.buffer(-5)
poly1_sh = Polygon(poly1_sh_buff.exterior)
poly2_sh_buff = poly2_sh.buffer(-5)
poly2_sh = Polygon(poly2_sh_buff.exterior)
# TODO Why can there be an exception?
try:
incompatible_polys = poly1_sh.intersects(poly2_sh) or \
poly1_sh.contains(poly2_sh) or poly2_sh.contains(poly1_sh)
except:
print("WARNING: Exception reached in getCandidateCompatibilityMatrices")
incompatible_polys = True
if incompatible_polys:
cand_incompatible_mat[i, j] = 1
cand_incompatible_mat[j, i] = 1
elif (poly1_poly2_share_an_edge or len(poly1) < 3 or len(poly2) < 3):
cand_neighbour_mat[i, j] = 1
cand_neighbour_mat[j, i] = 1
print('Calculated candidate interesection matrix of size %dx%d' % (
len(candidates), len(candidates)))
print("-------------------------------")
return cand_incompatible_mat, cand_neighbour_mat
def find_best_polygons(self, xys, intersection_dict_list, planes_candidate_edges, layout_planes):
'''
Find the optimal layout given a set of corners and edges and layout planes
:param xys: XY locations of candidate corners (planes intersections)
:param intersection_dict_list: Intersection dict with details on each of the planes interesctions
:param planes_candidate_edges: List of candidate edges
:param layout_planes: List of layout planes
:return: list of optimal layout components, Solver status: 'Optimal', 'Infeasible'
'''
def find_candidate_plane_polygons(xys_list, plane_edges_list, plane_ind):
'''
Parse polygons for given plane
:param xys_list:
:param plane_edges_list:
:param plane_ind:
:return:
'''
start_list = [(edge_ind, start_edge, plane_edges_list, xys_list, intersection_dict_list, plane_ind) for
edge_ind, start_edge in enumerate(plane_edges_list)]
init_plane_polygons_list = []
for start_edge in start_list:
start_edge_polys = start_search_edge(start_edge)
if start_edge_polys is not None:
init_plane_polygons_list += [start_edge_polys]
valid_plane_polygons_list = []
for pol in init_plane_polygons_list:
if pol is not None:
valid_plane_polygons_list += pol
init_plane_polygons_list = valid_plane_polygons_list
valid_plane_polygons_list = []
for cycle in init_plane_polygons_list:
# cycle += [cycle[0]]
cp_coord = [[xs[pol_vtx], ys[pol_vtx]] for pol_vtx in cycle]
cycle_poly = Polygon([(float(vtx[0]), float(vtx[1])) for vtx in cp_coord])
cycle_poly_sh_buff = cycle_poly.buffer(-5)
if isinstance(cycle_poly_sh_buff, MultiPolygon):
continue
cycle_poly = Polygon(cycle_poly_sh_buff.exterior)
if cycle_poly.is_empty:
continue
if not cycle_poly.is_simple:
continue
if not cycle_poly.is_valid:
continue
if cycle_poly.area < min_plane_size:
continue
valid_plane_polygons_list.append(cycle)
return valid_plane_polygons_list