Spaces:
Running
Running
Sagar Bharadwaj
commited on
Commit
·
c76c7ee
1
Parent(s):
804cbcd
Added shape validity check
Browse files- colorbynumber/config.py +1 -0
- colorbynumber/gen_islands.py +16 -10
colorbynumber/config.py
CHANGED
|
@@ -3,6 +3,7 @@ default_config = {
|
|
| 3 |
"denoise_h": 100,
|
| 4 |
"border_padding": 2,
|
| 5 |
"area_perc_threshold": 0.05,
|
|
|
|
| 6 |
"arc_length_area_ratio_threshold": 1,
|
| 7 |
"border_color": (0, 0, 0),
|
| 8 |
"font_size": 0.5,
|
|
|
|
| 3 |
"denoise_h": 100,
|
| 4 |
"border_padding": 2,
|
| 5 |
"area_perc_threshold": 0.05,
|
| 6 |
+
"check_shape_validity": True,
|
| 7 |
"arc_length_area_ratio_threshold": 1,
|
| 8 |
"border_color": (0, 0, 0),
|
| 9 |
"font_size": 0.5,
|
colorbynumber/gen_islands.py
CHANGED
|
@@ -56,7 +56,7 @@ class GenerateIslands:
|
|
| 56 |
|
| 57 |
|
| 58 |
def _get_cleaned_up_contours(self, island_fill, area_perc_threshold,
|
| 59 |
-
arc_length_area_ratio_threshold):
|
| 60 |
contours_image = np.ones_like(island_fill)
|
| 61 |
|
| 62 |
total_area = self.indices_color_choices.shape[0] * self.indices_color_choices.shape[1]
|
|
@@ -67,13 +67,16 @@ class GenerateIslands:
|
|
| 67 |
method = cv.CHAIN_APPROX_NONE
|
| 68 |
)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
contours_selected = []
|
| 79 |
hierarchy_selected = []
|
|
@@ -115,7 +118,7 @@ class GenerateIslands:
|
|
| 115 |
|
| 116 |
|
| 117 |
def _get_islands_for_one_color(self, color_index, border_padding, area_perc_threshold,
|
| 118 |
-
arc_length_area_ratio_threshold):
|
| 119 |
# Get a binary image with just the selected color
|
| 120 |
this_color = (self.indices_color_choices == color_index).astype(np.uint8)
|
| 121 |
# Pad the image to enable border detection on image boundaries
|
|
@@ -133,7 +136,8 @@ class GenerateIslands:
|
|
| 133 |
cleaned_up_contours, contours_selected, hierarchies_selected = self._get_cleaned_up_contours(
|
| 134 |
island_fill = this_component,
|
| 135 |
area_perc_threshold = area_perc_threshold,
|
| 136 |
-
arc_length_area_ratio_threshold = arc_length_area_ratio_threshold
|
|
|
|
| 137 |
)
|
| 138 |
|
| 139 |
# Get the centroid of the island
|
|
@@ -151,6 +155,7 @@ class GenerateIslands:
|
|
| 151 |
border_padding = config["border_padding"]
|
| 152 |
area_perc_threshold = config["area_perc_threshold"]
|
| 153 |
arc_length_area_ratio_threshold = config["arc_length_area_ratio_threshold"]
|
|
|
|
| 154 |
|
| 155 |
for color_index in np.unique(self.indices_color_choices):
|
| 156 |
self._get_islands_for_one_color(
|
|
@@ -158,6 +163,7 @@ class GenerateIslands:
|
|
| 158 |
border_padding = border_padding,
|
| 159 |
area_perc_threshold = area_perc_threshold,
|
| 160 |
arc_length_area_ratio_threshold = arc_length_area_ratio_threshold,
|
|
|
|
| 161 |
)
|
| 162 |
|
| 163 |
# Flatten the list of borders
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def _get_cleaned_up_contours(self, island_fill, area_perc_threshold,
|
| 59 |
+
arc_length_area_ratio_threshold, check_shape_validity):
|
| 60 |
contours_image = np.ones_like(island_fill)
|
| 61 |
|
| 62 |
total_area = self.indices_color_choices.shape[0] * self.indices_color_choices.shape[1]
|
|
|
|
| 67 |
method = cv.CHAIN_APPROX_NONE
|
| 68 |
)
|
| 69 |
|
| 70 |
+
if check_shape_validity:
|
| 71 |
+
is_valid_shape = self._is_valid_shape(
|
| 72 |
+
contours = contours,
|
| 73 |
+
hierarchy = hierarchy,
|
| 74 |
+
total_area = total_area,
|
| 75 |
+
area_perc_threshold = area_perc_threshold,
|
| 76 |
+
arc_length_area_ratio_threshold = arc_length_area_ratio_threshold
|
| 77 |
+
)
|
| 78 |
+
else:
|
| 79 |
+
is_valid_shape = True
|
| 80 |
|
| 81 |
contours_selected = []
|
| 82 |
hierarchy_selected = []
|
|
|
|
| 118 |
|
| 119 |
|
| 120 |
def _get_islands_for_one_color(self, color_index, border_padding, area_perc_threshold,
|
| 121 |
+
arc_length_area_ratio_threshold, check_shape_validity):
|
| 122 |
# Get a binary image with just the selected color
|
| 123 |
this_color = (self.indices_color_choices == color_index).astype(np.uint8)
|
| 124 |
# Pad the image to enable border detection on image boundaries
|
|
|
|
| 136 |
cleaned_up_contours, contours_selected, hierarchies_selected = self._get_cleaned_up_contours(
|
| 137 |
island_fill = this_component,
|
| 138 |
area_perc_threshold = area_perc_threshold,
|
| 139 |
+
arc_length_area_ratio_threshold = arc_length_area_ratio_threshold,
|
| 140 |
+
check_shape_validity = check_shape_validity
|
| 141 |
)
|
| 142 |
|
| 143 |
# Get the centroid of the island
|
|
|
|
| 155 |
border_padding = config["border_padding"]
|
| 156 |
area_perc_threshold = config["area_perc_threshold"]
|
| 157 |
arc_length_area_ratio_threshold = config["arc_length_area_ratio_threshold"]
|
| 158 |
+
check_shape_validity = config["check_shape_validity"]
|
| 159 |
|
| 160 |
for color_index in np.unique(self.indices_color_choices):
|
| 161 |
self._get_islands_for_one_color(
|
|
|
|
| 163 |
border_padding = border_padding,
|
| 164 |
area_perc_threshold = area_perc_threshold,
|
| 165 |
arc_length_area_ratio_threshold = arc_length_area_ratio_threshold,
|
| 166 |
+
check_shape_validity = check_shape_validity
|
| 167 |
)
|
| 168 |
|
| 169 |
# Flatten the list of borders
|