cv
¶
assign_colour(face, bounding_box, mass_centre, colour, segment_width, segment_height)
¶
Assigns the colour value at a mass center in an image to a 3x3 grid according to the sub-sections in the bounding box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
face |
ndarray |
The face to assign the colour to. |
required |
bounding_box |
Tuple[int, int, int, int] |
The bounding box (outer grid) surrounding the mass centres. |
required |
mass_centre |
Tuple[float, float] |
The mass centre, which should be alignable in the bounding box in a 3x3 grid. |
required |
colour |
TileColor |
The colour of the pixel of the mass centre. |
required |
segment_width |
float |
The width of a segment in the bounding box (its width / 3). |
required |
segment_height |
float |
The height of a segment in the bounding box (its height / 3). |
required |
Returns:
Type | Description |
---|---|
ndarray |
A 3x3 numpy array consisting of the TileColors assigned so far. |
Source code in core/cv/cv.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
find_colours(image)
¶
Creates a 3x3 numpy array containing TileColors describing the cube which is pictured in the input image in a minimal way
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray |
The input image (BGR) containing a face of a cube. |
required |
Returns:
Type | Description |
---|---|
ndarray |
A 3x3 numpy array containing the TileColors of the pictured cube. |
Source code in core/cv/cv.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
get_bgr(colour)
¶
Returns the BGR value for a colour supplied as a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
colour |
str |
The colour. |
required |
Returns:
Type | Description |
---|---|
Tuple[float, float, float] |
The BGR value of the colour as a tuple of (B, G, R) in range 0..1 |
Source code in core/cv/cv.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
get_colour(image_hsv, face, bounding_box)
¶
Creates an array of TileColors described in the passed image. More specifically, this function fills in values of the face which are not yet assigned and tries to reconstruct them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_hsv |
ndarray |
An image in HSV color space containing a face of a Rubik's cube. |
required |
face |
ndarray |
The assigned face values so far. |
required |
bounding_box |
Tuple[int, int, int, int] |
The bounding box containing all cube segments. |
required |
Returns:
Type | Description |
---|---|
ndarray |
A 3x3 numpy array containing the TileColors of the pictured cube. |
Source code in core/cv/cv.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
is_square_box(box)
¶
Returns whether or not a box is square-like.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box |
Tuple[int, int, int, int] |
The bounding box in question.
The rectangle is defined as the opencv rectangle -- a tuple of |
required |
Returns:
Type | Description |
---|---|
bool |
A bool, indicating whether or not the supplied box is square-like. |
Source code in core/cv/cv.py
51 52 53 54 55 56 57 58 59 60 61 |
|
is_square_contour(contour, width, height)
¶
Returns whether or not a contour is square-like. The checks are based upon:
- the area of the contour is similar to the area of the bounding box
- the aspect ratio of the bounding box is close to 1 (a square)
- the width and height are within an expected range
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contour |
List[Tuple[int, int]] |
The contour in question.
The contour is defined as the opencv contour -- a list of points (tuples) of |
required |
Returns:
Type | Description |
---|---|
bool |
A bool, indicating whether or not the supplied contour is square-like. |
Source code in core/cv/cv.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
mask_colour(image_hsv, colour)
¶
Masks the supplied image with the supplied colour according to COLOUR_RANGES
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_hsv |
ndarray |
The image (HSV) to be masked. |
required |
colour |
str |
The colour to mask with. |
required |
Returns:
Type | Description |
---|---|
ndarray |
The masked image. |
Source code in core/cv/cv.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
process_images(face_images, use_kmeans=True)
¶
Processes the images of each face of the cube to a 3x3 numpy array containing the respective TileColors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
face_images |
List[numpy.ndarray] |
A list of numpy arrays (BGR images) with one side of the cube shown in each of the image. Six images are expected in total. |
required |
use_kmeans |
bool |
Flag, indicating whether or not use the kmeans clustering algorithm and corresponding computer to assign the colours. Otherwise, use simpler approach using HSV colour ranges. |
True |
Returns:
Type | Description |
---|---|
List[numpy.ndarray] |
A list of 3x3 numpy arrays containing the TileColors of the respective image. |
Source code in core/cv/cv.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|
sort_mass_centres(mass_centre)
¶
Function to sort mass centres by.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mass_centre |
Tuple[float, float] |
The supplied mass centre. |
required |
Returns:
Type | Description |
---|---|
int |
The value used for sorting. |
Source code in core/cv/cv.py
139 140 141 142 143 144 145 146 147 148 149 |
|
to_array(image_hsv, coloured_contours, bounding_box)
¶
Converts a list of coloured contours to a 3x3 numpy array which contains the TileColors depicted in the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_hsv |
ndarray |
The input image containing a face of a cube in HSV color space. |
required |
coloured_contours |
List[Tuple[List[Tuple[int, int]], str]] |
A list of contours and their respective colour. |
required |
bounding_box |
Tuple[int, int, int, int] |
The bounding box containing the contours. |
required |
Returns:
Type | Description |
---|---|
ndarray |
A 3x3 numpy array containing the TileColors of the pictured cube. |
Source code in core/cv/cv.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|