Spaces:
Running
Running
Sagar Bharadwaj
commited on
Commit
·
0a92df1
1
Parent(s):
c6d0a62
Added open morphological transformation to remove small holes and isthmuses
Browse files- colorbynumber/config.py +5 -0
- colorbynumber/gen_islands.py +9 -2
colorbynumber/config.py
CHANGED
@@ -8,6 +8,11 @@ default_config = {
|
|
8 |
# Padding around the borders of the image.
|
9 |
"border_padding": 2,
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
# Color islands with area less than this threshold will be ignored.
|
12 |
# The value is a percentage of the total area of the image.
|
13 |
"area_perc_threshold": 0.05,
|
|
|
8 |
# Padding around the borders of the image.
|
9 |
"border_padding": 2,
|
10 |
|
11 |
+
# Determines the size of the kernel used for open morphological operation.
|
12 |
+
# This removes small islands and isthmuses.
|
13 |
+
"open_kernel_size": 3,
|
14 |
+
|
15 |
+
|
16 |
# Color islands with area less than this threshold will be ignored.
|
17 |
# The value is a percentage of the total area of the image.
|
18 |
"area_perc_threshold": 0.05,
|
colorbynumber/gen_islands.py
CHANGED
@@ -128,12 +128,17 @@ class GenerateIslands:
|
|
128 |
|
129 |
|
130 |
def _get_islands_for_one_color(self, color_index, border_padding, area_perc_threshold,
|
131 |
-
arc_length_area_ratio_threshold, check_shape_validity
|
|
|
132 |
# Get a binary image with just the selected color
|
133 |
this_color = (self.indices_color_choices == color_index).astype(np.uint8)
|
134 |
# Pad the image to enable border detection on image boundaries
|
135 |
this_color = np.pad(this_color, border_padding, mode='constant', constant_values=0)
|
136 |
|
|
|
|
|
|
|
|
|
137 |
# Find connected components
|
138 |
num_labels, labels_im = cv.connectedComponents(this_color)
|
139 |
|
@@ -166,6 +171,7 @@ class GenerateIslands:
|
|
166 |
area_perc_threshold = config["area_perc_threshold"]
|
167 |
arc_length_area_ratio_threshold = config["arc_length_area_ratio_threshold"]
|
168 |
check_shape_validity = config["check_shape_validity"]
|
|
|
169 |
|
170 |
for color_index in np.unique(self.indices_color_choices):
|
171 |
self._get_islands_for_one_color(
|
@@ -173,7 +179,8 @@ class GenerateIslands:
|
|
173 |
border_padding = border_padding,
|
174 |
area_perc_threshold = area_perc_threshold,
|
175 |
arc_length_area_ratio_threshold = arc_length_area_ratio_threshold,
|
176 |
-
check_shape_validity = check_shape_validity
|
|
|
177 |
)
|
178 |
|
179 |
# Flatten the list of borders
|
|
|
128 |
|
129 |
|
130 |
def _get_islands_for_one_color(self, color_index, border_padding, area_perc_threshold,
|
131 |
+
arc_length_area_ratio_threshold, check_shape_validity,
|
132 |
+
open_kernel_size):
|
133 |
# Get a binary image with just the selected color
|
134 |
this_color = (self.indices_color_choices == color_index).astype(np.uint8)
|
135 |
# Pad the image to enable border detection on image boundaries
|
136 |
this_color = np.pad(this_color, border_padding, mode='constant', constant_values=0)
|
137 |
|
138 |
+
# Run the open morphological operation to remove small islands and isthmuses
|
139 |
+
kernel = np.ones((open_kernel_size, open_kernel_size),np.uint8)
|
140 |
+
this_color = cv.morphologyEx(this_color, cv.MORPH_OPEN, kernel)
|
141 |
+
|
142 |
# Find connected components
|
143 |
num_labels, labels_im = cv.connectedComponents(this_color)
|
144 |
|
|
|
171 |
area_perc_threshold = config["area_perc_threshold"]
|
172 |
arc_length_area_ratio_threshold = config["arc_length_area_ratio_threshold"]
|
173 |
check_shape_validity = config["check_shape_validity"]
|
174 |
+
open_kernel_size = config["open_kernel_size"]
|
175 |
|
176 |
for color_index in np.unique(self.indices_color_choices):
|
177 |
self._get_islands_for_one_color(
|
|
|
179 |
border_padding = border_padding,
|
180 |
area_perc_threshold = area_perc_threshold,
|
181 |
arc_length_area_ratio_threshold = arc_length_area_ratio_threshold,
|
182 |
+
check_shape_validity = check_shape_validity,
|
183 |
+
open_kernel_size = open_kernel_size,
|
184 |
)
|
185 |
|
186 |
# Flatten the list of borders
|