-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOCR_Window.m
More file actions
200 lines (182 loc) · 6.59 KB
/
OCR_Window.m
File metadata and controls
200 lines (182 loc) · 6.59 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
function varargout = OCR_Window(varargin)
% OCR_WINDOW M-file for OCR_Window.fig
% OCR_WINDOW, by itself, creates a new OCR_WINDOW or raises the existing
% singleton*.
%
% H = OCR_WINDOW returns the handle to a new OCR_WINDOW or the handle to
% the existing singleton*.
%
% OCR_WINDOW('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in OCR_WINDOW.M with the given input arguments.
%
% OCR_WINDOW('Property','Value',...) creates a new OCR_WINDOW or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before OCR_Window_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to OCR_Window_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help OCR_Window
% Last Modified by GUIDE v2.5 29-Apr-2010 09:32:14
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @OCR_Window_OpeningFcn, ...
'gui_OutputFcn', @OCR_Window_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes just before OCR_Window is made visible.
function OCR_Window_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to OCR_Window (see VARARGIN)
set(hObject,'Name','Arabic OCR Project');
movegui(hObject,'center');
global net;
global all_words;
if exist('DataBase.mat','file') == 2
saved_items = load('DataBase.mat','-mat');
all_words = saved_items.all_words;
net = saved_items.net;
else
error_no_nn_exist;
end
% Choose default command line output for OCR_Window
handles.output = hObject;
guidata(hObject, handles);
% Update handles structure
% UIWAIT makes OCR_Window wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Outputs from this function are returned to the command line.
function varargout = OCR_Window_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ÒÑ ÇáÇÓÊÚÑÇÖ
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.bmp;*.jpg;*.gif;*.png';'*.*'}, 'Pick an Image File','Tests/');
if pathname ~= 0
S = imread([pathname,filename]);
axes(handles.axes1);
imshow(S);
handles.S = S;
guidata(hObject, handles);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ÒÑ ÇáÊãííÒ
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global net;
global all_words;
try
I = handles.S;
%Threshold
Igray = rgb2gray(I);
Ibw = im2bw(Igray,graythresh(Igray));
Iedge = edge(uint8(Ibw));
%--------------------------------
se = strel('Line',25,0);
se1 = strel('Line',20,90);
Iedge2 = imdilate(Iedge, se);
Iedge3 = imdilate(Iedge2, se1);
Ifill = imfill(Iedge3,'holes');
[Ilabel num] = bwlabel(Ifill);
Iprops = regionprops(Ilabel);
Ibox = [Iprops.BoundingBox];
[y,x]=size(Ibox);
x=x/4;
Ibox = reshape(Ibox,[4 x]);
Fbox = [];
line = [];
counter = 0;
for qq =1:size(Ibox,2)
Tbox = [];
min = 1024;
avg_height = 0;
for j = 1:size(Ibox,2)
if min > Ibox(2,j)
min = Ibox(2,j);
counter = counter + 1;
end
avg_height = avg_height + Ibox(4,j);
end
avg_height = avg_height / size(Ibox,2);
for j = 1:size(Ibox,2)
if abs(min - Ibox(2,j)) < avg_height
Tbox = [Tbox Ibox(:,j)];
line = [line counter];
Ibox(2,j) = 2024;
end
end
if size(Tbox,1) > 0
Tbox = sortrows(Tbox',-1)';
Fbox = [Fbox Tbox];
end
end
Ibox = Fbox;
fid = fopen('Arabic_output.txt', 'w', 'n', 'UTF-8');
current_line = line(1);
for cnt = 1:size(Ibox,2)
rectangle('position',Ibox(:,cnt),'edgecolor','r');
img{cnt} = imcrop(Ibw,Ibox(:,cnt));
%pre-process
bw2 = charcrop(img{cnt});
%------------
%Feature Extraction
charvec = figresize(bw2);
%------------
%Recognize
result = sim(net,charvec);
if sum(ones(size(result))-result<=0.05) == 1 && sum(result >= 0.2) == 1
[val, num] = max(result);
str = char(all_words{num});
else
str = '?';
end
if current_line == line(cnt)
fwrite(fid,' ', 'char');
else
str1 = char(13);
fwrite(fid, str1, 'char');
str2 = char(10);
fwrite(fid, str2, 'char');
current_line = line(cnt);
end
fwrite(fid, str, 'char');
%------------
end
fclose(fid);
dos('notepad Arabic_output.txt &');
guidata(hObject, handles);
catch ME
Error_no_word;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%