postprocessing.py
Go to the documentation of this file.
1 from PIL import Image
2 import rospy
3 
4 def get_colors(pix_data, map_image):
5  colors = []
6  dictionary = {}
7  # type dictionary
8  for y in range(map_image.size[1]):
9  for x in range(map_image.size[0]):
10  if pix_data[x, y] != (0, 0, 0, 255):
11  color = pix_data[x, y]
12  if color not in colors:
13  colors.append(color)
14  dictionary[color] = 1
15  else:
16  points = dictionary[color]
17  points += 1
18  dictionary[color] = points
19  return dictionary, colors
20 
21 
22 def remove_small_color(dictionary, colors, th):
23  keys = dictionary.keys()
24  for key in list(keys):
25  if dictionary[key] <= th:
26  colors.remove(key)
27 
28 
29 def get_color(pos, pix_data, colors):
30  color = pix_data[pos]
31  if color in colors:
32  return True, color
33  return False, None
34 
35 
36 def check_position1(pos, size):
37  if pos[0] >= size[0] - 1:
38  return False
39  return True
40 
41 
42 def check_position2(pos):
43  if pos[0] <= 0:
44  return False
45  return True
46 
47 
48 def check_position3(pos, size):
49  if pos[1] >= size[1] - 1:
50  return False
51  return True
52 
53 
54 def check_position4(pos):
55  if pos[1] <= 0:
56  return False
57  return True
58 
59 
60 def check_position5(pos, size):
61  if pos[1] >= size[1] - 1 or pos[0] >= size[0] - 1:
62  return False
63  return True
64 
65 
66 def check_position6(pos, size):
67  if pos[1] >= size[1] - 1 or pos[0] <= 0:
68  return False
69  return True
70 
71 
72 def check_position7(pos, size):
73  if pos[1] <= 0 or pos[0] >= size[0] - 1:
74  return False
75  return True
76 
77 
78 def check_position8(pos):
79  if pos[1] <= 0 or pos[0] <= 0:
80  return False
81  return True
82 
83 
84 def compute_distance(pix_data, position, size, colors):
85  p1, p2, p3, p4, p5, p6, p7, p8 = True, True, True, True, True, True, True, True
86  pos_x = position[0]
87  pos_y = position[1]
88  if pos_x == size[0] - 1:
89  p1, p5, p7 = False, False, False
90  if pos_x == 0:
91  p2, p6, p8 = False, False, False
92  if pos_y == size[1] - 1:
93  p3, p5, p6 = False, False, False
94  if pos_y == 0:
95  p4, p7, p8 = False, False, False
96  ind = 0
97  color = None
98  while True:
99  ind += 1
100  if p1:
101  pos = pos_x + ind, pos_y
102  p1 = check_position1(pos, size)
103  flag, color = get_color(pos, pix_data, colors)
104  if flag:
105  break
106  if p2:
107  pos = pos_x - ind, pos_y
108  p2 = check_position2(pos)
109  flag, color = get_color(pos, pix_data, colors)
110  if flag:
111  break
112  if p3:
113  pos = pos_x, pos_y + ind
114  p3 = check_position3(pos, size)
115  flag, color = get_color(pos, pix_data, colors)
116  if flag:
117  break
118  if p4:
119  pos = pos_x, pos_y - ind
120  p4 = check_position4(pos)
121  flag, color = get_color(pos, pix_data, colors)
122  if flag:
123  break
124  if p5:
125  pos = pos_x + ind, pos_y + ind
126  p5 = check_position5(pos, size)
127  flag, color = get_color(pos, pix_data, colors)
128  if flag:
129  break
130  if p6:
131  pos = pos_x - ind, pos_y + ind
132  p6 = check_position6(pos, size)
133  flag, color = get_color(pos, pix_data, colors)
134  if flag:
135  break
136  if p7:
137  pos = pos_x + ind, pos_y - ind
138  p7 = check_position7(pos, size)
139  flag, color = get_color(pos, pix_data, colors)
140  if flag:
141  break
142  if p8:
143  pos = pos_x - ind, pos_y - ind
144  p8 = check_position8(pos)
145  flag, color = get_color(pos, pix_data, colors)
146  if flag:
147  break
148  if not p1 and not p2 and not p3 and not p4 and not p5 and not p6 and not p7 and not p8:
149  break
150 
151  if color is None:
152  black = 0, 0, 0, 255
153  return black
154  return color
155 
156 
157 def oversegmentation(segmentation_map_path, th_post, filepath='.'):
158  initial_map = Image.open(segmentation_map_path)
159  final_map = initial_map.copy()
160  pix_data_initial = initial_map.load()
161  pix_data_final = final_map.load()
162 
163  dictionary, colors = get_colors(pix_data_initial, initial_map)
164 
165  new_colors = colors[:]
166  if (255, 255, 255, 255) in new_colors:
167  new_colors.remove((255, 255, 255, 255))
168  del dictionary[(255, 255, 255, 255)]
169 
170  remove_small_color(dictionary, new_colors, th_post)
171 
172  colors_eliminated = [(255, 255, 255, 255)]
173  for color in colors:
174  if color not in new_colors:
175  colors_eliminated.append(color)
176 
177  for y in range(initial_map.size[1]):
178  for x in range(initial_map.size[0]):
179  position = [x, y]
180  pixel_color = pix_data_initial[x, y]
181  if pixel_color in colors_eliminated:
182  new_pixel_color = compute_distance(pix_data_initial, position, initial_map.size, new_colors)
183  pix_data_final[x, y] = new_pixel_color
184 
185 
186 
187  title = filepath + '8b_rooms_th1_on_map_post.png'
188  #title_pdf = filepath + '8b_rooms_th1_on_map_post.pdf'
189  rospy.loginfo('[rose2] 8b_rooms_th1_on_map_post')
190  final_map.save(title)
191  #pdf_image = final_map.convert('RGB')
192  #pdf_image.save(title_pdf)
193  return title, new_colors
194 
195 
196 def clear_rooms(room_image, param_obj, rooms):
197  initial_map = Image.open(room_image)
198  final_map = initial_map.copy()
199  pix_data_initial = initial_map.load()
200  pix_data_final = final_map.load()
201 
202  dictionary, colors = get_colors(pix_data_initial, initial_map)
203 
204  new_colors = colors[:]
205 
206  remove_small_color(dictionary, new_colors, param_obj.th_post)
207 
208  colors_eliminated = []
209  for color in colors:
210  if color not in new_colors:
211  colors_eliminated.append(color)
212 
213  for y in range(initial_map.size[1]):
214  for x in range(initial_map.size[0]):
215  position = [x, y]
216  pixel_color = pix_data_initial[x, y]
217  if pixel_color in colors_eliminated:
218  new_pixel_color = compute_distance(pix_data_initial, position, initial_map.size, new_colors)
219  pix_data_final[x, y] = new_pixel_color
220 
221 
222 
223  final_map.save(room_image)
postprocessing.check_position8
def check_position8(pos)
Definition: postprocessing.py:78
postprocessing.check_position2
def check_position2(pos)
Definition: postprocessing.py:42
postprocessing.clear_rooms
def clear_rooms(room_image, param_obj, rooms)
Definition: postprocessing.py:196
postprocessing.get_colors
def get_colors(pix_data, map_image)
Definition: postprocessing.py:4
postprocessing.get_color
def get_color(pos, pix_data, colors)
Definition: postprocessing.py:29
postprocessing.check_position7
def check_position7(pos, size)
Definition: postprocessing.py:72
postprocessing.remove_small_color
def remove_small_color(dictionary, colors, th)
Definition: postprocessing.py:22
postprocessing.check_position5
def check_position5(pos, size)
Definition: postprocessing.py:60
postprocessing.check_position6
def check_position6(pos, size)
Definition: postprocessing.py:66
postprocessing.check_position3
def check_position3(pos, size)
Definition: postprocessing.py:48
postprocessing.check_position1
def check_position1(pos, size)
Definition: postprocessing.py:36
postprocessing.compute_distance
def compute_distance(pix_data, position, size, colors)
Definition: postprocessing.py:84
postprocessing.oversegmentation
def oversegmentation(segmentation_map_path, th_post, filepath='.')
Definition: postprocessing.py:157
postprocessing.check_position4
def check_position4(pos)
Definition: postprocessing.py:54


rose2
Author(s): Gabriele Somaschini, Matteo Luperto
autogenerated on Wed Jun 28 2023 02:21:53