-
Notifications
You must be signed in to change notification settings - Fork 56
/
floorplan_utils.py
executable file
·1218 lines (1083 loc) · 47.6 KB
/
floorplan_utils.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
import numpy as np
from skimage import measure
import cv2
import copy
from utils import *
NUM_WALL_CORNERS = 13
NUM_CORNERS = 21
CORNER_RANGES = {'wall': (0, 13), 'opening': (13, 17), 'icon': (17, 21)}
MAX_NUM_CORNERS = 300
NUM_FINAL_ICONS = 10
NUM_FINAL_ROOMS = 15
NUM_ICONS = 13
NUM_ROOMS = 16
HEIGHT=256
WIDTH=256
NUM_POINTS = 50000
NUM_INPUT_CHANNELS = 7
NUM_CHANNELS = [7, 64, 64, 64, 128, 256]
SIZES = [WIDTH, WIDTH // 2, WIDTH // 4, WIDTH // 8, WIDTH // 16, WIDTH // 32]
POINT_ORIENTATIONS = [[(2, ), (3, ), (0, ), (1, )], [(0, 3), (0, 1), (1, 2), (2, 3)], [(1, 2, 3), (0, 2, 3), (0, 1, 3), (0, 1, 2)], [(0, 1, 2, 3)]]
def getOrientationRanges(width, height):
orientationRanges = [[width, 0, 0, 0], [width, height, width, 0], [width, height, 0, height], [0, height, 0, 0]]
return orientationRanges
def getIconNames():
iconNames = []
iconLabelMap = getIconLabelMap()
for iconName, _ in iconLabelMap.iteritems():
iconNames.append(iconName)
continue
return iconNames
def getRoomLabelMap():
labelMap = {}
labelMap['living_room'] = 1
labelMap['kitchen'] = 2
labelMap['bedroom'] = 3
labelMap['bathroom'] = 4
labelMap['restroom'] = 4
labelMap['office'] = 3
labelMap['closet'] = 6
labelMap['balcony'] = 7
labelMap['corridor'] = 8
labelMap['dining_room'] = 9
labelMap['laundry_room'] = 10
labelMap['garage'] = 11
labelMap['recreation_room'] = 12
labelMap['stairs'] = 13
labelMap['other'] = 14
labelMap['wall'] = 15
return labelMap
def getLabelRoomMap():
labelMap = {}
labelMap[1] = 'living room'
labelMap[2] = 'kitchen'
labelMap[3] = 'bedroom'
labelMap[4] = 'bathroom'
labelMap[6] = 'closet'
labelMap[7] = 'balcony'
labelMap[8] = 'corridor'
labelMap[9] = 'dining room'
return labelMap
def getIconLabelMap():
labelMap = {}
labelMap['cooking_counter'] = 1
labelMap['bathtub'] = 2
labelMap['toilet'] = 3
labelMap['washing_basin'] = 4
labelMap['sofa'] = 5
labelMap['cabinet'] = 6
labelMap['bed'] = 7
labelMap['table'] = 8
labelMap['desk'] = 8
labelMap['refrigerator'] = 9
labelMap['TV'] = 0
labelMap['entrance'] = 0
labelMap['chair'] = 0
labelMap['door'] = 11
labelMap['window'] = 12
return labelMap
def getLabelIconMap():
labelMap = {}
labelMap[1] = 'cooking_counter'
labelMap[2] = 'bathtub'
labelMap[3] = 'toilet'
labelMap[4] = 'washing_basin'
labelMap[5] = 'sofa'
labelMap[6] = 'cabinet'
labelMap[7] = 'bed'
labelMap[8] = 'table'
labelMap[9] = 'refrigerator'
return labelMap
def getLabelMapNYU40():
labelMap = {}
labelMap[1] = 'wall'
labelMap[2] = 'floor'
labelMap[3] = 'cabinet'
labelMap[4] = 'bed'
labelMap[5] = 'chair'
labelMap[6] = 'sofa'
labelMap[7] = 'table'
labelMap[8] = 'door'
labelMap[9] = 'window'
labelMap[10] = 'bookshelf'
labelMap[11] = 'picture'
labelMap[12] = 'cooking_counter'
labelMap[13] = 'blinds'
labelMap[14] = 'desk'
labelMap[15] = 'shelf'
labelMap[16] = 'curtain'
labelMap[17] = 'dresser'
labelMap[18] = 'pillow'
labelMap[19] = 'mirror'
labelMap[20] = 'entrance' #mat
labelMap[21] = 'clothes'
labelMap[22] = 'ceiling'
labelMap[23] = 'book'
labelMap[24] = 'refrigerator'
labelMap[25] = 'TV'
labelMap[26] = 'paper'
labelMap[27] = 'towel'
labelMap[28] = 'shower_curtain'
labelMap[29] = 'box'
labelMap[30] = 'whiteboard'
labelMap[31] = 'person'
labelMap[32] = 'nightstand'
labelMap[33] = 'toilet'
labelMap[34] = 'washing_basin'
labelMap[35] = 'lamp'
labelMap[36] = 'bathtub'
labelMap[37] = 'bag'
labelMap[38] = 'otherprop'
labelMap[39] = 'otherstructure'
labelMap[40] = 'unannotated'
return labelMap
def getNYUScanNetMap():
labelMap = np.zeros(41, dtype=np.int32)
labelMap[1] = 1
labelMap[2] = 2
labelMap[3] = 19
labelMap[4] = 6
labelMap[5] = 3
labelMap[6] = 8
labelMap[7] = 4
labelMap[8] = 14
labelMap[9] = 15
labelMap[10] = 7
labelMap[11] = 18
labelMap[12] = 13
labelMap[13] = 12 #20 Blinds
labelMap[14] = 5
labelMap[15] = 7
labelMap[16] = 12
labelMap[17] = 19
labelMap[18] = 20
labelMap[19] = 20
labelMap[20] = 20
labelMap[21] = 20
labelMap[22] = 1
labelMap[23] = 20
labelMap[24] = 17
labelMap[25] = 20
labelMap[26] = 20
labelMap[27] = 20
labelMap[28] = 16
labelMap[29] = 20
labelMap[30] = 20
labelMap[31] = 20
labelMap[32] = 20
labelMap[33] = 11
labelMap[34] = 9
labelMap[35] = 20
labelMap[36] = 10
labelMap[37] = 20
labelMap[38] = 20
labelMap[39] = 20
labelMap[40] = 0
return labelMap
def getMatterportClassMap():
classMap = {
'a': 1,
'b': 2,
'c': 3,
'd': 4,
'e': 5,
'f': 6,
'g': 7,
'h': 8,
'i': 9,
'j': 10,
'k': 11,
'l': 12,
'm': 13,
'n': 14,
'o': 15,
'p': 16,
'r': 17,
's': 18,
't': 19,
'u': 20,
'v': 21,
'w': 22,
'x': 23,
'y': 24,
'z': 25,
'B': 26,
'C': 27,
'D': 28,
'S': 29,
'Z': 30
}
return classMap
def calcLineDim(points, line):
point_1 = points[line[0]]
point_2 = points[line[1]]
if abs(point_2[0] - point_1[0]) > abs(point_2[1] - point_1[1]):
lineDim = 0
else:
lineDim = 1
return lineDim
def calcLineDirection(line):
return int(abs(line[0][0] - line[1][0]) < abs(line[0][1] - line[1][1]))
def calcLineDirectionPoints(points, line):
point_1 = points[line[0]]
point_2 = points[line[1]]
if isinstance(point_1[0], tuple):
point_1 = point_1[0]
pass
if isinstance(point_2[0], tuple):
point_2 = point_2[0]
pass
return calcLineDirection((point_1, point_2))
def pointDistance(point_1, point_2):
#return np.sqrt(pow(point_1[0] - point_2[0], 2) + pow(point_1[1] - point_2[1], 2))
return max(abs(point_1[0] - point_2[0]), abs(point_1[1] - point_2[1]))
def sortLines(lines):
newLines = []
for line in lines:
direction = calcLineDirection(line)
if line[0][direction] < line[1][direction]:
newLines.append((line[0], line[1]))
else:
newLines.append((line[1], line[0]))
pass
continue
return newLines
def lineRange(line):
direction = calcLineDirection(line)
fixedValue = (line[0][1 - direction] + line[1][1 - direction]) / 2
minValue = min(line[0][direction], line[1][direction])
maxValue = max(line[0][direction], line[1][direction])
return direction, fixedValue, minValue, maxValue
def findConnections(line_1, line_2, gap):
connection_1 = -1
connection_2 = -1
pointConnected = False
for c_1 in xrange(2):
if pointConnected:
break
for c_2 in xrange(2):
if pointDistance(line_1[c_1], line_2[c_2]) > gap:
continue
connection_1 = c_1
connection_2 = c_2
connectionPoint = ((line_1[c_1][0] + line_2[c_2][0]) / 2, (line_1[c_1][1] + line_2[c_2][1]) / 2)
pointConnected = True
break
continue
if pointConnected:
return [connection_1, connection_2], connectionPoint
direction_1, fixedValue_1, min_1, max_1 = lineRange(line_1)
direction_2, fixedValue_2, min_2, max_2 = lineRange(line_2)
if direction_1 == direction_2:
return [-1, -1], (0, 0)
#print(fixedValue_1, min_1, max_1, fixedValue_2, min_2, max_2)
if min(fixedValue_1, max_2) < max(fixedValue_1, min_2) - gap or min(fixedValue_2, max_1) < max(fixedValue_2, min_1) - gap:
return [-1, -1], (0, 0)
if abs(min_1 - fixedValue_2) <= gap:
return [0, 2], (fixedValue_2, fixedValue_1)
if abs(max_1 - fixedValue_2) <= gap:
return [1, 2], (fixedValue_2, fixedValue_1)
if abs(min_2 - fixedValue_1) <= gap:
return [2, 0], (fixedValue_2, fixedValue_1)
if abs(max_2 - fixedValue_1) <= gap:
return [2, 1], (fixedValue_2, fixedValue_1)
return [2, 2], (fixedValue_2, fixedValue_1)
def lines2Corners(lines, gap, getSingularCorners=False):
corners = []
lineConnections = []
for _ in xrange(len(lines)):
lineConnections.append({})
continue
connectionCornerMap = {}
connectionCornerMap[(1, 1)] = 4
connectionCornerMap[(0, 1)] = 5
connectionCornerMap[(0, 0)] = 6
connectionCornerMap[(1, 0)] = 7
connectionCornerMap[(2, 0)] = 8
connectionCornerMap[(1, 2)] = 9
connectionCornerMap[(2, 1)] = 10
connectionCornerMap[(0, 2)] = 11
connectionCornerMap[(2, 2)] = 12
corners = []
for lineIndex_1, line_1 in enumerate(lines):
for lineIndex_2, line_2 in enumerate(lines):
if lineIndex_2 == lineIndex_1:
continue
connections, connectionPoint = findConnections(line_1, line_2, gap=gap)
if connections[0] == -1 and connections[1] == -1:
continue
if calcLineDirection(line_1) == calcLineDirection(line_2):
print('overlap', line_1, line_2, connections)
exit(1)
pass
if calcLineDirection(line_1) == 1:
continue
indices = [lineIndex_1, lineIndex_2]
#print(lineIndex_1, lineIndex_2, connections)
for c in xrange(2):
if connections[c] in [0, 1] and connections[c] in lineConnections[indices[c]]:
print('duplicate corner', line_1, line_2, connections)
exit(1)
pass
lineConnections[indices[c]][connections[c]] = True
continue
corners.append((connectionPoint, connectionCornerMap[tuple(connections)]))
continue
continue
if getSingularCorners:
singularCorners = []
for lineIndex, connections in enumerate(lineConnections):
if 0 not in connections:
print('single corner', lines[lineIndex], connections)
singularCorners.append((lineIndex, 0))
pass
if 1 not in connections:
print('single corner', lines[lineIndex], connections)
singularCorners.append((lineIndex, 1))
pass
continue
return corners, singularCorners
return corners
def drawWallMask(walls, width, height, thickness=3, indexed=False):
if indexed:
wallMask = np.full((height, width), -1, dtype=np.int32)
for wallIndex, wall in enumerate(walls):
cv2.line(wallMask, (int(wall[0][0]), int(wall[0][1])), (int(wall[1][0]), int(wall[1][1])), color=wallIndex, thickness=thickness)
continue
else:
wallMask = np.zeros((height, width), dtype=np.int32)
for wall in walls:
cv2.line(wallMask, (int(wall[0][0]), int(wall[0][1])), (int(wall[1][0]), int(wall[1][1])), color=1, thickness=thickness)
continue
wallMask = wallMask.astype(np.bool)
pass
return wallMask
def mergeLines(line_1, line_2):
direction_1, fixedValue_1, min_1, max_1 = lineRange(line_1)
direction_2, fixedValue_2, min_2, max_2 = lineRange(line_2)
fixedValue = (fixedValue_1 + fixedValue_2) / 2
if direction_1 == 0:
return [[min(min_1, min_2), fixedValue], [max(max_1, max_2), fixedValue]]
else:
return [[fixedValue, min(min_1, min_2)], [fixedValue, max(max_1, max_2)]]
return
def findIntersection(line_1, line_2):
direction_1, fixedValue_1, min_1, max_1 = lineRange(line_1)
direction_2, fixedValue_2, min_2, max_2 = lineRange(line_2)
if direction_1 == 0:
return (fixedValue_2, fixedValue_1)
else:
return (fixedValue_1, fixedValue_2)
return
def extendLine(line, point):
direction, fixedValue, min_value, max_value = lineRange(line)
if direction == 0:
return ((min(min_value, point[direction]), fixedValue), (max(max_value, point[direction]), fixedValue))
else:
return ((fixedValue, min(min_value, point[direction])), (fixedValue, max(max_value, point[direction])))
return
def divideWalls(walls):
horizontalWalls = []
verticalWalls = []
for wall in walls:
if calcLineDirection(wall) == 0:
horizontalWalls.append(wall)
else:
verticalWalls.append(wall)
pass
continue
return horizontalWalls, verticalWalls
def connectWalls(walls, roomSegmentation, gap=3):
width = roomSegmentation.shape[1]
height = roomSegmentation.shape[0]
roomBoundary = np.zeros(roomSegmentation.shape, dtype=np.bool)
for direction in xrange(2):
for shift in [-1, 1]:
roomBoundary = np.logical_or(roomBoundary, roomSegmentation != np.roll(roomSegmentation, shift, axis=direction))
continue
continue
roomBoundary = roomBoundary.astype(np.uint8)
roomBoundary[0] = roomBoundary[-1] = roomBoundary[:, 0] = roomBoundary[:, -1] = 0
uncoveredBoundary = roomBoundary.copy()
wallGroups = divideWalls(walls)
wallMasks = [drawWallMask(walls, width, height, indexed=True, thickness=gap * 2) for walls in wallGroups]
uncoveredBoundary[wallMasks[0] >= 0] = 0
uncoveredBoundary[wallMasks[1] >= 0] = 0
uncoveredBoundary = cv2.dilate(uncoveredBoundary, np.ones((3, 3)), iterations=gap)
components = measure.label(uncoveredBoundary, background=0)
connectedWalls = []
for walls, wallMask in zip(wallGroups, wallMasks):
newWalls = copy.deepcopy(walls)
invalidWallIndices = []
for label in xrange(components.min() + 1, components.max() + 1):
mask = components == label
wallIndices = np.unique(wallMask[mask]).tolist()
if -1 in wallIndices:
wallIndices.remove(-1)
pass
if len(wallIndices) != 2:
continue
wall_1 = newWalls[wallIndices[0]]
wall_2 = newWalls[wallIndices[1]]
direction_1, fixedValue_1, min_1, max_1 = lineRange(wall_1)
direction_2, fixedValue_2, min_2, max_2 = lineRange(wall_2)
if direction_1 == direction_2:
if abs(fixedValue_1 - fixedValue_2) < gap:
newWallIndex = len(newWalls)
wallMask[wallMask == wallIndices[0]] = newWallIndex
wallMask[wallMask == wallIndices[1]] = newWallIndex
newWall = mergeLines(wall_1, wall_2)
newWalls.append(newWall)
invalidWallIndices.append(wallIndices[0])
invalidWallIndices.append(wallIndices[1])
pass
pass
# else:
# print(wall_1, wall_2)
# ys, xs = mask.nonzero()
# newWall = [[xs.min(), ys.min()], [xs.max(), ys.max()]]
# newWallDirection = calcLineDirection(newWall)
# if newWallDirection != direction_1 and newWall[1][1 - newWallDirection] - newWall[0][1 - newWallDirection] < gap * 2 + 1:
# fixedValue = (newWall[1][1 - newWallDirection] + newWall[0][1 - newWallDirection]) / 2
# newWall[1][1 - newWallDirection] = newWall[0][1 - newWallDirection] = fixedValue
# newWalls.append(newWall)
# pass
# pass
# else:
# assert(False)
# intersectionPoint = findIntersection(wall_1, wall_2)
# newWalls[wallIndices[0]] = extendLine(wall_1, intersectionPoint)
# newWalls[wallIndices[1]] = extendLine(wall_2, intersectionPoint)
# pass
continue
#print(invalidWallIndices)
invalidWallIndices = sorted(invalidWallIndices, key=lambda x: -x)
for index in invalidWallIndices:
del newWalls[index]
continue
connectedWalls += newWalls
continue
newWalls = connectedWalls
wallMask = drawWallMask(newWalls, width, height, indexed=True, thickness=gap * 2)
uncoveredBoundary = roomBoundary.copy()
uncoveredBoundary[wallMask >= 0] = 0
uncoveredBoundary = cv2.dilate(uncoveredBoundary, np.ones((3, 3)), iterations=gap)
components = measure.label(uncoveredBoundary, background=0)
#cv2.imwrite('test/segmentation.png', drawSegmentationImage(components))
for label in xrange(components.min() + 1, components.max() + 1):
mask = components == label
#cv2.imwrite('test/mask_' + str(label) + '.png', drawMaskImage(mask))
wallIndices = np.unique(wallMask[mask]).tolist()
if -1 in wallIndices:
wallIndices.remove(-1)
pass
lines = [newWalls[index] for index in wallIndices]
#cv2.imwrite('test/mask_' + str(label) + '_segment.png', drawMaskImage(mask))
#cv2.imwrite('test/mask_' + str(label) + '.png', drawMaskImage(drawWallMask(lines, width, height)))
horizontalLines, verticalLines = divideWalls(lines)
if len(horizontalLines) > 0 and len(verticalLines) > 0:
continue
#print(label, wallIndices, len(horizontalLines), len(verticalLines))
for direction, lines in enumerate([horizontalLines, verticalLines]):
if len(lines) < 2:
continue
#wall_1 = lines[0]
#wall_2 = lines[1]
#print(wall_1, wall_2)
#direction_1, fixedValue_1, min_1, max_1 = lineRange(wall_1)
#direction_2, fixedValue_2, min_2, max_2 = lineRange(wall_2)
#values = [line[direction] for line in lines]
#print(wall_1, wall_2)
ys, xs = mask.nonzero()
newWall = [[xs.min(), ys.min()], [xs.max(), ys.max()]]
newWallDirection = calcLineDirection(newWall)
#print(label, wallIndices, newWallDirection, direction, newWall[1][1 - newWallDirection] - newWall[0][1 - newWallDirection])
if newWallDirection != direction and newWall[1][1 - newWallDirection] - newWall[0][1 - newWallDirection] <= (gap * 2 + 2) * 2:
fixedValue = (newWall[1][1 - newWallDirection] + newWall[0][1 - newWallDirection]) / 2
newWall[1][1 - newWallDirection] = newWall[0][1 - newWallDirection] = fixedValue
values = [line[0][newWallDirection] for line in lines] + [line[1][newWallDirection] for line in lines]
min_value = min(values)
max_value = max(values)
newWall[0][newWallDirection] = min_value
newWall[1][newWallDirection] = max_value
newWalls.append(newWall)
#print('new orthogonal wall', newWall)
pass
continue
continue
wallMask = drawWallMask(newWalls, width, height, indexed=True, thickness=gap * 2)
uncoveredBoundary = roomBoundary.copy()
uncoveredBoundary[wallMask >= 0] = 0
uncoveredBoundary = cv2.dilate(uncoveredBoundary, np.ones((3, 3)), iterations=gap)
components = measure.label(uncoveredBoundary, background=0)
for label in xrange(components.min() + 1, components.max() + 1):
mask = components == label
wallIndices = np.unique(wallMask[mask]).tolist()
if -1 in wallIndices:
wallIndices.remove(-1)
pass
if len(wallIndices) != 2:
continue
wall_1 = newWalls[wallIndices[0]]
wall_2 = newWalls[wallIndices[1]]
#print(wall_1, wall_2)
direction_1 = calcLineDirection(wall_1)
direction_2 = calcLineDirection(wall_2)
if direction_1 != direction_2:
intersectionPoint = findIntersection(wall_1, wall_2)
newWalls[wallIndices[0]] = extendLine(wall_1, intersectionPoint)
newWalls[wallIndices[1]] = extendLine(wall_2, intersectionPoint)
pass
continue
# try:
# _, singularCorners = lines2Corners(newWalls, gap=gap, getSingularCorners=True)
# for _, singularCorner_1 in enumerate(singularCorners):
# for singularCorner_2 in singularCorners[_ + 1:]:
# wall_1 = newWalls[singularCorner_1[0]]
# wall_2 = newWalls[singularCorner_2[0]]
# corner_1 = wall_1[singularCorner_1[1]]
# corner_2 = wall_2[singularCorner_2[1]]
# if pointDistance(corner_1, corner_2) < (gap * 2 + 1) * 2:
# intersectionPoint = findIntersection(wall_1, wall_2)
# newWalls[singularCorner_1[0]] = extendLine(wall_1, intersectionPoint)
# newWalls[singularCorner_2[0]] = extendLine(wall_2, intersectionPoint)
# pass
# continue
# continue
# except:
# pass
return newWalls
def extractLines(lineMask, lengthThreshold=11, widthThreshold=5):
lines = []
components = measure.label(lineMask, background=0)
for label in xrange(components.min() + 1, components.max() + 1):
mask = components == label
ys, xs = mask.nonzero()
line = [[xs.min(), ys.min()], [xs.max(), ys.max()]]
direction = calcLineDirection(line)
if abs(line[1][1 - direction] - line[0][1 - direction]) > widthThreshold or abs(line[1][direction] - line[0][direction]) < lengthThreshold:
continue
fixedValue = (line[1][1 - direction] + line[0][1 - direction]) / 2
line[1][1 - direction] = line[0][1 - direction] = fixedValue
lines.append(line)
continue
return lines
def drawPoints(filename, width, height, points, backgroundImage=None, pointSize=5, pointColor=None):
colorMap = ColorPalette(NUM_CORNERS).getColorMap()
if np.all(np.equal(backgroundImage, None)):
image = np.zeros((height, width, 3), np.uint8)
else:
if backgroundImage.ndim == 2:
image = np.tile(np.expand_dims(backgroundImage, -1), [1, 1, 3])
else:
image = backgroundImage
pass
pass
no_point_color = pointColor is None
for point in points:
if no_point_color:
pointColor = colorMap[point[2] * 4 + point[3]]
pass
#print('used', pointColor)
#print('color', point[2] , point[3])
image[max(int(round(point[1])) - pointSize, 0):min(int(round(point[1])) + pointSize, height), max(int(round(point[0])) - pointSize, 0):min(int(round(point[0])) + pointSize, width)] = pointColor
continue
if filename != '':
cv2.imwrite(filename, image)
return
else:
return image
def drawPointsSeparately(path, width, height, points, backgroundImage=None, pointSize=5):
if np.all(np.equal(backgroundImage, None)):
image = np.zeros((height, width, 13), np.uint8)
else:
image = np.tile(np.expand_dims(backgroundImage, -1), [1, 1, 13])
pass
for point in points:
image[max(int(round(point[1])) - pointSize, 0):min(int(round(point[1])) + pointSize, height), max(int(round(point[0])) - pointSize, 0):min(int(round(point[0])) + pointSize, width), int(point[2] * 4 + point[3])] = 255
continue
for channel in xrange(13):
cv2.imwrite(path + '_' + str(channel) + '.png', image[:, :, channel])
continue
return
def drawLineMask(width, height, points, lines, lineWidth = 5, backgroundImage = None):
lineMask = np.zeros((height, width))
for lineIndex, line in enumerate(lines):
point_1 = points[line[0]]
point_2 = points[line[1]]
direction = calcLineDirectionPoints(points, line)
fixedValue = int(round((point_1[1 - direction] + point_2[1 - direction]) / 2))
minValue = int(min(point_1[direction], point_2[direction]))
maxValue = int(max(point_1[direction], point_2[direction]))
if direction == 0:
lineMask[max(fixedValue - lineWidth, 0):min(fixedValue + lineWidth + 1, height), minValue:maxValue + 1] = 1
else:
lineMask[minValue:maxValue + 1, max(fixedValue - lineWidth, 0):min(fixedValue + lineWidth + 1, width)] = 1
pass
continue
return lineMask
def drawLines(filename, width, height, points, lines, lineLabels = [], backgroundImage = None, lineWidth = 5, lineColor = None):
colorMap = ColorPalette(len(lines)).getColorMap()
if backgroundImage is None:
image = np.ones((height, width, 3), np.uint8) * 0
else:
if backgroundImage.ndim == 2:
image = np.stack([backgroundImage, backgroundImage, backgroundImage], axis=2)
else:
image = backgroundImage
pass
pass
for lineIndex, line in enumerate(lines):
point_1 = points[line[0]]
point_2 = points[line[1]]
direction = calcLineDirectionPoints(points, line)
fixedValue = int(round((point_1[1 - direction] + point_2[1 - direction]) / 2))
minValue = int(round(min(point_1[direction], point_2[direction])))
maxValue = int(round(max(point_1[direction], point_2[direction])))
if len(lineLabels) == 0:
if np.any(lineColor == None):
lineColor = np.random.rand(3) * 255
pass
if direction == 0:
image[max(fixedValue - lineWidth, 0):min(fixedValue + lineWidth + 1, height), minValue:maxValue + 1, :] = lineColor
else:
image[minValue:maxValue + 1, max(fixedValue - lineWidth, 0):min(fixedValue + lineWidth + 1, width), :] = lineColor
else:
labels = lineLabels[lineIndex]
isExterior = False
if direction == 0:
for c in xrange(3):
image[max(fixedValue - lineWidth, 0):min(fixedValue, height), minValue:maxValue, c] = colorMap[labels[0]][c]
image[max(fixedValue, 0):min(fixedValue + lineWidth + 1, height), minValue:maxValue, c] = colorMap[labels[1]][c]
continue
else:
for c in xrange(3):
image[minValue:maxValue, max(fixedValue - lineWidth, 0):min(fixedValue, width), c] = colorMap[labels[1]][c]
image[minValue:maxValue, max(fixedValue, 0):min(fixedValue + lineWidth + 1, width), c] = colorMap[labels[0]][c]
continue
pass
pass
continue
if filename == '':
return image
else:
cv2.imwrite(filename, image)
def drawRectangles(filename, width, height, points, rectangles, labels, lineWidth = 2, backgroundImage = None, rectangleColor = None):
colorMap = ColorPalette(NUM_ICONS).getColorMap()
if backgroundImage is None:
image = np.ones((height, width, 3), np.uint8) * 0
else:
image = backgroundImage
pass
for rectangleIndex, rectangle in enumerate(rectangles):
point_1 = points[rectangle[0]]
point_2 = points[rectangle[1]]
point_3 = points[rectangle[2]]
point_4 = points[rectangle[3]]
if len(labels) == 0:
if rectangleColor is None:
color = np.random.rand(3) * 255
else:
color = rectangleColor
else:
color = colorMap[labels[rectangleIndex]]
pass
x_1 = int(round((point_1[0] + point_3[0]) / 2))
x_2 = int(round((point_2[0] + point_4[0]) / 2))
y_1 = int(round((point_1[1] + point_2[1]) / 2))
y_2 = int(round((point_3[1] + point_4[1]) / 2))
cv2.rectangle(image, (x_1, y_1), (x_2, y_2), color=tuple(color.tolist()), thickness = 2)
# point_1 = (int(point_1[0]), int(point_1[1]))
# point_2 = (int(point_2[0]), int(point_2[1]))
# point_3 = (int(point_3[0]), int(point_3[1]))
# point_4 = (int(point_4[0]), int(point_4[1]))
# image[max(point_1[1] - lineWidth, 0):min(point_1[1] + lineWidth, height), point_1[0]:point_2[0] + 1, :] = color
# image[max(point_3[1] - lineWidth, 0):min(point_3[1] + lineWidth, height), point_3[0]:point_4[0] + 1, :] = color
# image[point_1[1]:point_3[1] + 1, max(point_1[0] - lineWidth, 0):min(point_1[0] + lineWidth, width), :] = color
# image[point_2[1]:point_4[1] + 1, max(point_2[0] - lineWidth, 0):min(point_2[0] + lineWidth, width), :] = color
continue
if filename == '':
return image
else:
cv2.imwrite(filename, image)
pass
def drawResultImage(width, height, result):
resultImage = drawLines('', width, height, result['wall'][0], result['wall'][1], result['wall'][2], None, lineWidth=3)
resultImage = drawLines('', width, height, result['door'][0], result['door'][1], [], resultImage, lineWidth=2, lineColor=0)
iconImage = drawRectangles('', width, height, result['icon'][0], result['icon'][1], result['icon'][2], lineWidth=2)
return resultImage, iconImage
def resizeResult(result, width, height, oriWidth=256, oriHeight=256):
result['wall'][0] = [[float(point[0]) / oriWidth * width, float(point[1]) / oriHeight * height, point[2], point[3]] for point in result['wall'][0]]
result['door'][0] = [[float(point[0]) / oriWidth * width, float(point[1]) / oriHeight * height, point[2], point[3]] for point in result['door'][0]]
result['icon'][0] = [[float(point[0]) / oriWidth * width, float(point[1]) / oriHeight * height, point[2], point[3]] for point in result['icon'][0]]
#result['room'][0] = [(cv2.resize(mask, (width, height), interpolation=cv2.INTER_NEAREST) > 0).astype(np.uint8) for mask in result['room'][0]]
return
def drawResultImageFinal(width, height, result):
colorMap = np.array([(224, 255, 192), (255, 160, 96), (255, 224, 128), (192, 255, 255), (192, 255, 255), (192, 255, 255), (192, 192, 224), (224, 255, 192), (255, 224, 224), (224, 224, 224)])
borderColorMap = np.array([(128, 192, 64), (192, 64, 64), (192, 128, 64), (0, 128, 192), (0, 128, 192), (0, 128, 192), (128, 64, 160), (128, 192, 64), (192, 64, 0), (255, 255, 255)])
colorMap = np.concatenate([np.full(shape=(1, 3), fill_value=0), colorMap, borderColorMap], axis=0).astype(np.uint8)
colorMap = colorMap[:, ::-1]
labelRoomMap = getLabelRoomMap()
roomSegmentation = np.zeros((height, width), dtype=np.int32)
roomsInfo = []
wall_dict = result['wall']
wallMask = drawWallMask([(wall_dict[0][line[0]], wall_dict[0][line[1]]) for line in wall_dict[1]], width, height, thickness=3)
roomRegions = measure.label(1 - wallMask, background=0)
#cv2.imwrite('test/' + str(dictIndex) + '_segmentation_regions.png', drawSegmentationImage(roomRegions))
backgroundIndex = roomRegions.min()
wallPoints = wall_dict[0]
roomLabels = {}
sizes = np.array([width, height])
for wallIndex, wallLabels in enumerate(wall_dict[2]):
wallLine = wall_dict[1][wallIndex]
lineDim = calcLineDim(wallPoints, wallLine)
#print('wall', wallIndex, wallPoints[wallLine[0]][:2], wallPoints[wallLine[1]][:2])
center = np.round((np.array(wallPoints[wallLine[0]][:2]) + np.array(wallPoints[wallLine[1]][:2])) / 2).astype(np.int32)
for c in xrange(2):
direction = c * 2 - 1
if lineDim == 1:
direction *= -1
pass
point = center
for offset in xrange(10):
point[1 - lineDim] += direction
if point[lineDim] < 0 or point[lineDim] >= sizes[lineDim]:
break
roomIndex = roomRegions[point[1], point[0]]
if roomIndex != backgroundIndex:
#print(roomIndex, wallLabels[c], wallLabels, point.tolist())
#mask = roomRegions == roomIndex
#mask = cv2.dilate(mask.astype(np.uint8), np.ones((3, 3)), iterations=1)
#roomSegmentation[mask] = wallLabels[c]
#rooms[wallLabels[c]].append(cv2.dilate(mask.astype(np.uint8), np.ones((3, 3)), iterations=wallLineWidth))
#roomRegions[mask] = backgroundIndex
if roomIndex not in roomLabels:
roomLabels[roomIndex] = {}
pass
roomLabels[roomIndex][wallLabels[c]] = True
break
continue
continue
continue
rooms = []
indexMap = {}
for roomIndex, labels in roomLabels.iteritems():
#print(roomIndex, labels)
if roomIndex == roomRegions[0][0]:
continue
indexMap[roomIndex] = len(rooms)
mask = roomRegions == roomIndex
mask = cv2.dilate(mask.astype(np.uint8), np.ones((3, 3)), iterations=3)
# if 7 in labels and 2 not in labels:
# labels[2] = True
# pass
# if 5 in labels and 3 not in labels:
# labels[3] = True
# pass
# if 9 in labels and 1 not in labels:
# labels[1] = True
# pass
rooms.append((mask, labels))
continue
wallLineWidth = 5
# foregroundMask = roomSegmentation > 0
# foregroundMask = cv2.dilate(foregroundMask, np.ones((3, 3)), iterations=wallLineWidth)
# roomSegmentation[foregroundMask] =
for mask, labels in rooms:
label = min([label for label in labels])
if label < 0:
continue
kernel = np.zeros((3, 3))
kernel[1:, 1:] = 1
#mask = cv2.erode(mask.astype(np.uint8), kernel.astype(np.uint8), iterations=1)
erodedMask = cv2.erode(mask, np.ones((3, 3)), iterations=wallLineWidth)
roomSegmentation[mask.astype(np.bool)] = label + 10
roomSegmentation[erodedMask.astype(np.bool)] = label
continue
image = colorMap[roomSegmentation.reshape(-1)].reshape((height, width, 3))
pointColor = tuple((np.array([0.3, 0.3, 0.9]) * 255).astype(np.uint8).tolist())
for wallLine in result['wall'][1]:
for pointIndex in wallLine:
point = result['wall'][0][pointIndex]
cv2.circle(image, (int(point[0]), int(point[1])), color=pointColor, radius=8, thickness=-1)
cv2.circle(image, (int(point[0]), int(point[1])), color=(255, 255, 255), radius=4, thickness=-1)
continue
continue
lineSegmentLength = 20.0
for doorLine in result['door'][1]:
point_1 = np.array(result['door'][0][doorLine[0]][:2]).astype(np.float32)
point_2 = np.array(result['door'][0][doorLine[1]][:2]).astype(np.float32)
lineDim = calcLineDim(result['door'][0], doorLine)
for i in xrange(int(abs(point_1[lineDim] - point_2[lineDim]) / lineSegmentLength + 1)):
ratio = i * lineSegmentLength / abs(point_1[lineDim] - point_2[lineDim])
if ratio >= 1:
break
startPoint = point_1 + ratio * (point_2 - point_1)
ratio = (i + 0.5) * lineSegmentLength / abs(point_1[lineDim] - point_2[lineDim])
ratio = min(ratio, 1)
endPoint = point_1 + ratio * (point_2 - point_1)
cv2.line(image, (startPoint[0], startPoint[1]), (endPoint[0], endPoint[1]), color=(0, 0, 0), thickness=4)
continue
for point in [point_1, point_2]:
startPoint = point.copy()
startPoint[1 - lineDim] += lineSegmentLength / 2
endPoint = point.copy()
endPoint[1 - lineDim] -= lineSegmentLength / 2
cv2.line(image, (startPoint[0], startPoint[1]), (endPoint[0], endPoint[1]), color=(0, 0, 0), thickness=2)
continue
continue
labelIconMap = getLabelIconMap()
iconPos = []
for iconIndex, (icon, label) in enumerate(zip(result['icon'][1], result['icon'][2])):
name = labelIconMap[label + 1]
iconImage = cv2.imread('icons/' + name + '.jpg')
points = [result['icon'][0][pointIndex] for pointIndex in icon]
x_1 = int(round((points[0][0] + points[2][0]) / 2))
x_2 = int(round((points[1][0] + points[3][0]) / 2))
y_1 = int(round((points[0][1] + points[1][1]) / 2))
y_2 = int(round((points[2][1] + points[3][1]) / 2))
iconSize = iconImage.shape #(y, x)
#print('icon_size', iconSize)
icon_is_landscape = iconSize[1] > iconSize[0]
slot_size = (x_2 - x_1 + 1, y_2 - y_1 + 1)
slot_center = np.array((x_1 + slot_size[0]/2, y_1 + slot_size[1] / 2))
slot_is_landscape = slot_size[0] > slot_size[1]
min_dist = float('inf')
line = None
close_line_dim = 0
for wallIndex, wallLabels in enumerate(wall_dict[2]):
wallLine = wall_dict[1][wallIndex]
lineDim = calcLineDim(wallPoints, wallLine)
center = np.round((np.array(wallPoints[wallLine[0]][:2]) + np.array(wallPoints[wallLine[1]][:2])) / 2).astype(np.int32)
point1=np.array(wallPoints[wallLine[0]][:2])
point2 = np.array(wallPoints[wallLine[1]][:2])
n = point2 - point1
dist = np.dot((point1 - slot_center) - np.dot((point1 - slot_center), n), n)
#print('indices', wallIndex, wallLabels, wallLine)
# print('points', wallPoints[wallLine[0]], wallPoints[wallLine[1]])
# pass
if dist < 5:
min_dist = dist
line = (point1, point2)
close_line_dim = lineDim
pass
pass
#sys.stderr.write("{}, {}, {}, {}, {}\n".format(y_1, y_2, x_1, x_2, iconImage.shape))
print('has line: ', line, name, close_line_dim)
if name == "toilet":
if line is not None:
if close_line_dim == 0: #x
y_pos = (line[0][1] + line[1][1]) / 2
if y_pos > y_2: #toilet is below
print('first case rot')
iconImage = rotateImage(iconImage, 2)
elif y_pos < y_1: # toilet is above
pass # do nothing
else:
print("bad case", x_1, x_2, y_1, y_2, line)
pass
else: #y
x_pos = (line[0][0] + line[1][0])/2
print('here', x_pos, x_1, x_2)
if x_pos > x_2: #toilet is to the left
pass # do nothing
elif x_pos < x_1: # toilet is to the right
print(slot_is_landscape, icon_is_landscape)
if slot_is_landscape: