Spaces:
Running
Running
Sagar Bharadwaj
commited on
Commit
·
8cc93d2
1
Parent(s):
2969573
Added generate color legend
Browse files- colorbynumber/main.py +67 -0
colorbynumber/main.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import cv2 as cv
|
|
|
2 |
|
3 |
from .config import default_config
|
4 |
from .simplify_image import simplify_image
|
@@ -37,3 +38,69 @@ class ColorByNumber:
|
|
37 |
self.numbered_islands = numbered_islands
|
38 |
|
39 |
return self.numbered_islands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2 as cv
|
2 |
+
import numpy as np
|
3 |
|
4 |
from .config import default_config
|
5 |
from .simplify_image import simplify_image
|
|
|
38 |
self.numbered_islands = numbered_islands
|
39 |
|
40 |
return self.numbered_islands
|
41 |
+
|
42 |
+
def generate_color_legend(self,
|
43 |
+
cols=7,
|
44 |
+
rows=None,
|
45 |
+
square_size=100,
|
46 |
+
margin=10,
|
47 |
+
gap_horizontal=5, gap_vertical=30,
|
48 |
+
font=cv.FONT_HERSHEY_SIMPLEX,
|
49 |
+
font_size=1,
|
50 |
+
border_color=(0, 0, 0)
|
51 |
+
):
|
52 |
+
"""
|
53 |
+
Generates a grid of colored squares with labels below them.
|
54 |
+
|
55 |
+
Args:
|
56 |
+
colors: List of colors in (R, G, B) format.
|
57 |
+
rows: Number of rows in the grid (optional).
|
58 |
+
cols: Number of columns in the grid (optional).
|
59 |
+
|
60 |
+
Returns:
|
61 |
+
A NumPy array representing the color grid image.
|
62 |
+
"""
|
63 |
+
|
64 |
+
# Calculate grid dimensions if not provided
|
65 |
+
if rows is None and cols is None:
|
66 |
+
num_colors = len(self.color_list)
|
67 |
+
rows = cols = int(np.sqrt(num_colors)) + 1
|
68 |
+
|
69 |
+
elif rows is None:
|
70 |
+
cols = min(cols, len(self.color_list))
|
71 |
+
rows = int(np.ceil(len(self.color_list) / cols))
|
72 |
+
|
73 |
+
# Calculate total width and height based on margins, gaps, and squares
|
74 |
+
total_width = 2 * margin + (cols + 1) * square_size + (cols - 1) * gap_horizontal
|
75 |
+
total_height = 2 * margin + (rows + 1) * square_size + (rows - 1) * gap_vertical
|
76 |
+
|
77 |
+
# Create a white image
|
78 |
+
image = np.ones((total_height, total_width, 3), dtype=np.uint8) * 255
|
79 |
+
|
80 |
+
# Fill squares with colors
|
81 |
+
for i, color in enumerate(self.color_list):
|
82 |
+
row = i // cols
|
83 |
+
col = i % cols
|
84 |
+
|
85 |
+
start_col = margin + col * (square_size + gap_horizontal)
|
86 |
+
end_col = start_col + square_size
|
87 |
+
|
88 |
+
start_row = margin + row * (square_size + gap_vertical)
|
89 |
+
end_row = start_row + square_size
|
90 |
+
|
91 |
+
# Fill square with color
|
92 |
+
image[start_row:end_row, start_col:end_col] = color
|
93 |
+
|
94 |
+
# Draw border around that color
|
95 |
+
image[start_row, start_col:end_col] = border_color # Top Border
|
96 |
+
image[end_row, start_col:end_col] = border_color # Bottom Border
|
97 |
+
image[start_row:end_row, start_col] = border_color # Left Border
|
98 |
+
image[start_row:end_row, end_col] = border_color # Right Border
|
99 |
+
|
100 |
+
# Draw text label below the square
|
101 |
+
text_size, _ = cv.getTextSize(str(i), font, font_size, 1)
|
102 |
+
text_row = (end_row + text_size[1]) + 5
|
103 |
+
text_col = start_col + (square_size // 2) - (text_size[0] // 2)
|
104 |
+
cv.putText(image, str(i), (text_col, text_row), font, font_size, (0, 0, 0), 1)
|
105 |
+
|
106 |
+
return image
|