Sagar Bharadwaj commited on
Commit
8ad61d2
·
1 Parent(s): 3e8f8f6

Added generate islands function

Browse files
Files changed (1) hide show
  1. colorbynumber/gen_islands.py +31 -81
colorbynumber/gen_islands.py CHANGED
@@ -1,87 +1,37 @@
 
 
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
 
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