-
Notifications
You must be signed in to change notification settings - Fork 11
/
image.py
521 lines (470 loc) · 26 KB
/
image.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
from networkx.algorithms.components import connected_components
from ARCGraph import *
class Image:
abstractions = ["na", "nbccg", "ccgbr", "ccgbr2", "ccg", "mcccg", "lrg", "nbvcg"]
abstraction_ops = {"nbccg": "get_non_black_components_graph",
"ccgbr": "get_connected_components_graph_background_removed",
"ccgbr2": "get_connected_components_graph_background_removed_2",
"ccg": "get_connected_components_graph",
"mcccg": "get_multicolor_connected_components_graph",
"na": "get_no_abstraction_graph",
"nbvcg": "get_non_background_vertical_connected_components_graph",
"nbhcg": "get_non_background_horizontal_connected_components_graph",
"lrg": "get_largest_rectangle_graph"}
multicolor_abstractions = ["mcccg", "na"]
def __init__(self, task, grid=None, width=None, height=None, graph=None, name="image"):
"""
an image represents a 2D grid of pixels.
the coordinate system follows the convention of 0,0 being the top left pixel of the image
:param grid: a grid that represent the image
:param width: if a grid is not given, determines the width of the graph
:param height: if a grid is not given, determines the height of the graph
:param graph: if a networkx graph is given, use it directly as the graph
"""
self.task = task
self.name = name
self.colors_included = set()
self.background_color = 0
self.grid = grid
self.most_common_color = 0
self.least_common_color = 0
if not grid and not graph:
# create a graph with default color
self.width = width
self.height = height
self.image_size = (width, height)
self.graph = nx.grid_2d_graph(height, width)
nx.set_node_attributes(self.graph, 0, "color")
self.arc_graph = ARCGraph(self.graph, self.name, self)
self.colors_included.add(0)
elif graph:
self.width = max([node[1] for node in graph.nodes()]) + 1
self.height = max([node[0] for node in graph.nodes()]) + 1
self.image_size = (width, height)
self.graph = graph
self.arc_graph = ARCGraph(self.graph, self.name, self)
colors = []
for node, data in graph.nodes(data=True):
colors.append(data["color"])
if 0 not in colors:
self.background_color = max(set(colors), key=colors.count) # simple way to retrieve most common item
self.colors_included = set(colors)
if len(colors) != 0:
self.most_common_color = max(set(colors), key=colors.count)
self.least_common_color = min(set(colors), key=colors.count)
else:
# create a graph with the color in given grid
self.width = len(grid[0])
self.height = len(grid)
self.image_size = (self.width, self.height)
self.graph = nx.grid_2d_graph(self.height, self.width)
colors = []
for r, row in enumerate(grid):
for c, color in enumerate(row):
self.graph.nodes[r, c]["color"] = color
colors.append(color)
self.arc_graph = ARCGraph(self.graph, self.name, self)
if 0 not in colors:
self.background_color = max(set(colors), key=colors.count) # simple way to retrieve most common item
self.colors_included = set(colors)
if len(colors) != 0:
self.most_common_color = max(set(colors), key=colors.count)
self.least_common_color = min(set(colors), key=colors.count)
self.corners = {(0, 0), (0, self.width - 1), (self.height - 1, 0), (self.height - 1, self.width - 1)}
def copy(self):
"""
return a copy of the image
"""
return Image(self.task, grid=self.grid, name=self.name)
# --------------------------------------abstractions-----------------------------------
def get_connected_components_graph(self, graph=None):
"""
return an abstracted graph where a node is defined as:
a group of adjacent pixels of the same color in the original graph
"""
if not graph:
graph = self.graph
color_connected_components_graph = nx.Graph()
# for color in self.colors_included:
for color in range(10):
color_nodes = (node for node, data in graph.nodes(data=True) if data.get("color") == color)
color_subgraph = graph.subgraph(color_nodes)
color_connected_components = connected_components(color_subgraph)
for i, component in enumerate(color_connected_components):
color_connected_components_graph.add_node((color, i), nodes=list(component), color=color,
size=len(list(component)))
for node_1, node_2 in combinations(color_connected_components_graph.nodes, 2):
nodes_1 = color_connected_components_graph.nodes[node_1]["nodes"]
nodes_2 = color_connected_components_graph.nodes[node_2]["nodes"]
for n1 in nodes_1:
for n2 in nodes_2:
if n1[0] == n2[0]: # two nodes on the same row
for column_index in range(min(n1[1], n2[1]) + 1, max(n1[1], n2[1])):
if graph.nodes[n1[0], column_index]["color"] != self.background_color:
break
else:
color_connected_components_graph.add_edge(node_1, node_2, direction="horizontal")
break
elif n1[1] == n2[1]: # two nodes on the same column:
for row_index in range(min(n1[0], n2[0]) + 1, max(n1[0], n2[0])):
if graph.nodes[row_index, n1[1]]["color"] != self.background_color:
break
else:
color_connected_components_graph.add_edge(node_1, node_2, direction="vertical")
break
else:
continue
break
return ARCGraph(color_connected_components_graph, self.name, self, "ccg")
def get_connected_components_graph_background_removed(self, graph=None):
"""
return an abstracted graph where a node is defined as:
a group of adjacent pixels of the same color in the original graph.
remove nodes identified as background.
background is defined as a node that includes a corner and has the most common color
"""
if not graph:
graph = self.graph
ccgbr = nx.Graph()
for color in range(10):
color_nodes = (node for node, data in graph.nodes(data=True) if data.get("color") == color)
color_subgraph = graph.subgraph(color_nodes)
color_connected_components = connected_components(color_subgraph)
if color != self.background_color:
for i, component in enumerate(color_connected_components):
ccgbr.add_node((color, i), nodes=list(component), color=color, size=len(list(component)))
else:
for i, component in enumerate(color_connected_components):
if len(set(component) & self.corners) == 0: # background color + contains a corner
ccgbr.add_node((color, i), nodes=list(component), color=color, size=len(list(component)))
for node_1, node_2 in combinations(ccgbr.nodes, 2):
nodes_1 = ccgbr.nodes[node_1]["nodes"]
nodes_2 = ccgbr.nodes[node_2]["nodes"]
for n1 in nodes_1:
for n2 in nodes_2:
if n1[0] == n2[0]: # two nodes on the same row
for column_index in range(min(n1[1], n2[1]) + 1, max(n1[1], n2[1])):
if graph.nodes[n1[0], column_index]["color"] != self.background_color:
break
else:
ccgbr.add_edge(node_1, node_2, direction="horizontal")
break
elif n1[1] == n2[1]: # two nodes on the same column:
for row_index in range(min(n1[0], n2[0]) + 1, max(n1[0], n2[0])):
if graph.nodes[row_index, n1[1]]["color"] != self.background_color:
break
else:
ccgbr.add_edge(node_1, node_2, direction="vertical")
break
else:
continue
break
return ARCGraph(ccgbr, self.name, self, "ccgbr")
def get_connected_components_graph_background_removed_2(self, graph=None):
"""
return an abstracted graph where a node is defined as:
a group of adjacent pixels of the same color in the original graph.
remove nodes identified as background.
background is defined as a node that includes a corner or an edge node and has the most common color
"""
if not graph:
graph = self.graph
ccgbr2 = nx.Graph()
for color in range(10):
color_nodes = (node for node, data in graph.nodes(data=True) if data.get("color") == color)
color_subgraph = graph.subgraph(color_nodes)
color_connected_components = connected_components(color_subgraph)
for i, component in enumerate(color_connected_components):
if color != self.background_color:
ccgbr2.add_node((color, i), nodes=list(component), color=color, size=len(list(component)))
else:
component = list(component)
for node in component:
# if the node touches any edge of image it is not included
if node[0] == 0 or node[0] == self.height - 1 or node[1] == 0 or node[1] == self.width - 1:
break
else:
ccgbr2.add_node((color, i), nodes=component, color=color, size=len(component))
for node_1, node_2 in combinations(ccgbr2.nodes, 2):
nodes_1 = ccgbr2.nodes[node_1]["nodes"]
nodes_2 = ccgbr2.nodes[node_2]["nodes"]
for n1 in nodes_1:
for n2 in nodes_2:
if n1[0] == n2[0]: # two nodes on the same row
for column_index in range(min(n1[1], n2[1]) + 1, max(n1[1], n2[1])):
if graph.nodes[n1[0], column_index]["color"] != self.background_color:
break
else:
ccgbr2.add_edge(node_1, node_2, direction="horizontal")
break
elif n1[1] == n2[1]: # two nodes on the same column:
for row_index in range(min(n1[0], n2[0]) + 1, max(n1[0], n2[0])):
if graph.nodes[row_index, n1[1]]["color"] != self.background_color:
break
else:
ccgbr2.add_edge(node_1, node_2, direction="vertical")
break
else:
continue
break
return ARCGraph(ccgbr2, self.name, self, "ccgbr2")
def get_non_background_vertical_connected_components_graph(self, graph=None):
"""
return an abstracted graph where a node is defined as:
a group of vertically adjacent pixels of the same color in the original graph, excluding background color.
"""
if not graph:
graph = self.graph
non_background_vertical_connected_components_graph = nx.Graph()
for color in range(10):
color_connected_components = []
if color == self.background_color:
continue
for column in range(self.width):
color_nodes = (node for node, data in graph.nodes(data=True) if
node[1] == column and data.get("color") == color)
color_subgraph = graph.subgraph(color_nodes)
color_connected_components.extend(list(connected_components(color_subgraph)))
for i, component in enumerate(color_connected_components):
non_background_vertical_connected_components_graph.add_node((color, i), nodes=list(component),
color=color, size=len(list(component)))
for node_1, node_2 in combinations(non_background_vertical_connected_components_graph.nodes, 2):
nodes_1 = non_background_vertical_connected_components_graph.nodes[node_1]["nodes"]
nodes_2 = non_background_vertical_connected_components_graph.nodes[node_2]["nodes"]
for n1 in nodes_1:
for n2 in nodes_2:
if n1[0] == n2[0]: # two nodes on the same row
for column_index in range(min(n1[1], n2[1]) + 1, max(n1[1], n2[1])):
if graph.nodes[n1[0], column_index]["color"] != self.background_color:
break
else:
non_background_vertical_connected_components_graph.add_edge(node_1, node_2,
direction="horizontal")
break
elif n1[1] == n2[1]: # two nodes on the same column:
for row_index in range(min(n1[0], n2[0]) + 1, max(n1[0], n2[0])):
if graph.nodes[row_index, n1[1]]["color"] != self.background_color:
break
else:
non_background_vertical_connected_components_graph.add_edge(node_1, node_2,
direction="vertical")
break
else:
continue
break
return ARCGraph(non_background_vertical_connected_components_graph, self.name, self, "nbvcg")
def get_non_background_horizontal_connected_components_graph(self, graph=None):
"""
return an abstracted graph where a node is defined as:
a group of horizontally adjacent pixels of the same color in the original graph, excluding background color.
"""
if not graph:
graph = self.graph
non_background_horizontal_connected_components_graph = nx.Graph()
# for color in self.colors_included:
for color in range(10):
color_connected_components = []
if color == self.background_color:
continue
for row in range(self.height):
color_nodes = (node for node, data in graph.nodes(data=True) if
node[0] == row and data.get("color") == color)
color_subgraph = graph.subgraph(color_nodes)
color_connected_components.extend(list(connected_components(color_subgraph)))
for i, component in enumerate(color_connected_components):
non_background_horizontal_connected_components_graph.add_node((color, i), nodes=list(component),
color=color, size=len(list(component)))
for node_1, node_2 in combinations(non_background_horizontal_connected_components_graph.nodes, 2):
nodes_1 = non_background_horizontal_connected_components_graph.nodes[node_1]["nodes"]
nodes_2 = non_background_horizontal_connected_components_graph.nodes[node_2]["nodes"]
for n1 in nodes_1:
for n2 in nodes_2:
if n1[0] == n2[0]: # two nodes on the same row
for column_index in range(min(n1[1], n2[1]) + 1, max(n1[1], n2[1])):
if graph.nodes[n1[0], column_index]["color"] != self.background_color:
break
else:
non_background_horizontal_connected_components_graph.add_edge(node_1, node_2,
direction="horizontal")
break
elif n1[1] == n2[1]: # two nodes on the same column:
for row_index in range(min(n1[0], n2[0]) + 1, max(n1[0], n2[0])):
if graph.nodes[row_index, n1[1]]["color"] != self.background_color:
break
else:
non_background_horizontal_connected_components_graph.add_edge(node_1, node_2,
direction="vertical")
break
else:
continue
break
return ARCGraph(non_background_horizontal_connected_components_graph, self.name, self, "nbhcg")
def get_non_black_components_graph(self, graph=None):
"""
return an abstracted graph where a node is defined as:
a group of adjacent pixels of the same color in the original graph, excluding background color.
"""
if not graph:
graph = self.graph
non_black_components_graph = nx.Graph()
# for color in self.colors_included:
for color in range(10):
if color == 0:
continue
color_nodes = (node for node, data in graph.nodes(data=True) if data.get("color") == color)
color_subgraph = graph.subgraph(color_nodes)
color_connected_components = connected_components(color_subgraph)
for i, component in enumerate(color_connected_components):
non_black_components_graph.add_node((color, i), nodes=list(component), color=color,
size=len(list(component)))
for node_1, node_2 in combinations(non_black_components_graph.nodes, 2):
nodes_1 = non_black_components_graph.nodes[node_1]["nodes"]
nodes_2 = non_black_components_graph.nodes[node_2]["nodes"]
for n1 in nodes_1:
for n2 in nodes_2:
if n1[0] == n2[0]: # two nodes on the same row
for column_index in range(min(n1[1], n2[1]) + 1, max(n1[1], n2[1])):
if graph.nodes[n1[0], column_index]["color"] != 0:
break
else:
non_black_components_graph.add_edge(node_1, node_2, direction="horizontal")
break
elif n1[1] == n2[1]: # two nodes on the same column:
for row_index in range(min(n1[0], n2[0]) + 1, max(n1[0], n2[0])):
if graph.nodes[row_index, n1[1]]["color"] != 0:
break
else:
non_black_components_graph.add_edge(node_1, node_2, direction="vertical")
break
else:
continue
break
return ARCGraph(non_black_components_graph, self.name, self, "nbccg")
def get_largest_rectangle_graph(self, graph=None):
"""
return an abstracted graph where a node is defined as:
a group of adjacent pixels of the same color in the original graph that makes up a rectangle, excluding black.
rectangles are identified from largest to smallest.
"""
if not graph:
graph = self.graph
# https://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529?pgno=1
def area(llx, lly, urx, ury):
if llx > urx or lly > ury or [llx, lly, urx, ury] == [0, 0, 0, 0]:
return 0
else:
return (urx - llx + 1) * (ury - lly + 1)
def all_nb(llx, lly, urx, ury, g):
for x in range(llx, urx + 1):
for y in range(lly, ury + 1):
if (y, x) not in g:
return False
return True
lrg = nx.Graph()
for color in range(10):
if color == 0:
continue
color_nodes = (node for node, data in graph.nodes(data=True) if data.get("color") == color)
color_subgraph = graph.subgraph(color_nodes)
subgraph_nodes = set(color_subgraph.nodes())
i = 0
while len(subgraph_nodes) != 0:
best = [0, 0, 0, 0]
for llx in range(self.width):
for lly in range(self.height):
for urx in range(self.width):
for ury in range(self.height):
cords = [llx, lly, urx, ury]
if area(*cords) > area(*best) and all_nb(*cords, subgraph_nodes):
best = cords
component = []
for x in range(best[0], best[2] + 1):
for y in range(best[1], best[3] + 1):
component.append((y, x))
subgraph_nodes.remove((y, x))
lrg.add_node((color, i), nodes=component, color=color, size=len(component))
i += 1
for node_1, node_2 in combinations(lrg.nodes, 2):
nodes_1 = lrg.nodes[node_1]["nodes"]
nodes_2 = lrg.nodes[node_2]["nodes"]
for n1 in nodes_1:
for n2 in nodes_2:
if n1[0] == n2[0]: # two nodes on the same row
for column_index in range(min(n1[1], n2[1]) + 1, max(n1[1], n2[1])):
if graph.nodes[n1[0], column_index]["color"] != 0:
break
else:
lrg.add_edge(node_1, node_2, direction="horizontal")
break
elif n1[1] == n2[1]: # two nodes on the same column:
for row_index in range(min(n1[0], n2[0]) + 1, max(n1[0], n2[0])):
if graph.nodes[row_index, n1[1]]["color"] != 0:
break
else:
lrg.add_edge(node_1, node_2, direction="vertical")
break
else:
continue
break
return ARCGraph(lrg, self.name, self, "lrg")
def get_multicolor_connected_components_graph(self, graph=None):
"""
return an abstracted graph where a node is defined as:
a group of adjacent pixels of any non-background color in the original graph.
"""
if not graph:
graph = self.graph
multicolor_connected_components_graph = nx.Graph()
non_background_nodes = [node for node, data in graph.nodes(data=True) if data["color"] != self.background_color]
color_subgraph = graph.subgraph(non_background_nodes)
multicolor_connected_components = connected_components(color_subgraph)
for i, component in enumerate(multicolor_connected_components):
sub_nodes = []
sub_nodes_color = []
for node in component:
sub_nodes.append(node)
sub_nodes_color.append(graph.nodes[node]["color"])
multicolor_connected_components_graph.add_node((len(sub_nodes), i), nodes=sub_nodes, color=sub_nodes_color,
size=len(sub_nodes))
# add edges between the abstracted nodes
for node_1, node_2 in combinations(multicolor_connected_components_graph.nodes, 2):
nodes_1 = multicolor_connected_components_graph.nodes[node_1]["nodes"]
nodes_2 = multicolor_connected_components_graph.nodes[node_2]["nodes"]
for n1 in nodes_1:
for n2 in nodes_2:
if n1[0] == n2[0]: # two nodes on the same row
for column_index in range(min(n1[1], n2[1]) + 1, max(n1[1], n2[1])):
if graph.nodes[n1[0], column_index]["color"] != self.background_color:
break
else:
multicolor_connected_components_graph.add_edge(node_1, node_2, direction="horizontal")
break
elif n1[1] == n2[1]: # two nodes on the same column:
for row_index in range(min(n1[0], n2[0]) + 1, max(n1[0], n2[0])):
if graph.nodes[row_index, n1[1]]["color"] != self.background_color:
break
else:
multicolor_connected_components_graph.add_edge(node_1, node_2, direction="vertical")
break
else:
continue
break
return ARCGraph(multicolor_connected_components_graph, self.name, self, "mcccg")
def get_no_abstraction_graph(self, graph=None):
"""
return an abstracted graph where a node is defined as:
the entire graph as one multi-color node.
"""
if not graph:
graph = self.graph
no_abs_graph = nx.Graph()
sub_nodes = []
sub_nodes_color = []
for node, data in graph.nodes(data=True):
sub_nodes.append(node)
sub_nodes_color.append(graph.nodes[node]["color"])
no_abs_graph.add_node((0, 0), nodes=sub_nodes, color=sub_nodes_color, size=len(sub_nodes))
return ARCGraph(no_abs_graph, self.name, self, "na")
# undo abstraction
def undo_abstraction(self, arc_graph):
return arc_graph.undo_abstraction()