Spaces:
Running
Running
aditink
commited on
Commit
·
c858e30
1
Parent(s):
e3a0f13
Inefficient, not fully tested island generation
Browse files- colorbynumber/gen_islands.py +87 -0
colorbynumber/gen_islands.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
def _is_same_color(color1, color2):
|
3 |
+
"""Check if two colors are the same.
|
4 |
+
|
5 |
+
Args:
|
6 |
+
color1 (list): A tuple representing the RGB values of a color (R, G, B).
|
7 |
+
color2 (list): A tuple representing the RGB values of a color (R, G, B).
|
8 |
+
Returns:
|
9 |
+
bool: True if the colors are the same, False otherwise.
|
10 |
+
"""
|
11 |
+
all_same = True
|
12 |
+
for i in range(3):
|
13 |
+
all_same = all_same and color1[i] == color2[i]
|
14 |
+
return all_same
|
15 |
+
|
16 |
+
|
17 |
+
def _get_neighbors(pixel, image_shape):
|
18 |
+
"""Get the neighbors of a pixel.
|
19 |
+
|
20 |
+
Args:
|
21 |
+
pixel (tuple): A tuple representing the row and column of a pixel.
|
22 |
+
image_shape (tuple): A tuple representing the number of rows and columns in the image.
|
23 |
+
Returns:
|
24 |
+
set: A set of tuples representing the row and column of the neighbors.
|
25 |
+
"""
|
26 |
+
row, col = pixel
|
27 |
+
neighbors = set()
|
28 |
+
if row > 0:
|
29 |
+
neighbors.add((row - 1, col))
|
30 |
+
if row < image_shape[0] - 1:
|
31 |
+
neighbors.add((row + 1, col))
|
32 |
+
if col > 0:
|
33 |
+
neighbors.add((row, col - 1))
|
34 |
+
if col < image_shape[1] - 1:
|
35 |
+
neighbors.add((row, col + 1))
|
36 |
+
return neighbors
|
37 |
+
|
38 |
+
def get_islands(simplified_image, color_list):
|
39 |
+
"""Get the numbered islands from an image.
|
40 |
+
|
41 |
+
Args:
|
42 |
+
image (np.array): Numpy image.
|
43 |
+
color_list (list): A list of string re allowed colors.
|
44 |
+
Returns:
|
45 |
+
islands (list): A list of islands where each island is a tuple consisting of
|
46 |
+
1. the color name
|
47 |
+
2. a list of tuples representing the row and column of the pixels in the island.
|
48 |
+
"""
|
49 |
+
islands = []
|
50 |
+
visited_pixels = set()
|
51 |
+
current_color = simplified_image[0, 0]
|
52 |
+
stack_to_visit = set()
|
53 |
+
# Go to all the neighbors of the current pixel.
|
54 |
+
# If the neighbor has the same color, add it to the island and visit its neighbors.
|
55 |
+
# If the neighbor has a different color, add it to the stack to visit later.
|
56 |
+
stack_to_visit.add((0, 0))
|
57 |
+
while stack_to_visit:
|
58 |
+
pixel = stack_to_visit.pop()
|
59 |
+
print(pixel)
|
60 |
+
if pixel in visited_pixels:
|
61 |
+
continue
|
62 |
+
current_color = simplified_image[pixel]
|
63 |
+
visited_pixels.add(pixel)
|
64 |
+
island = [pixel]
|
65 |
+
neighbor_stack = _get_neighbors(pixel, simplified_image.shape)
|
66 |
+
while len(neighbor_stack)>0:
|
67 |
+
neighbor = neighbor_stack.pop()
|
68 |
+
(neighbor_row, neighbor_col) = neighbor
|
69 |
+
if neighbor in visited_pixels:
|
70 |
+
continue
|
71 |
+
if _is_same_color(simplified_image[neighbor_row][neighbor_col], current_color):
|
72 |
+
island.append(neighbor)
|
73 |
+
visited_pixels.add(neighbor)
|
74 |
+
neighbor_stack = neighbor_stack.union(_get_neighbors(neighbor, simplified_image.shape))
|
75 |
+
else:
|
76 |
+
stack_to_visit.add(neighbor)
|
77 |
+
# Remove any visited pixels from the stack
|
78 |
+
neighbor_stack = neighbor_stack - visited_pixels
|
79 |
+
print(len(neighbor_stack))
|
80 |
+
print("visited pixels")
|
81 |
+
print(len(visited_pixels))
|
82 |
+
islands.append((color_list[current_color], island))
|
83 |
+
# Debug: size of visited pixels
|
84 |
+
stack_to_visit = stack_to_visit - visited_pixels
|
85 |
+
print("visited islands")
|
86 |
+
print(len(islands))
|
87 |
+
return islands
|