Spaces:
Running
Running
Sagar Bharadwaj
commited on
Commit
·
d2ae102
1
Parent(s):
342535f
Updated docstrings
Browse files- colorbynumber/config.py +15 -0
- colorbynumber/gen_islands.py +6 -1
- colorbynumber/main.py +15 -6
- colorbynumber/numbered_islands.py +7 -42
colorbynumber/config.py
CHANGED
@@ -1,11 +1,26 @@
|
|
1 |
default_config = {
|
|
|
2 |
"denoise": True,
|
|
|
|
|
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,
|
10 |
"font_color": (0, 0, 0),
|
11 |
"font_thickness": 2,
|
|
|
1 |
default_config = {
|
2 |
+
# If True, the image will be denoised after simplification.
|
3 |
"denoise": True,
|
4 |
+
|
5 |
+
# Higher values will result in more aggressive denoising.
|
6 |
"denoise_h": 100,
|
7 |
+
|
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,
|
14 |
+
|
15 |
+
# If True, all shapes with perimeter to area ratio of less than
|
16 |
+
# arc_length_area_ratio_threshold will be ignored.
|
17 |
"check_shape_validity": True,
|
18 |
"arc_length_area_ratio_threshold": 1,
|
19 |
+
|
20 |
+
# Color of the border around around color islands.
|
21 |
"border_color": (0, 0, 0),
|
22 |
+
|
23 |
+
# Font for the numbers shown in color islands.
|
24 |
"font_size": 0.5,
|
25 |
"font_color": (0, 0, 0),
|
26 |
"font_thickness": 2,
|
colorbynumber/gen_islands.py
CHANGED
@@ -6,7 +6,12 @@ from .config import default_config
|
|
6 |
|
7 |
|
8 |
class GenerateIslands:
|
9 |
-
def __init__(self, indices_color_choices
|
|
|
|
|
|
|
|
|
|
|
10 |
self.indices_color_choices = indices_color_choices
|
11 |
|
12 |
# List of coordinates for each islands border
|
|
|
6 |
|
7 |
|
8 |
class GenerateIslands:
|
9 |
+
def __init__(self, indices_color_choices):
|
10 |
+
"""
|
11 |
+
Args:
|
12 |
+
indices_color_choices: 2D numpy array with the same shape as the image.
|
13 |
+
Shows the color index chosen for each pixel in the image.
|
14 |
+
"""
|
15 |
self.indices_color_choices = indices_color_choices
|
16 |
|
17 |
# List of coordinates for each islands border
|
colorbynumber/main.py
CHANGED
@@ -8,6 +8,12 @@ from .numbered_islands import create_numbered_islands
|
|
8 |
|
9 |
class ColorByNumber:
|
10 |
def __init__(self, image_path, color_list, config = default_config):
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
self.image_path = image_path
|
12 |
self.color_list = color_list
|
13 |
self.config = config
|
@@ -53,12 +59,15 @@ class ColorByNumber:
|
|
53 |
Generates a grid of colored squares with labels below them.
|
54 |
|
55 |
Args:
|
56 |
-
|
57 |
-
rows: Number of rows in the grid
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
62 |
"""
|
63 |
|
64 |
# Calculate grid dimensions if not provided
|
|
|
8 |
|
9 |
class ColorByNumber:
|
10 |
def __init__(self, image_path, color_list, config = default_config):
|
11 |
+
"""
|
12 |
+
Args:
|
13 |
+
image_path: Path to the image file.
|
14 |
+
color_list: List of colors in (R, G, B) format.
|
15 |
+
config: Dictionary of configuration parameters (optional).
|
16 |
+
"""
|
17 |
self.image_path = image_path
|
18 |
self.color_list = color_list
|
19 |
self.config = config
|
|
|
59 |
Generates a grid of colored squares with labels below them.
|
60 |
|
61 |
Args:
|
62 |
+
cols: Number of columns in the grid.
|
63 |
+
rows: Number of rows in the grid.
|
64 |
+
square_size: Size of each square in the grid.
|
65 |
+
margin: Margin around the grid.
|
66 |
+
gap_horizontal: Horizontal gap between squares.
|
67 |
+
gap_vertical: Vertical gap between squares.
|
68 |
+
font: Font for the labels.
|
69 |
+
font_size: Font size for the labels.
|
70 |
+
border_color: Color of the border around each square.
|
71 |
"""
|
72 |
|
73 |
# Calculate grid dimensions if not provided
|
colorbynumber/numbered_islands.py
CHANGED
@@ -19,6 +19,7 @@ def _add_text_to_image(image, text, position, font_size, font_color, font_thickn
|
|
19 |
position (tuple): The position to add the text.
|
20 |
font_size (int): The size of the font.
|
21 |
font_color (tuple): The color of the font.
|
|
|
22 |
Returns:
|
23 |
np.array: A new image with the text added.
|
24 |
"""
|
@@ -44,11 +45,13 @@ def create_numbered_islands(islands, image_shape,
|
|
44 |
"""Create a new image with the islands numbered.
|
45 |
|
46 |
Args:
|
47 |
-
image (np.array): Numpy image.
|
48 |
islands (list): A list of tuples.
|
49 |
-
Each tuple contains the
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
52 |
"""
|
53 |
|
54 |
padding = config["border_padding"]
|
@@ -87,41 +90,3 @@ def create_numbered_islands(islands, image_shape,
|
|
87 |
return numbered_islands
|
88 |
|
89 |
return numbered_islands
|
90 |
-
|
91 |
-
def _test_create_numbered_islands():
|
92 |
-
image_shape = (853, 1280, 3)
|
93 |
-
|
94 |
-
max_row, max_col = image_shape[0], image_shape[1]
|
95 |
-
|
96 |
-
diagonal_coordinates = [
|
97 |
-
(i,i) for i in range(max_row)
|
98 |
-
]
|
99 |
-
left_border = [
|
100 |
-
(i, 0) for i in range(max_row)
|
101 |
-
]
|
102 |
-
bottom_border_left = [
|
103 |
-
(max_row-1, i) for i in range(max_row)
|
104 |
-
]
|
105 |
-
region_1 = diagonal_coordinates + left_border + bottom_border_left
|
106 |
-
|
107 |
-
bottom_border_right = [
|
108 |
-
(max_row - 1, i) for i in range(max_row, max_col)
|
109 |
-
]
|
110 |
-
right_border = [
|
111 |
-
(i, max_col - 1) for i in range(max_row)
|
112 |
-
]
|
113 |
-
top_border = [
|
114 |
-
(0, i) for i in range(max_col)
|
115 |
-
]
|
116 |
-
region_2 = bottom_border_right + right_border + top_border
|
117 |
-
|
118 |
-
|
119 |
-
numbered_islands = create_numbered_islands(
|
120 |
-
islands = [
|
121 |
-
('random_color_1', region_1),
|
122 |
-
('random_color_2', region_2)
|
123 |
-
],
|
124 |
-
image_shape = image_shape,
|
125 |
-
)
|
126 |
-
|
127 |
-
return numbered_islands
|
|
|
19 |
position (tuple): The position to add the text.
|
20 |
font_size (int): The size of the font.
|
21 |
font_color (tuple): The color of the font.
|
22 |
+
font_thickness (int): The thickness of the font.
|
23 |
Returns:
|
24 |
np.array: A new image with the text added.
|
25 |
"""
|
|
|
45 |
"""Create a new image with the islands numbered.
|
46 |
|
47 |
Args:
|
|
|
48 |
islands (list): A list of tuples.
|
49 |
+
Each tuple contains the color id and the coordinates of the island border.
|
50 |
+
image_shape (tuple): The shape of the original image.
|
51 |
+
centroid_coords_list (list): A list of centroid coordinates for the islands.
|
52 |
+
config (dict): Configuration dictionary.
|
53 |
+
show_numbers (bool): If True, the numbers will be shown in the islands.
|
54 |
+
binary (bool): If True, the output will be a binary image.
|
55 |
"""
|
56 |
|
57 |
padding = config["border_padding"]
|
|
|
90 |
return numbered_islands
|
91 |
|
92 |
return numbered_islands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|