Spaces:
Running
Running
Sagar Bharadwaj
commited on
Commit
·
9faa18b
1
Parent(s):
9777205
Added contour analysis in gen islands
Browse files- colorbynumber/gen_islands.py +45 -13
colorbynumber/gen_islands.py
CHANGED
@@ -8,9 +8,37 @@ class GenerateIslands:
|
|
8 |
):
|
9 |
self.indices_color_choices = indices_color_choices
|
10 |
self.gradient_kernel = np.ones((gradient_kernel_size, gradient_kernel_size), np.uint8)
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Get a binary image with just the selected color
|
15 |
this_color = (self.indices_color_choices == color_index).astype(np.uint8)
|
16 |
# Pad the image to enable border detection on image boundaries
|
@@ -19,21 +47,25 @@ class GenerateIslands:
|
|
19 |
# Find connected components
|
20 |
num_labels, labels_im = cv.connectedComponents(this_color)
|
21 |
|
22 |
-
this_color_index_island_list = []
|
23 |
-
|
24 |
for component_id in range(1, num_labels):
|
25 |
this_component = (labels_im == component_id).astype(np.uint8)
|
|
|
26 |
|
27 |
-
#
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
this_color_index_island_list.append((color_index, np.where(gradient == 1)))
|
31 |
-
|
32 |
-
return this_color_index_island_list
|
33 |
|
34 |
-
def get_islands(self, border_padding=2):
|
35 |
for color_index in np.unique(self.indices_color_choices):
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
return
|
|
|
8 |
):
|
9 |
self.indices_color_choices = indices_color_choices
|
10 |
self.gradient_kernel = np.ones((gradient_kernel_size, gradient_kernel_size), np.uint8)
|
11 |
+
|
12 |
+
# List of coordinates for each islands border
|
13 |
+
self.island_borders = {}
|
14 |
+
for color_index in np.unique(indices_color_choices):
|
15 |
+
self.island_borders[color_index] = []
|
16 |
+
|
17 |
+
# Images of the islands
|
18 |
+
self.island_fills = {}
|
19 |
+
for color_index in np.unique(indices_color_choices):
|
20 |
+
self.island_fills[color_index] = []
|
21 |
|
22 |
+
def _get_cleaned_up_contours(self, island_fill, area_threshold_perc):
|
23 |
+
contours_image = np.ones_like(island_fill)
|
24 |
+
|
25 |
+
total_area = self.indices_color_choices.shape[0] * self.indices_color_choices.shape[1]
|
26 |
+
|
27 |
+
contours, hierarchy = cv.findContours(
|
28 |
+
island_fill,
|
29 |
+
mode = cv.RETR_TREE,
|
30 |
+
method = cv.CHAIN_APPROX_NONE
|
31 |
+
)
|
32 |
+
|
33 |
+
for cntr_id, contour in enumerate(contours):
|
34 |
+
area_fraction_perc = (cv.contourArea(contour) / total_area) * 100
|
35 |
+
if area_fraction_perc >= area_threshold_perc:
|
36 |
+
cv.drawContours(contours_image, contours, cntr_id, (0,255,0), 4)
|
37 |
+
|
38 |
+
return contours_image
|
39 |
+
|
40 |
+
|
41 |
+
def _get_islands_for_one_color(self, color_index, border_padding, area_threshold_perc):
|
42 |
# Get a binary image with just the selected color
|
43 |
this_color = (self.indices_color_choices == color_index).astype(np.uint8)
|
44 |
# Pad the image to enable border detection on image boundaries
|
|
|
47 |
# Find connected components
|
48 |
num_labels, labels_im = cv.connectedComponents(this_color)
|
49 |
|
|
|
|
|
50 |
for component_id in range(1, num_labels):
|
51 |
this_component = (labels_im == component_id).astype(np.uint8)
|
52 |
+
self.island_fills[color_index].append(this_component)
|
53 |
|
54 |
+
# Get cleaned up contours
|
55 |
+
cleaned_up_contours = self._get_cleaned_up_contours(this_component, area_threshold_perc)
|
56 |
+
|
57 |
+
contour_border_coords = np.where(cleaned_up_contours == 0)
|
58 |
+
if len(contour_border_coords[0]) > 0:
|
59 |
+
self.island_borders[color_index].append((color_index, contour_border_coords))
|
60 |
|
|
|
|
|
|
|
61 |
|
62 |
+
def get_islands(self, border_padding=2, area_threshold_perc=0.05):
|
63 |
for color_index in np.unique(self.indices_color_choices):
|
64 |
+
self._get_islands_for_one_color(color_index, border_padding, area_threshold_perc)
|
65 |
+
|
66 |
+
# Flatten the list of borders
|
67 |
+
island_borders_list = []
|
68 |
+
for color_id in self.island_borders:
|
69 |
+
island_borders_list += self.island_borders[color_id]
|
70 |
|
71 |
+
return island_borders_list
|