-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrubiksSolver.m
148 lines (129 loc) · 4.04 KB
/
rubiksSolver.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
clear;
close all;
addpath(genpath("solving_library"));
cam = webcam;
global cubeToDraw;
fullGrid(1:3,1:3,1:6) = '-';
cubeToDraw = fullGrid;
global currentSide;
currentSide = 1;
lastSide = 0;
global keepLooping;
keepLooping = true;
global paused;
paused = false;
global centerColor;
centerColor = [0 0 0];
global stickerColors;
stickerColors = zeros(6, 3);
stickerColors(1,:) = [123, 38, 16];
stickerColors(2,:) = [12, 120, 84];
stickerColors(3,:) = [25, 50, 190];
stickerColors(4,:) = [213, 106, 40];
stickerColors(5,:) = [160, 180, 220];
stickerColors(6,:) = [165, 150, 10];
viewAngle = [109.5 156.5 254 -15.5 113 68
17.5 20.5 23.5 25.5 53 -70];
set(gcf, 'KeyPressFcn', @keyPressed)
while keepLooping
drawnow
if ~paused
% original_image = imread("our_images/cube3.png");
original_image = cam.snapshot;
original_image = imresize(original_image, 128/size(original_image,2));
[L,Centers] = imsegkmeans(original_image,17);
image = label2rgb(L, im2double(Centers));
stickerMask = kMeansImageToStickerMask(L);
onlyStickersImage = cropImage(image, ~stickerMask);
subplot(1, 4, 1); imshow(image); title("k-means");
subplot(1, 4, 2); imshow(onlyStickersImage); title("only stickers");
grid = stickersToGrid(bwlabel(stickerMask), image, currentSide);
if grid(1) ~= "-"
fullGrid(:,:,currentSide) = grid;
cubeToDraw = fullGrid;
subplot(1, 4, 3); rubplot(color2idx(fullGrid)); title("current side: "+currentSide);
view(viewAngle(1, currentSide), viewAngle(2, currentSide));
elseif lastSide ~= currentSide
lastSide = currentSide;
subplot(1, 4, 3); rubplot(color2idx(fullGrid)); title("current side: "+currentSide);
view(viewAngle(1, currentSide), viewAngle(2, currentSide));
end
subplot(1, 4, 4); showStickerColors(); title("colors");
end
end
% kill program
close all;
clear cam;
fprintf("Rubik's Cube Solver Quit.\n");
function [mask] = kMeansImageToStickerMask(L)
mask = zeros(size(L, 1), size(L, 2));
for i=1:max(max(L))
cc = bwlabel(L==i);
for j=1:max(max(cc))
count = sum(sum(cc==j));
if count > 70 && count < 250
s = squareness(cc==j);
if s > 0.8
mask(cc==j) = 1;
1;
end
end
end
end
end
function [croppedImage] = cropImage(image, mask)
r = image(:,:,1);
g = image(:,:,2);
b = image(:,:,3);
r(mask==1) = 0;
g(mask==1) = 0;
b(mask==1) = 0;
image(:,:,1) = r;
image(:,:,2) = g;
image(:,:,3) = b;
croppedImage = image;
end
function keyPressed(~, event)
global currentSide;
global keepLooping;
global paused;
global cubeToDraw;
global centerColor;
global stickerColors;
switch event.Key
case 'rightarrow'
currentSide = min(currentSide+1, 6);
disp("new current side: " + currentSide);
case 'leftarrow'
currentSide = max(currentSide-1, 1);
disp("new current side: " + currentSide);
case 'return'
solution = Solve45(color2idx(cubeToDraw));
for i=1:size(solution, 2)
fprintf("%s ", string(solution(i)));
end
fprintf("\n");
animateMoves(color2idx(cubeToDraw), solution);
case 'space'
paused = ~paused;
if paused
disp('resumed');
else
disp('paused');
end
case {'q', 'escape'}
keepLooping = false;
case '1'
stickerColors(1,:) = centerColor;
case '2'
stickerColors(2,:) = centerColor;
case '3'
stickerColors(3,:) = centerColor;
case '4'
stickerColors(4,:) = centerColor;
case '5'
stickerColors(5,:) = centerColor;
case '6'
stickerColors(6,:) = centerColor;
end
end