How to read image character with OCR?

2 views (last 30 days)
bee lebah
bee lebah on 21 Jun 2022
Answered: bee lebah on 25 Jun 2022
Hi can you help me please.
I have a problem with my program, my ocr program doesn't read letters, it reads only the number '1' which I think is coming from 'else' in file read.m.
And why does this one have a double result?
read.m
function letter=read(imagn,num_letras)
global templates
comp=[ ];
for n=1:num_letras
sem=corr2(templates{1,n},imagn);
comp=[comp sem];
end
vd=find(comp==max(comp));
%*-*-*-*-*-*-*-*-*-*-*-*-*-
if vd==1
letter='ha';
elseif vd==2
letter='na';
elseif vd==3
letter='ca';
elseif vd==4
letter='ra';
elseif vd==5
letter='ka';
elseif vd==6
letter='da';
elseif vd==7
letter='ta';
elseif vd==8
letter='sa';
elseif vd==9
letter='wa';
elseif vd==10
letter='la';
elseif vd==11
letter='pa';
elseif vd==12
letter='dha';
elseif vd==13
letter='ja';
elseif vd==14
letter='ya';
elseif vd==15
letter='nya';
elseif vd==16
letter='ma';
elseif vd==17
letter='ga';
elseif vd==18
letter='ba';
elseif vd==19
letter='tha';
elseif vd==20
letter='nga';
elseif vd==21
letter='hi';
elseif vd==22
letter='ni';
elseif vd==23
letter='ci';
elseif vd==24
letter='ri';
elseif vd==25
letter='ki';
elseif vd==26
letter='di';
elseif vd==27
letter='ti';
elseif vd==28
letter='si';
elseif vd==29
letter='wi';
elseif vd==30
letter='li';
elseif vd==31
letter='pi';
elseif vd==32
letter='dhi';
elseif vd==33
letter='ji';
elseif vd==34
letter='yi';
elseif vd==35
letter='nyi';
elseif vd==36
letter='mi';
elseif vd==37
letter='gi';
elseif vd==38
letter='bi';
elseif vd==39
letter='thi';
elseif vd==40
letter='ngi';
elseif vd==41
letter='hu';
elseif vd==42
letter='nu';
elseif vd==43
letter='cu';
elseif vd==44
letter='ru';
elseif vd==45
letter='ku';
elseif vd==46
letter='du';
elseif vd==47
letter='tu';
elseif vd==48
letter='su';
elseif vd==49
letter='wu';
elseif vd==50
letter='lu';
elseif vd==51
letter='pu';
elseif vd==52
letter='dhu';
elseif vd==53
letter='ju';
elseif vd==54
letter='yu';
elseif vd==55
letter='nyu';
elseif vd==56
letter='mu';
elseif vd==57
letter='gu';
elseif vd==58
letter='bu';
elseif vd==59
letter='thu';
elseif vd==60
letter='ngu';
elseif vd==61
letter='he';
elseif vd==62
letter='ne';
elseif vd==63
letter='ce';
elseif vd==64
letter='re';
elseif vd==65
letter='ke';
elseif vd==66
letter='de';
elseif vd==67
letter='te';
elseif vd==68
letter='se';
elseif vd==69
letter='we';
elseif vd==70
letter='le';
elseif vd==71
letter='pe';
elseif vd==72
letter='dhe';
elseif vd==73
letter='je';
elseif vd==74
letter='ye';
elseif vd==75
letter='nye';
elseif vd==76
letter='me';
elseif vd==77
letter='ge';
elseif vd==78
letter='be';
elseif vd==79
letter='the';
elseif vd==80
letter='nge';
elseif vd==81
letter='ho';
elseif vd==82
letter='no';
elseif vd==83
letter='co';
elseif vd==84
letter='ro';
elseif vd==85
letter='ko';
elseif vd==86
letter='do';
elseif vd==87
letter='to';
elseif vd==88
letter='so';
elseif vd==89
letter='wo';
elseif vd==90
letter='lo';
elseif vd==91
letter='po';
elseif vd==92
letter='dho';
elseif vd==93
letter='jo';
elseif vd==94
letter='yo';
elseif vd==95
letter='nyo';
elseif vd==96
letter='mo';
elseif vd==97
letter='go';
elseif vd==98
letter='bo';
elseif vd==99
letter='tho';
elseif vd==100
letter='ngo';
else
letter='1';
end
gui
% --- Executes on button press in buttonpilgam.
function buttonpilgam_Callback(hObject, eventdata, handles)
open=guidata(gcbo);
[filename, formatfile] = uigetfile ('*','BukaGambar');
im=imread(fullfile(formatfile, filename));
set(open.figure1,'CurrentAxes',open.axes1);
set(imagesc(im));
set(open.axes1,'Userdata',im);
%handles.im = im;
%guidata(hObject,handles);
%axes(handles.axes1);
%imshow(handles.im);
% hObject handle to buttonpilgam (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in button3proses.
function button3proses_Callback(hObject, eventdata, handles)
%Proses Grayscale
open=guidata(gcbo);
g=get(open.axes1,'Userdata');
if size(g,3)==3
g=rgb2gray(g);
end
set(open.figure1,'CurrentAxes',open.axes2);
set(imshow(g));
set(open.figure1,'Userdata',g);
set(open.axes2,'Userdata',g);
%Proses Grayscale to Threeshold
%open=guidata(gcbo);
%g=get(open.axes2,'Userdata');
%if size(g,3)==3
% g=im2bw(g);
%end
%set(open.figure1,'CurrentAxes',open.axes3);
%set(imshow(g));
%set(open.figure1,'Userdata',g);
%set(open.axes3,'Userdata',g);
%Proses Threshold to Biner
open=guidata(gcbo);
g=get(open.axes2,'Userdata');
threshold = graythresh(g);
g=~im2bw(g,threshold);
g2=g;
set(open.figure1,'CurrentAxes',open.axes4);
set(imshow(g2));
g=bwareaopen(g,50);
set(open.figure1,'Userdata',g);
set(open.axes4,'Userdata',g);
%Proses OCR
word=[ ];
text=[ ];
re=g;
load templates
global templates
num_letras=size(templates,2);
while 1
[fl re]=lines(re);
imgn=fl;
n=0;
spacevector = [];
rc = fl;
while 1
%[fc rc space]=letter_crop(rc);
%while 1
%Fcn 'letter_crop' separate letters in a line
[fc rc space]=letter_crop(rc); %fc = first letter in the line
%rc = remaining cropped line
%space = space between the letter
% cropped and the next letter
%uncomment below line to see letters one by one
%figure,imshow(fc);pause(0.5)
img_r = imresize(fc,[42 24]); %resize letter so that correlation
%can be performed
n = n + 1;
spacevector(n)=space;
%Fcn 'read_letter' correlates the cropped letter with the images
%given in the folder 'letters_numbers'
letter = read(img_r,num_letras);
%letter concatenation
word = [word letter];
if isempty(rc) %breaks loop when there are no more characters
break;
end
end
max_space = max(spacevector);
no_spaces = 0;
for x= 1:n %loop to introduce space at requisite locations
if spacevector(x+no_spaces)> (0.75 * max_space)
no_spaces = no_spaces + 1;
for m = x:n
word(n+x-m+no_spaces)=word(n+x-m+no_spaces-1);
end
word(x+no_spaces) = ' ';
spacevector = [0 spacevector];
end
end
%-----------------------------------------------------------------
% Label and count connected components
%[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,[90 90]);
%-------------------------------------------------------------------
% Call fcn to convert image to text
% letter=read(img_r,num_letras);
% Letter concatenation
% max_space = max(spacevector);
% no_spaces = 0;
% for x= 1:n %loop to introduce space at requisite locations
% if spacevector(x+no_spaces)> (1 * max_space)
% no_spaces = no_spaces + 1;
% for m = x:n
% word(n+x-m+no_spaces)=word(n+x-m+no_spaces-1);
% end
% word(x+no_spaces) = ' ';
% spacevector = [0 spacevector];
% end
% end
% word=[word letter];
%end
text = char(text, word);
% Clear 'word' variable
%fprintf(text,'%s\n',word);
word=[ ];
%*When the sentences finish, breaks the loop
if isempty(re) %See variable 're' in Fcn 'lines'
break
end
end
set(handles.figure1,'Visible','on');
set(handles.text8hslteks,'String',[text]);
set(handles.text8hslteks,'FontSize',10);
guidata(hObject, handles);
%cropping
% hObject handle to button3proses (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
What's wrong with my code? Thank you for your attention.

Accepted Answer

bee lebah
bee lebah on 25 Jun 2022
So guys, finally it worked

More Answers (1)

yanqi liu
yanqi liu on 22 Jun 2022
yes,sir,may be
vd=find(comp==max(comp))
to find what vd is?
is the vd in range
  1 Comment
bee lebah
bee lebah on 22 Jun 2022
yes it is, but i don't know how to fix it. Can you help me?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by