Spaces:
Running
Running
Sagar Bharadwaj
commited on
Commit
·
8ad61d2
1
Parent(s):
3e8f8f6
Added generate islands function
Browse files- colorbynumber/gen_islands.py +31 -81
colorbynumber/gen_islands.py
CHANGED
@@ -1,87 +1,37 @@
|
|
|
|
|
|
1 |
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
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 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
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 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
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
|
|
|
1 |
+
import cv2 as cv
|
2 |
+
import numpy as np
|
3 |
|
4 |
+
class GenerateIslands:
|
5 |
+
def __init__(self,
|
6 |
+
indices_color_choices,
|
7 |
+
gradient_kernel_size = 4,
|
8 |
+
):
|
9 |
+
self.indices_color_choices = indices_color_choices
|
10 |
+
self.gradient_kernel = np.ones((gradient_kernel_size, gradient_kernel_size), np.uint8)
|
11 |
+
self.color_index_island_list = []
|
12 |
|
13 |
+
def _get_islands_for_one_color(self, color_index):
|
14 |
+
# Get a binary image with just the selected color
|
15 |
+
this_color = (self.indices_color_choices == color_index).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
# Find connected components
|
18 |
+
num_labels, labels_im = cv.connectedComponents(this_color)
|
19 |
|
20 |
+
this_color_index_island_list = []
|
21 |
+
|
22 |
+
for component_id in range(1, num_labels):
|
23 |
+
this_component = (labels_im == component_id).astype(np.uint8)
|
24 |
+
|
25 |
+
# Border detection
|
26 |
+
gradient = cv.morphologyEx(this_component, cv.MORPH_GRADIENT, self.gradient_kernel)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
this_color_index_island_list.append((color_index, np.where(gradient == 1)))
|
29 |
+
|
30 |
+
return this_color_index_island_list
|
31 |
|
32 |
+
def get_islands(self):
|
33 |
+
for color_index in np.unique(self.indices_color_choices):
|
34 |
+
this_color_index_island_list = self._get_islands_for_one_color(color_index)
|
35 |
+
self.color_index_island_list.extend(this_color_index_island_list)
|
36 |
+
|
37 |
+
return self.color_index_island_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|