-
Notifications
You must be signed in to change notification settings - Fork 2
/
char_recognition.m
96 lines (82 loc) · 3.29 KB
/
char_recognition.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
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 = 70;
[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;
refs=load('chess_piece_refs.mat');
refs = refs.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);
%test centers
%为了显示效果,未加“;”
real_centers{i,j}=[w-delta+centers(1),h-delta+centers(2)]
%此处real_centers上所在位置的为golden视角图中像素值
if dist < radiiStrong*1.3
%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_key_cell = refs.keys;
refs_key = refs_key_cell{1};
im_ref = refs(refs_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_key;
%match_color = 'black';
end
end
if match_max > 0
piece_infos(i, j).name = match_key;
%piece_infos(i, j).centers=real_centers{i,j};
%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