-
Notifications
You must be signed in to change notification settings - Fork 2
/
blackturn.m
92 lines (89 loc) · 3.81 KB
/
blackturn.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
i1 = im2double(imread('1474047530.jpg'));
i2 = im2double(imread('1718357029.jpg'));
%i3 = im2double(imread('1683227760.jpg'));
%function piece_infos = char_recognition(im_board_top_down)
r = load('golden_row_locs.mat');
r = r.r;
c = load('golden_col_locs.mat');
c = c.c;
delta = 100;
[height, width, ~] = size(im_board_top_down);
% figure;
refs_black = load('chess_pieces_ref_black.mat');
refs_black = refs_black.chess_piece_refs;
refs_red = load('chess_pieces_ref_red.mat');
refs_red = refs_red.chess_piece_refs;
start_i = 1;
start_j = 1;
end_i = numel(r);
end_j = numel(c);
for j = start_j:end_j
for i = start_i:end_i
h = r(i);
w = c(j);
im_piece = im_board_top_down(h-delta:h+delta,w-delta:w+delta,:);
% subplot(end_i,end_j,(i-1)*end_j+j);
% imshow(im_piece);
[centers, radii, metric] = imfindcircles(im_piece,[45 55],...
'Sensitivity', 0.95);
if numel(radii)
im_center = [delta+1 delta+1];
im_centers = repmat(im_center, [numel(radii) 1]);
dists = sqrt(sum((im_centers-centers).^2,2));
[val, idx] = min(dists);
centersStrong = centers(idx,:);
radiiStrong = radii(idx);
metricStrong = metric(idx);
dist = dists(idx);
if dist < radiiStrong*1.5
% viscircles(centersStrong, radiiStrong,'EdgeColor','b');
im_piece_gray = rgb2gray(im_piece);
[height_piece,width_piece] = size(im_piece_gray);
im_mask_gray = zeros(height_piece, width_piece);
for k = 1:height_piece
for l = 1:width_piece
pt = [l, k];
dist = sqrt(sum((pt-centersStrong).^2));
if dist <= radiiStrong
im_mask_gray(k,l) = 1;
end
end
end
im_mask = repmat(im_mask_gray, [1 1 3]);
im_piece_masked = im_piece.*im_mask;
match_max = 0;
for refs_black_key_cell = refs_black.keys
refs_black_key = refs_black_key_cell{1};
im_ref = refs_black(refs_black_key);
sift_result = sift_match(im_ref, rgb2gray(im_piece_masked));
if sift_result.ransac > match_max
match_max = sift_result.ransac;
match_key = refs_black_key;
match_color = 'black';
end
end
for refs_red_key_cell = refs_red.keys
refs_red_key = refs_red_key_cell{1};
im_ref = refs_red(refs_red_key);
sift_result = sift_match(im_ref, rgb2gray(im_piece_masked));
if sift_result.ransac > match_max
match_max = sift_result.ransac;
match_key = refs_red_key;
match_color = 'red';
end
end
if match_max > 0
piece_infos(i, j).name = match_key;
piece_infos(i, j).color = match_color;
piece_infos(i, j).im = im_piece_masked;
else
piece_infos(i, j).name = 'none';
piece_infos(i, j).color = 'none';
piece_infos(i, j).im = [];
end
% piece_infos(i, j)
end
end
end
end
%end