-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstickersToGrid.m
38 lines (34 loc) · 1.17 KB
/
stickersToGrid.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function [grid] = stickersToGrid(stickerMask, image, currentSide)
global stickerColors;
colors = ["R" "G" "B" "O" "W" "Y"];
centroids = ones(9, 3)*-1;
numStickers = max(max(stickerMask));
if numStickers == 9
for i=1:numStickers
[xs, ys] = find(stickerMask == i);
color = double(squeeze(image(xs(1), ys(1), :)));
distances = zeros(1, 6);
if i==5
global centerColor;
centerColor = color;
end
for j=1:6
r2 = (color(1) - stickerColors(j, 1))^2;
g2 = (color(2) - stickerColors(j, 2))^2;
b2 = (color(3) - stickerColors(j, 3))^2;
distances(j) = r2+g2+b2;
end
[~, idx] = min(distances);
centroids(i, 1) = mean(xs)*3 + mean(ys);
centroids(i, 2) = mean(ys);
centroids(i, 3) = idx;
end
else
disp("there are not 9 stickers");
grid = ["-" "-" "-"; "-" "-" "-"; "-" "-" "-"];
return;
end
centroids = sortrows(centroids, 1);
grid = reshape(centroids(1:9,3), 3, 3);
grid = colors(grid)';
end