-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectLetters.m
76 lines (63 loc) · 1.87 KB
/
detectLetters.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
function [newBoard, theLetters] = detectLetters(board)
% Input: detected board from detectBoggleBoardPyramid() function
% Output: smaller, more accurate board, and letters at each position
% First detect boundaries of outermost yellow (255) pixels in order to get
% rid of padding on outside
dimY = size(board, 1);
dimX = size(board, 2);
top = 1;
left = 1;
bottom = dimY;
right = dimX;
for i=1:dimY
if(any(board(i,:)==255))
top = i;
break;
end
end
for j=1:dimY
jj = dimY-j+1;
if(any(board(jj,:)==255))
bottom = jj;
break;
end
end
for k=1:dimX
if(any(board(:,k)==255))
left = k;
break;
end
end
for l=1:dimX
ll = dimX-l+1;
if(any(board(:,ll)==255))
right = ll;
break;
end
end
newBoard = board(top:bottom, left:right);
% Now split into windows and detect letters
load('Letters.mat');
theLetters = cell(4, 4);
realLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
[newDimY, newDimX] = size(newBoard);
cutPointsY = [1 round(newDimY/4) round(newDimY/2) round(3*newDimY/4) newDimY];
cutPointsX = [1 round(newDimX/4) round(newDimX/2) round(3*newDimX/4) newDimX];
for i = 1:4
for j=1:4
% Use a matched filter, calculate correlation, then pick the
% highest correlation
letCorrs = zeros(1, length(letters));
letIm = newBoard(cutPointsY(i):cutPointsY(i+1), cutPointsX(j):cutPointsX(j+1));
letIm = removePadding(letIm);
letIm = letIm - mean(letIm(:));
for k = 1:length(letters);
actualLetIm = letters{k};
actualLetIm = imresize(actualLetIm, size(letIm));
letCorrs(k) = sum(sum(actualLetIm.*letIm));
end
[~, idx] = max(letCorrs);
theLetters{i, j} = realLetters(ceil(idx/20));
end
end
end