-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
225 lines (187 loc) · 9.07 KB
/
main.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
import cv2
import numpy as np
import os
class RoomSegmentation:
def __init__(self, m=1, b=5, min_dist=1600, min_l=100, image_name=None, image_file=None):
self.m_thresh = m
self.b_thresh = b
self.l_thresh = min_dist
self.first_line_thresh = min_l
if image_file is None:
self.img = cv2.imread(image_name, 0)
self.img = self.convert_to_bw(self.img, 200)
else:
self.img = image_file
self.original_img = self.img.copy()
if image_name is None:
os.mkdir("Camera")
self.dir = "Camera"
else:
if image_name.split('.')[1].split('/')[-1] not in os.listdir('./results'):
os.mkdir('./results/' + image_name.split('.')[1].split('/')[-1])
self.dir = './results/' + image_name.split('.')[1].split('/')[-1]
@staticmethod
def convert_to_sqr(img):
row, col = img.shape
if row > col:
zero_side_image = np.zeros((row, (row - col) // 2), np.uint8)
new_image = np.concatenate((zero_side_image, img, zero_side_image), axis=1)
elif col > row:
zero_side_image = np.zeros(((col - row) // 2, col), np.uint8)
new_image = np.concatenate((zero_side_image, img, zero_side_image), axis=0)
else:
new_image = img
return new_image
def run(self, name):
kernel = np.ones((2, 2), np.uint8)
self.img = self.convert_to_sqr(self.img)
cv2.imwrite(self.dir + "/0-SquareImage.png", self.img)
detected_lines = self.line_segmentation(self.img, '/1-DetectedLines')
extended_lines = self.extend_lines(detected_lines)
extended_lined_img = cv2.createLineSegmentDetector(1).drawSegments(self.img, extended_lines)
cv2.imwrite(self.dir + "/2-ExtendedLines.png", extended_lined_img)
lined_img = cv2.cvtColor(extended_lined_img, cv2.COLOR_BGR2GRAY)
rotated_img = self.rotate_img(lined_img, 90)
detected_lines = self.line_segmentation(rotated_img, '/3-DetectedLines')
extended2_lines = self.extend_lines(detected_lines)
extended2_lined_img = cv2.createLineSegmentDetector(1).drawSegments(rotated_img, extended2_lines)
cv2.imwrite(self.dir + "/4-ExtendedLines.png", extended2_lined_img)
lined_img = cv2.cvtColor(extended2_lined_img, cv2.COLOR_BGR2GRAY)
lined_img = self.convert_to_bw(lined_img, 200)
lined_img = cv2.erode(lined_img, kernel, iterations=1)
lined_img = cv2.morphologyEx(lined_img, cv2.MORPH_OPEN, kernel, iterations=5)
cv2.imwrite(self.dir + "/5-MorphologyErodeOpen.png", lined_img)
# circles = self.circle_detection(lined_img)
second_rotated_img = self.rotate_img(lined_img, 270)
components = self.connected_component(second_rotated_img)
# original = self.transfer_to_original(components)
# cv2.imshow('components' + name, components)
# cv2.imwrite('output_%s.png' % name, components)
cv2.imwrite(self.dir + '/6-output_%s.png' % name, components)
# cv2.waitKey()
@staticmethod
def circle_detection(image):
circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 1, 20,
param1=50, param2=30, minRadius=50, maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
# draw the outer circle
cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 0), 2)
return image
@staticmethod
def convert_to_bw(image, thresh):
for i in range(image.shape[0]):
for j in range(image.shape[1]):
if image[i][j] > thresh:
image[i][j] = 255
else:
image[i][j] = 0
return image
def line_segmentation(self, img, name):
lsd = cv2.createLineSegmentDetector(1)
lines = lsd.detect(img)[0]
cv2.imwrite(self.dir + name + '.png', lsd.drawSegments(img, lines))
detected_lines = []
for line in lines:
x1 = line[0][0]
y1 = line[0][1]
x2 = line[0][2]
y2 = line[0][3]
if (x2 - x1) ** 2 + (y2 - y1) ** 2 < self.first_line_thresh:
continue
if x2 - x1 == 0:
continue
m = (y2 - y1) / (x2 - x1)
b = y2 - m * x2
detected_lines.append([m, b, (x1, y1), (x2, y2)])
return detected_lines
def extend_lines(self, lines):
temp_lines = []
final_lines = []
for i in range(len(lines)):
for j in range(i, len(lines)):
l1 = lines[i]
l2 = lines[j]
if abs(l1[0] - l2[0]) < self.m_thresh and \
abs(l1[1] - l2[1]) < self.b_thresh:
temp_lines.append([[l1[2][0], l1[2][1], l2[2][0], l2[2][1]]])
temp_lines.append([[l1[2][0], l1[2][1], l2[3][0], l2[3][1]]])
temp_lines.append([[l1[3][0], l1[3][1], l2[2][0], l2[2][1]]])
temp_lines.append([[l1[3][0], l1[3][1], l2[3][0], l2[3][1]]])
temp_lines.sort(key=lambda k: (k[0][0] - k[0][2]) ** 2 + (k[0][1] - k[0][3]) ** 2)
k = temp_lines[0]
if (k[0][0] - k[0][2]) ** 2 + (k[0][1] - k[0][3]) ** 2 < self.l_thresh:
final_lines.append(temp_lines[0])
temp_lines = []
return np.asarray(final_lines)
@staticmethod
def draw_lines(img, lines, name):
tmp = cv2.createLineSegmentDetector(1).drawSegments(img, lines)
cv2.imshow(name, tmp)
@staticmethod
def connected_component(img):
ret, labels = cv2.connectedComponents(img, connectivity=8, ltype=4)
# Map component labels to hue val
label_hue = np.uint8(360 * labels / np.max(labels))
blank_ch = 255 * np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])
# cvt to BGR for display
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
# set bg label to black
labeled_img[label_hue == 0] = 0
return labeled_img
@staticmethod
def rotate_img(img, theta):
rows, cols = img.shape
m = cv2.getRotationMatrix2D((cols / 2, rows / 2), theta, 1)
dst = cv2.warpAffine(img, m, (cols, rows))
return dst
def transfer_to_original(self, final_img):
sqr_org_img = self.convert_to_sqr(self.original_img)
w, h = sqr_org_img.shape
blank_ch = np.zeros((w, h, 3))
for i in range(w):
for j in range(h):
if np.sum(final_img[i][j]) != 0:
blank_ch[i][j] = final_img[i][j]
else:
val = sqr_org_img[i][j]
blank_ch[i][j] = [val, val, val]
return blank_ch
if __name__ == '__main__':
office_a = RoomSegmentation(m=1, b=70, min_dist=1600, min_l=10,
image_file=None, image_name='./Data/office_a.png')
office_a.run('office_a')
# office_b = RoomSegmentation(m=1, b=5, min_dist=1600, min_l=100,
# image_file=None, image_name='./Data/office_b.png')
# office_b.run('office_b')
# office_c = RoomSegmentation(m=1, b=5, min_dist=1600, min_l=100,
# image_file=None, image_name='./Data/office_c.png')
# office_c.run('office_c')
# office_d = RoomSegmentation(m=1, b=15, min_dist=1600, min_l=100,
# image_file=None, image_name='./Data/office_d.png')
# office_d.run('office_d')
# office_e = RoomSegmentation(m=1, b=20, min_dist=1600, min_l=100,
# image_file=None, image_name='./Data/office_e.png')
# office_e.run('office_e')
# office_f = RoomSegmentation(m=1, b=15, min_dist=1600, min_l=100,
# image_file=None, image_name='./Data/office_f.png')
# office_f.run('office_f')
# office_g = RoomSegmentation(m=1, b=20, min_dist=6400, min_l=100,
# image_file=None, image_name='./Data/office_g.png')
# office_g.run('office_g')
# office_h = RoomSegmentation(m=1, b=100, min_dist=4900, min_l=1,
# image_file=None, image_name='./Data/office_h.png')
# office_h.run('office_h')
# office_i = RoomSegmentation(m=1, b=15, min_dist=3600, min_l=100,
# image_file=None, image_name='./Data/office_i.png')
# office_i.run('office_i')
# nlb = RoomSegmentation(m=0.04, b=15, min_dist=1600, min_l=60,
# image_file=None, image_name='./Data/NLB.png')
# nlb.run('NLB')
# Freiburg101_scan = RoomSegmentation(m=1, b=6, min_dist=1600, min_l=100,
# image_file=None, image_name='./Data/Freiburg101_scan.png')
# Freiburg101_scan.run('Freiburg101_scan')
# Freiburg79_scan = RoomSegmentation(m=1, b=20, min_dist=1600, min_l=100,
# image_file=None, image_name='./Data/Freiburg79_scan.png')
# Freiburg79_scan.run('Freiburg79_scan')