Sagar Bharadwaj commited on
Commit
e3a0f13
·
1 Parent(s): f490375

Create numbered islands

Browse files
Files changed (1) hide show
  1. colorbynumber/numbered_islands.py +116 -0
colorbynumber/numbered_islands.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+ def _get_centroid(coordinates):
5
+ """Get the centroid of a set of coordinates.
6
+
7
+ Args:
8
+ coordinates (list): A list of tuples.
9
+ Each tuple contains the row and column of a pixel.
10
+ Returns:
11
+ tuple: The row and column of the centroid.
12
+ """
13
+ row_sum = 0
14
+ col_sum = 0
15
+ for row, col in coordinates:
16
+ row_sum += row
17
+ col_sum += col
18
+ return (row_sum // len(coordinates), col_sum // len(coordinates))
19
+
20
+
21
+ def _add_text_to_image(image, text, position, font_size=1, font_color=(0, 0, 0)):
22
+ """Add text to an image.
23
+
24
+ Args:
25
+ image (np.array): Numpy image.
26
+ text (str): The text to add.
27
+ position (tuple): The position to add the text.
28
+ font_size (int): The size of the font.
29
+ font_color (tuple): The color of the font.
30
+ Returns:
31
+ np.array: A new image with the text added.
32
+ """
33
+ font = cv2.FONT_HERSHEY_SIMPLEX
34
+ font_scale = font_size
35
+ font_thickness = 2
36
+ position = (position[1], position[0])
37
+ return cv2.putText(
38
+ image,
39
+ text,
40
+ position,
41
+ font,
42
+ font_scale,
43
+ font_color,
44
+ font_thickness,
45
+ cv2.LINE_AA
46
+ )
47
+
48
+
49
+ def create_numbered_islands(islands, image_shape, border_color=[0, 0, 0]):
50
+ """Create a new image with the islands numbered.
51
+
52
+ Args:
53
+ image (np.array): Numpy image.
54
+ islands (list): A list of tuples.
55
+ Each tuple contains the color and the coordinates of the pixels in an island.
56
+ Returns:
57
+ np.array: A new image with the islands numbered.
58
+ """
59
+ # Create an all white image
60
+ numbered_islands = np.ones(image_shape, dtype=np.uint8) * 255
61
+
62
+ color_to_number = {}
63
+ for i, (color, _) in enumerate(islands):
64
+ color_to_number[color] = i + 1
65
+
66
+ for color, island_coordinates in islands:
67
+ for row, col in island_coordinates:
68
+ numbered_islands[row, col] = border_color
69
+ centroid = _get_centroid(island_coordinates)
70
+
71
+ # Add the number to the centroid of the island
72
+ numbered_islands = _add_text_to_image(
73
+ numbered_islands,
74
+ str(color_to_number[color]),
75
+ centroid
76
+ )
77
+
78
+ return numbered_islands
79
+
80
+ def _test_create_numbered_islands():
81
+ image_shape = (853, 1280, 3)
82
+
83
+ max_row, max_col = image_shape[0], image_shape[1]
84
+
85
+ diagonal_coordinates = [
86
+ (i,i) for i in range(max_row)
87
+ ]
88
+ left_border = [
89
+ (i, 0) for i in range(max_row)
90
+ ]
91
+ bottom_border_left = [
92
+ (max_row-1, i) for i in range(max_row)
93
+ ]
94
+ region_1 = diagonal_coordinates + left_border + bottom_border_left
95
+
96
+ bottom_border_right = [
97
+ (max_row - 1, i) for i in range(max_row, max_col)
98
+ ]
99
+ right_border = [
100
+ (i, max_col - 1) for i in range(max_row)
101
+ ]
102
+ top_border = [
103
+ (0, i) for i in range(max_col)
104
+ ]
105
+ region_2 = bottom_border_right + right_border + top_border
106
+
107
+
108
+ numbered_islands = create_numbered_islands(
109
+ islands = [
110
+ ('random_color_1', region_1),
111
+ ('random_color_2', region_2)
112
+ ],
113
+ image_shape = image_shape,
114
+ )
115
+
116
+ return numbered_islands