1
1
import json
2
2
from os import path
3
- import matplotlib .pyplot as plt
4
3
import cv2
5
4
import numpy as np
6
5
import rospkg
20
19
)
21
20
22
21
23
- reference_colours = {
24
- "blue_very_light" : np .array ([240 , 248 , 255 ]), # Alice blue
25
- "blue_light" : np .array ([173 , 216 , 230 ]), # Light blue
26
- "blue_sky" : np .array ([135 , 206 , 235 ]), # Sky blue
27
- "blue_powder" : np .array ([176 , 224 , 230 ]), # Powder blue
28
- "blue_celeste" : np .array ([178 , 255 , 255 ]), # Celeste, very pale blue shade
29
- "blue_periwinkle" : np .array (
30
- [204 , 204 , 255 ]
31
- ), # Periwinkle, a mix of light blue and lavender
32
- "blue_cadet" : np .array ([95 , 158 , 160 ]), # Cadet blue, a muted blue-green
33
- "blue" : np .array ([0 , 0 , 255 ]), # Standard blue
34
- "blue_royal" : np .array ([65 , 105 , 225 ]), # Royal blue
35
- "blue_deep" : np .array ([0 , 0 , 139 ]), # Deep blue
36
- "blue_dark" : np .array ([0 , 0 , 128 ]), # Dark blue
37
- # "blue_navy": np.array([0, 0, 80]), # Navy blue
38
- "yellow_very_light" : np .array ([255 , 255 , 204 ]), # Very light yellow
39
- "yellow_light" : np .array ([255 , 255 , 224 ]), # Light yellow
40
- "yellow" : np .array ([255 , 255 , 0 ]), # Standard yellow
41
- "yellow_gold" : np .array ([255 , 215 , 0 ]), # Gold yellow
42
- "yellow_dark" : np .array ([204 , 204 , 0 ]), # Dark yellow
43
- "yellow_mustard" : np .array ([255 , 219 , 88 ]), # Mustard yellow
44
- "red_very_light" : np .array ([255 , 204 , 204 ]), # Very light red
45
- "red_light" : np .array ([255 , 102 , 102 ]), # Light red
46
- "red" : np .array ([255 , 0 , 0 ]), # Standard red
47
- "red_dark" : np .array ([139 , 0 , 0 ]), # Dark red
48
- "red_maroon" : np .array ([128 , 0 , 0 ]), # Maroon
49
- "orange_very_light" : np .array ([255 , 229 , 180 ]), # Very light orange
50
- "orange_light" : np .array ([255 , 179 , 71 ]), # Light orange
51
- "orange" : np .array ([255 , 165 , 0 ]), # Standard orange
52
- "orange_dark" : np .array ([255 , 140 , 0 ]), # Dark orange
53
- "orange_burnt" : np .array ([204 , 85 , 0 ]), # Burnt orange
54
- "black" : np .array ([30 , 30 , 30 ]), # Black
55
- "white" : np .array ([255 , 255 , 255 ]), # White
56
- "gray" : np .array ([160 , 160 , 160 ]), # Gray
57
- }
58
-
59
- colour_group_map = {
60
- "blue_very_light" : "blue" ,
61
- "blue_light" : "blue" ,
62
- "blue_sky" : "blue" ,
63
- "blue_powder" : "blue" ,
64
- "blue_celeste" : "blue" ,
65
- "blue_periwinkle" : "blue" ,
66
- "blue_cadet" : "blue" ,
67
- "blue" : "blue" ,
68
- "blue_royal" : "blue" ,
69
- "blue_deep" : "blue" ,
70
- "blue_dark" : "blue" ,
71
- "yellow_very_light" : "yellow" ,
72
- "yellow_light" : "yellow" ,
73
- "yellow" : "yellow" ,
74
- "yellow_gold" : "yellow" ,
75
- "yellow_dark" : "yellow" ,
76
- "yellow_mustard" : "yellow" ,
77
- "red_very_light" : "red" ,
78
- "red_light" : "red" ,
79
- "red" : "red" ,
80
- "red_dark" : "red" ,
81
- "red_maroon" : "red" ,
82
- "orange_very_light" : "orange" ,
83
- "orange_light" : "orange" ,
84
- "orange" : "orange" ,
85
- "orange_dark" : "orange" ,
86
- "orange_burnt" : "orange" ,
87
- "black" : "black" ,
88
- "white" : "white" ,
89
- "gray" : "gray" ,
90
- }
91
-
92
- possible_colours = ["blue" , "yellow" , "red" , "orange" , "black" , "white" , "gray" ]
93
-
94
-
95
- def estimate_colour (rgb_array ):
96
- # Calculate distances to each reference colour
97
- # distances = {colour: cie76_distance(rgb_array, ref_rgb) for colour, ref_rgb in reference_colours.items()}
98
- distances = {
99
- colour : np .linalg .norm (rgb_array - ref_rgb )
100
- for colour , ref_rgb in reference_colours .items ()
101
- }
102
-
103
- # Find the colour with the smallest distance
104
- estimated_colour = min (distances , key = distances .get )
105
-
106
- return estimated_colour
107
-
108
-
109
- def split_and_sample_colours (image , mask , square_size ):
110
- height , width , _ = image .shape
111
- squares_colours = {}
112
- valid_squares = set ()
113
-
114
- square_index = 0
115
-
116
- for y in range (0 , height , square_size ):
117
- for x in range (0 , width , square_size ):
118
- square = image [y : y + square_size , x : x + square_size ]
119
- mask_square = mask [y : y + square_size , x : x + square_size ]
120
-
121
- # Calculate the average colour
122
- average_colour = square .mean (axis = (0 , 1 ))
123
-
124
- # Save the average colour in the dictionary
125
- squares_colours [square_index ] = estimate_colour (average_colour )
126
- # squares_colours[square_index] = average_colour # estimate_colour(average_colour)
127
-
128
- # Check the mask condition
129
- a = np .sum (mask_square )
130
- if np .sum (mask_square ) > 0.5 * square_size * square_size :
131
- valid_squares .add (square_index )
132
-
133
- square_index += 1
134
-
135
- return squares_colours , valid_squares
136
-
137
-
138
22
def gaussian_blur (image , kernel_size , rep = 3 ):
139
23
"""
140
24
Apply Gaussian blur to an RGB image.
@@ -154,51 +38,6 @@ def gaussian_blur(image, kernel_size, rep=3):
154
38
return image
155
39
156
40
157
- def visualize_grids (image , squares_colours , square_size ):
158
- """
159
- Display the image with coloured grids based on average colours.
160
-
161
- Parameters:
162
- image (numpy.ndarray): The original RGB image.
163
- squares_colours (dict): Dictionary with the average colours of each square.
164
- square_size (int): The size of each square.
165
- """
166
- height , width , _ = image .shape
167
- grid_image = np .zeros ((height , width , 3 ), dtype = np .uint8 )
168
-
169
- square_index = 0
170
- for y in range (0 , height , square_size ):
171
- for x in range (0 , width , square_size ):
172
- # Get the colour from the dictionary
173
- colour = np .array (reference_colours [squares_colours [square_index ]])
174
- # Fill the square using numpy slicing
175
- grid_image [y : y + square_size , x : x + square_size ] = colour
176
-
177
- # Optionally draw a white border around the square
178
- grid_image [y : y + square_size , x : x + 1 ] = [255 , 255 , 255 ] # Left border
179
- grid_image [y : y + square_size , x + square_size - 1 : x + square_size ] = [
180
- 255 ,
181
- 255 ,
182
- 255 ,
183
- ] # Right border
184
- grid_image [y : y + 1 , x : x + square_size ] = [255 , 255 , 255 ] # Top border
185
- grid_image [y + square_size - 1 : y + square_size , x : x + square_size ] = [
186
- 255 ,
187
- 255 ,
188
- 255 ,
189
- ] # Bottom border
190
-
191
- square_index += 1
192
-
193
- plt .figure (figsize = (10 , 10 ))
194
- plt .imshow (grid_image )
195
- plt .title ("Image with Average colour Grids" )
196
- plt .axis ("off" )
197
- plt .figure ()
198
- plt .imshow (image )
199
- plt .show ()
200
-
201
-
202
41
def X2conv (in_channels , out_channels , inner_channels = None ):
203
42
inner_channels = out_channels // 2 if inner_channels is None else inner_channels
204
43
down_conv = nn .Sequential (
0 commit comments