-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCRwapper.m
68 lines (65 loc) · 2.4 KB
/
OCRwapper.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
function [ wordRes, scoreRes ] = OCRwapper( imagen )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
% Show image
% imshow(imagen);
% Convert to gray scale
if size(imagen,3)==3 %RGB image
imagen=rgb2gray(imagen);
end
% Convert to BW. Using automtic threshold
threshold = graythresh(imagen);
imagen =~im2bw(imagen,threshold);
% Remove all object containing fewer than 30 pixels
imagen = bwareaopen(imagen,30);
%Storage matrix word and score from image
word=[ ];
score = [];
re=imagen;
%Opens text.txt as file for write
% fid = fopen('OCR/result.txt', 'wt');
% Load templates
global templates
load templates
% Compute the number of letters in template file
num_letras=size(templates,2);
%%
imgn = clip(re);
if isempty(imgn)
% fprintf(fid,'%s %f\n','other', -1);% write result to text file, the final result
wordRes = 'other';
scoreRes = -1;
else
[L, Ne] = bwlabel(imgn);
for n=1:Ne
[r,c] = find(L==n);
% Extract letter
n1=imgn(min(r):max(r),min(c):max(c));
% Resize letter (same size of template)
img_r=imresize(n1,[42 24]);
%Uncomment line below to see letters one by one
%imshow(img_r);pause(0.5)
%-------------------------------------------------------------------
% Call function to convert image to text
[letter, confidence] = read_letter(img_r,num_letras);
% Letter concatenation
word=[word letter];
score = [score confidence];
end
ind = find(score == max(score)); % this can hanle kuangkuang
if (max(score) < 0.2) % set up threshold, ****************************** set here*******************
% fprintf(fid,'%s %f\n','other', -1);% write result to text file, the final result
wordRes = 'other';
scoreRes = -1;
else
% fprintf(fid,'%s %f\n',word(ind), score(ind));% write result to text file, the final result
wordRes = word(ind);
scoreRes = score(ind);
end
% Clear 'word' variable
word=[ ];
end
% fclose(fid);
%Open 'text.txt' file
% winopen('OCR/result.txt')
end