6 import matplotlib.cm
as cmx
7 import matplotlib.colors
as colors
8 import matplotlib.patches
as patches
9 import matplotlib.path
as mplPath
11 from descartes
import PolygonPatch
12 from matplotlib
import pyplot
as plt
13 from random
import randint
17 default_format =
".pdf"
18 normal_format =
".png"
26 fig, ax = plt.subplots()
27 fig.set_size_inches(width, height)
28 ax = plt.Axes(fig, [0., 0., 1., 1.])
34 def draw_image(image, name, size, format=default_format, filepath='.'):
35 rospy.loginfo(
"[rose2] ", name)
37 title = os.path.join(filepath, name)
39 plt.savefig(title + format)
40 plt.savefig(title + normal_format)
43 def draw_canny(image, name, format=default_format, filepath='.'):
44 rospy.loginfo(
"[rose2] {}".format(name))
46 title = os.path.join(filepath, name)
47 ax.imshow(image, cmap=
'Greys')
48 plt.savefig(title + format)
49 plt.savefig(title + normal_format)
52 def draw_hough(image, walls, name, size, format=default_format, filepath='.'):
53 rospy.loginfo(
"[rose2] {}".format(name))
55 title = os.path.join(filepath, name)
57 if len(img.shape) == 2:
58 img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
59 for x1, y1, x2, y2
in walls:
60 cv2.line(img, (x1, y1), (x2, y2), (randint(0, 255), randint(0, 255), randint(0, 255)), 3)
61 ax.imshow(img, cmap=
'Greys')
62 plt.savefig(title + format)
63 plt.savefig(title + normal_format)
66 def draw_walls(walls, name, size, format=default_format, filepath='.'):
67 rospy.loginfo(
"[rose2] {}".format(name))
71 title = os.path.join(filepath, name)
72 img = np.zeros((size[1], size[0], 3), np.uint8)
73 img[:, :] = (255, 255, 255)
80 x_coordinates.extend((x1, x2))
81 y_coordinates.extend((y1, y2))
82 ax.plot(x_coordinates, y_coordinates, color=np.random.rand(3,), linewidth=1)
85 plt.savefig(title + format)
86 plt.savefig(title + normal_format)
89 def draw_contour(vertices, name, size, format=default_format, filepath='.'):
91 rospy.loginfo(
"[rose2] {}".format(name))
93 title = os.path.join(filepath, name)
94 img = np.zeros((size[1], size[0], 3), np.uint8)
95 img[:, :] = (255, 255, 255)
97 bbPath = mplPath.Path(vertices)
98 patch = patches.PathPatch(bbPath, facecolor=
'orange', lw=2)
102 plt.savefig(title + format)
103 plt.savefig(title + normal_format)
107 rospy.loginfo(
"[rose2] {}".format(name))
111 title = os.path.join(filepath, name)
112 img = np.zeros((size[1], size[0], 3), np.uint8)
113 img[:, :] = (255, 255, 255)
115 num_of_colors = len(set(angular_clusters))
116 cm = plt.get_cmap(
"nipy_spectral")
117 cNorm = colors.Normalize(vmin=0, vmax=num_of_colors)
118 scalar_map = cmx.ScalarMappable(norm=cNorm, cmap=cm)
119 for index, c
in enumerate(np.random.permutation(list(set(angular_clusters)))):
121 if wall.angular_cluster == c:
122 x_coordinates.extend((wall.x1, wall.x2))
123 y_coordinates.extend((wall.y1, wall.y2))
124 ax.plot(x_coordinates, y_coordinates, color=colors.rgb2hex(scalar_map.to_rgba(index)), linewidth=1)
127 plt.savefig(title + format)
128 plt.savefig(title + normal_format)
132 rospy.loginfo(
"[rose2] {}".format(name))
136 title = os.path.join(filepath, name)
137 img = np.zeros((size[1], size[0], 3), np.uint8)
138 img[:, :] = (255, 255, 255)
140 for wall
in representatives_segments:
145 x_coordinates.extend((x1, x2))
146 y_coordinates.extend((y1, y2))
147 ax.plot(x_coordinates, y_coordinates, color=
'k', linewidth=1)
150 plt.savefig(title + format)
151 plt.savefig(title + normal_format)
155 rospy.loginfo(
"[rose2] {}".format(name))
159 title = os.path.join(filepath, name)
160 img = np.zeros((size[1], size[0], 3), np.uint8)
161 img[:, :] = (255, 255, 255)
163 num_of_colors = len(set(wall_clusters))
164 cm = plt.get_cmap(
"nipy_spectral")
165 cNorm = colors.Normalize(vmin=0, vmax=num_of_colors)
166 scalar_map = cmx.ScalarMappable(norm=cNorm, cmap=cm)
167 for index, c
in enumerate(np.random.permutation(list(set(wall_clusters)))):
169 if wall.wall_cluster == c:
170 x_coordinates.extend((wall.x1, wall.x2))
171 y_coordinates.extend((wall.y1, wall.y2))
172 ax.plot(x_coordinates, y_coordinates, color=colors.rgb2hex(scalar_map.to_rgba(index)), linewidth=1)
175 plt.savefig(title + format)
176 plt.savefig(title + normal_format)
180 rospy.loginfo(
"[rose2] {}".format(name))
184 title = os.path.join(filepath, name)
185 img = np.zeros((size[1], size[0], 3), np.uint8)
186 img[:, :] = (255, 255, 255)
188 num_of_colors = len(set(spatial_clusters))
189 cm = plt.get_cmap(
"nipy_spectral")
190 cNorm = colors.Normalize(vmin=0, vmax=num_of_colors)
191 scalar_map = cmx.ScalarMappable(norm=cNorm, cmap=cm)
192 for index, c
in enumerate(np.random.permutation(list(set(spatial_clusters)))):
194 if wall.spatial_cluster == c:
195 x_coordinates.extend((wall.x1, wall.x2))
196 y_coordinates.extend((wall.y1, wall.y2))
197 ax.plot(x_coordinates, y_coordinates, color=colors.rgb2hex(scalar_map.to_rgba(index)),
201 plt.savefig(title + format)
202 plt.savefig(title + normal_format)
206 rospy.loginfo(
"[rose2] {}".format(name))
210 title = os.path.join(filepath, name)
211 img = np.zeros((size[1], size[0], 3), np.uint8)
212 img[:, :] = (255, 255, 255)
215 x_coordinates.append(wall.x1)
216 x_coordinates.append(wall.x2)
217 y_coordinates.append(wall.y1)
218 y_coordinates.append(wall.y2)
219 ax.plot(x_coordinates, y_coordinates, color=
'k', linewidth=1.5)
222 for segment
in extended_segments:
223 x_coordinates.append(segment.x1)
224 x_coordinates.append(segment.x2)
225 y_coordinates.append(segment.y1)
226 y_coordinates.append(segment.y2)
227 ax.plot(x_coordinates, y_coordinates, color=
'r', linewidth=1)
230 plt.savefig(title + format)
231 plt.savefig(title + normal_format)
234 def draw_dbscan(labels, faces, polygon_faces, edges, contours, name, size, format=default_format, filepath='.'):
236 rospy.loginfo(
"[rose2] {}".format(name))
238 title = os.path.join(filepath, name)
239 img = np.zeros((size[1], size[0], 3), np.uint8)
240 img[:, :] = (255, 255, 255)
243 cmap = matplotlib.cm.get_cmap(
'Paired')
244 for label
in set(labels):
245 rgba = cmap(random.random())
247 col = matplotlib.colors.rgb2hex(rgba)
248 assigned_color.append(col)
249 for index, l
in enumerate(labels):
252 f_poly = polygon_faces[index]
253 f_patch = PolygonPatch(f_poly, fc=col, ec=
'BLACK')
254 ax.add_patch(f_patch)
262 x_text = sum_x/(2*len(f.borders))
263 y_text = sum_y/(2*len(f.borders))
264 ax.text(x_text, y_text, str(l), fontsize=8)
268 if edge.weight >= 0.1:
269 x_coordinates.append(edge.x1)
270 x_coordinates.append(edge.x2)
271 y_coordinates.append(edge.y1)
272 y_coordinates.append(edge.y2)
273 ax.plot(x_coordinates,y_coordinates, color=
'k', linewidth=4.0)
279 x_coordinates.append(c1[0][0])
280 y_coordinates.append(c1[0][1])
281 ax.plot(x_coordinates, y_coordinates, color=
'0.8', linewidth=3.0)
284 plt.savefig(title + format)
285 plt.savefig(title + normal_format)
286 return assigned_color, fig, ax
289 def draw_edges(edges, walls, threshold, name, size, format=default_format, filepath='.'):
290 rospy.loginfo(
"[rose2] {}".format(name))
294 title = os.path.join(filepath, name)
295 img = np.zeros((size[1], size[0], 3), np.uint8)
296 img[:, :] = (255, 255, 255)
299 x_coordinates.append(wall.x1)
300 x_coordinates.append(wall.x2)
301 y_coordinates.append(wall.y1)
302 y_coordinates.append(wall.y2)
303 ax.plot(x_coordinates, y_coordinates, color=
'k', linewidth=1.5)
306 for segment
in edges:
307 color = (randint(0, 255)/255, randint(0, 255)/255, randint(0, 255)/255)
308 if segment.weight > threshold:
309 x_coordinates.append(segment.x1)
310 x_coordinates.append(segment.x2)
311 y_coordinates.append(segment.y1)
312 y_coordinates.append(segment.y2)
313 ax.plot(x_coordinates, y_coordinates, color=color, linewidth=3)
316 plt.savefig(title + format)
317 plt.savefig(title + normal_format)
320 def draw_cells(polygon_cells, polygons_out, partial_polygons, name, size, format=default_format, filepath='.',):
322 rospy.loginfo(
"[rose2] {}".format(name))
324 title = os.path.join(filepath, name)
325 img = np.zeros((size[1], size[0], 3), np.uint8)
326 img[:, :] = (255, 255, 255)
328 for index, s
in enumerate(polygon_cells):
329 f_patch = PolygonPatch(s, fc=
'orange', ec=
'BLACK')
330 ax.add_patch(f_patch)
333 for index, s
in enumerate(partial_polygons):
334 f_patch = PolygonPatch(s, fc=
'green', ec=
'BLACK')
335 ax.add_patch(f_patch)
338 for index, s
in enumerate(polygons_out):
339 f_patch = PolygonPatch(s, fc=
'#FFFFFF', ec=
'BLACK')
340 ax.add_patch(f_patch)
343 plt.savefig(title + format)
344 plt.savefig(title + normal_format)
348 def draw_rooms(rooms, colors, name, size, format=default_format, filepath='.'):
350 for i, color
in enumerate(colors):
354 new_color = np.random.rand(3,)
356 new_colors.append(new_color)
358 rospy.loginfo(
"[rose2] {}".format(name))
360 title = os.path.join(filepath, name)
361 img = np.zeros((size[1], size[0], 3), np.uint8)
362 img[:, :] = (255, 255, 255)
365 for index, s
in enumerate(rooms):
366 f_patch = PolygonPatch(s, fc=new_colors[index], ec=
'none')
367 patches.append(f_patch)
368 ax.add_patch(f_patch)
371 plt.savefig(title + format)
372 plt.savefig(title + normal_format)
373 return fig, ax, patches
377 rospy.loginfo(
"[rose2] {}".format(name))
379 title = os.path.join(filepath, name)
381 if len(im.shape) == 3:
382 image = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
383 th, image = cv2.threshold(image, 230, 255, cv2.THRESH_BINARY)
384 cv2.imwrite(filepath +
'0_Map_tmp.png', image, [cv2.IMWRITE_PNG_COMPRESSION,1])
385 final_image = Image.open(filepath +
'0_Map_tmp.png').convert(
'RGBA')
386 room_image = Image.open(filepath +
'8b_rooms_th1.png')
387 pix_data_final = final_image.load()
388 pix_data_room = room_image.load()
389 for y
in range(final_image.size[1]):
390 for x
in range(final_image.size[0]):
391 if pix_data_final[x, y] == (255, 255, 255, 255):
392 pix_data_final[x, y] = pix_data_room[x, y]
393 final_image.save(title + normal_format)
396 return title + normal_format
400 rospy.loginfo(
"[rose2] {}".format(name))
402 title = os.path.join(filepath, name)
404 if len(image.shape) == 3:
405 image = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
406 th, image2 = cv2.threshold(image, 230, 255, cv2.THRESH_BINARY)
407 if len(image.shape) == 3:
408 image = cv2.cvtColor(image2, cv2.COLOR_GRAY2RGB)
409 for segment
in extended_segments:
414 cv2.line(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 3)
415 cv2.imwrite(filepath +
'0_Map_tmp_lines.png', image, [cv2.IMWRITE_PNG_COMPRESSION])
416 final_image = Image.open(filepath +
'0_Map_tmp_lines.png').convert(
'RGBA')
417 room_image = Image.open(filepath +
'8b_rooms_th1.png')
418 pix_data_final = final_image.load()
419 pix_data_room = room_image.load()
420 for y
in range(final_image.size[1]):
421 for x
in range(final_image.size[0]):
422 if pix_data_final[x, y] == (255, 255, 255, 255):
423 pix_data_final[x, y] = pix_data_room[x, y]
424 final_image.save(title + normal_format)
430 rospy.loginfo(
"[rose2] {}".format(name))
432 title = os.path.join(filepath, name)
435 if len(image.shape) == 3:
436 im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
437 th, image = cv2.threshold(im, 230, 255, cv2.THRESH_BINARY)
438 th, image2 = cv2.threshold(im2, 150, 255, cv2.THRESH_BINARY)
439 cv2.imwrite(filepath +
'0_Map_tmp.png', image, [cv2.IMWRITE_PNG_COMPRESSION,1])
440 cv2.imwrite(filepath +
'0_Map_tmp2.png', image2, [cv2.IMWRITE_PNG_COMPRESSION,1])
441 threshold_image = Image.open(filepath +
'0_Map_tmp.png').convert(
'RGBA')
442 th_image2 = Image.open(filepath +
'0_Map_tmp2.png').convert(
'RGBA')
443 room_image = Image.open(filepath +
'8b_rooms_th1.png').resize(size)
444 final_image = Image.open(filepath +
'0_Map_tmp.png').resize(size).convert(
'RGBA')
445 pix_data_threshold = threshold_image.load()
446 pix_data_room = room_image.load()
447 pix_data_final = final_image.load()
448 pix_data_threshold2 = th_image2.load()
449 for y
in range(threshold_image.size[1]):
450 for x
in range(threshold_image.size[0]):
451 if pix_data_threshold[x, y] == (255, 255, 255, 255):
452 pix_data_final[x, y] = pix_data_room[x, y]
454 if pix_data_room[x, y] != (255, 255, 255, 255)
and pix_data_threshold2[x, y] == (255, 255, 255, 255):
455 pixel = pix_data_room[x, y]
456 pix_data_final[x, y] = (pixel[0], pixel[1], pixel[2], 125)
458 final_image.save(title + normal_format)
463 def draw_sides(edges, name, size, format=default_format, filepath='.'):
464 rospy.loginfo(
"[rose2] {}".format(name))
468 title = os.path.join(filepath, name)
469 img = np.zeros((size[1], size[0], 3), np.uint8)
470 img[:, :] = (255, 255, 255)
473 if edge.weight >= 0.3:
474 x_coordinates.append(edge.x1)
475 x_coordinates.append(edge.x2)
476 y_coordinates.append(edge.y1)
477 y_coordinates.append(edge.y2)
478 ax.plot(x_coordinates, y_coordinates, color=
'k', linewidth=4.0)
481 plt.savefig(title + format)
482 plt.savefig(title + normal_format)
488 title = os.path.join(filepath, name)
489 img = np.zeros((size[1], size[0], 3), np.uint8)
490 img[:, :] = (255, 255, 255)
494 num_of_colors = len(set(spatial_clusters))
495 cm = plt.get_cmap(
"nipy_spectral")
496 c_norm = colors.Normalize(vmin=0, vmax=num_of_colors)
497 scalar_map = cmx.ScalarMappable(norm=c_norm, cmap=cm)
498 for index, c
in enumerate(np.random.permutation(list(set(spatial_clusters)))):
499 for wall
in wall_list:
500 if wall.spatial_cluster == c:
501 x_coordinates.extend((wall.x1, wall.x2))
502 y_coordinates.extend((wall.y1, wall.y2))
503 ax.plot(x_coordinates, y_coordinates, color=colors.rgb2hex(scalar_map.to_rgba(index)), linewidth=2.0)
507 for index, s
in enumerate(rooms):
508 f_patch = PolygonPatch(s, fc=colori[index], ec=
'BLACK')
509 ax.add_patch(f_patch)
512 plt.savefig(title + format)
513 plt.savefig(title + normal_format)