⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guisb.m

📁 手写文字识别系统,可以实现图片直接转换成txt文档
💻 M
字号:
function varargout = guisb(varargin)
% GUISB M-file for guisb.fig
%      GUISB, by itself, creates a new GUISB or raises the existing
%      singleton*.
%
%      H = GUISB returns the handle to a new GUISB or the handle to
%      the existing singleton*.
%
%      GUISB('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUISB.M with the given input arguments.
%
%      GUISB('Property','Value',...) creates a new GUISB or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before guisb_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to guisb_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

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help guisb

% Last Modified by GUIDE v2.5 22-Nov-2008 23:17:52

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @guisb_OpeningFcn, ...
                   'gui_OutputFcn',  @guisb_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 guisb is made visible.
function guisb_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 guisb (see VARARGIN)

% Choose default command line output for guisb
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes guisb wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = guisb_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;



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- 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)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%模板
duqu=char('E:\0.jpg','E:\1.jpg','E:\2.jpg','E:\3.jpg','E:\4.jpg','E:\5.jpg','E:\6.jpg','E:\7.jpg','E:\8.jpg','E:\9.jpg','E:\a.jpg','E:\b.jpg','E:\c.jpg','E:\d.jpg','E:\e.jpg','E:\f.jpg','E:\g.jpg','E:\h.jpg','E:\i.jpg','E:\j.jpg','E:\k.jpg','E:\l.jpg','E:\m.jpg','E:\n.jpg','E:\o.jpg','E:\p.jpg','E:\q.jpg','E:\r.jpg','E:\s.jpg','E:\t.jpg','E:\u.jpg','E:\v.jpg','E:\w.jpg','E:\x.jpg','E:\y.jpg','E:\z.jpg');%可以在这这里继续添加你的文件名字
tmp=size(duqu);           
n=tmp(1);    %%%file_num:文件的个数
for k=1:n
a=deblank(duqu(k,:));
P{k}=imread(a);%读取图片数据
%图像边缘清晰化处理
X{k}=im2bw(P{k});
%寻找边界
%寻找下边界
shang=0;
xia=0;
zuo=0;
you=0;
for i=1:length(X{k}(:,1))
    for j=1:length(X{k}(1,:))
        if(X{k}(i,j)==0) 
            xia=i;break;
        end
    end
 end

%寻找右边界
  for i=1:length(X{k}(1,:))
    for j=1:length(X{k}(:,1))
        if(X{k}(j,i)==0) 
            you=i;break;
        end
    end
  end

X{k}=X{k}';
%寻找左边界
 for i=length(X{k}(:,1)):-1:1
    for j=length(X{k}(1,:)):-1:1
        if(X{k}(i,j)==0) 
           zuo=i;break;
        end
    end
 end
%寻找上边界
  for i=length(X{k}(1,:)):-1:1
    for j=length(X{k}(:,1)):-1:1
        if(X{k}(j,i)==0) 
           shang=i;break;
        end
    end
  end
%fprintf('手写文字上下左右边界:\n');
h=[shang xia zuo you] ;   
%fprintf('手写文字数据矩阵:\n');
X{k}=X{k}';
%截图
moban{k}=zeros(xia-shang,you-zuo);
 for i=shang:xia
    for j=zuo:you
    moban{k}(i-shang+1,j-zuo+1)=X{k}(i,j);
    end
 end
end 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%识别
%duqu=char('E:\0.jpg','E:\1.jpg','E:\2.jpg','E:\3.jpg','E:\4.jpg','E:\5.jpg','E:\6.jpg','E:\7.jpg','E:\8.jpg','E:\9.jpg','E:\a.jpg','E:\b.jpg','E:\c.jpg','E:\d.jpg','E:\e.jpg','E:\f.jpg','E:\g.jpg','E:\h.jpg','E:\i.jpg','E:\j.jpg','E:\k.jpg','E:\l.jpg','E:\m.jpg','E:\n.jpg','E:\o.jpg','E:\p.jpg','E:\q.jpg','E:\r.jpg','E:\s.jpg','E:\t.jpg','E:\u.jpg','E:\v.jpg','E:\w.jpg','E:\x.jpg','E:\y.jpg','E:\z.jpg');%可以在这这里继续添加你的文件名字
duqu=char(get(handles.edit1,'string'));
tmp=size(duqu);           
n=tmp(1);    %%%file_num:文件的个数

c=num2str(zeros(36,1));
b=num2str(zeros(n,1));
for i=1:36
    c(i)=char(' ');
end
for j=1:n
    b(i)=char(' ');
end
for k=1:n
a=deblank(duqu(k,:));
P{k}=imread(a);%读取图片数据
%图像边缘清晰化处理
X{k}=im2bw(P{k});
%寻找边界
%寻找下边界
shang=0;
xia=0;
zuo=0;
you=0;
for i=1:length(X{k}(:,1))
    for j=1:length(X{k}(1,:))
        if(X{k}(i,j)==0) 
            xia=i;break;
        end
    end
 end

%寻找右边界
  for i=1:length(X{k}(1,:))
    for j=1:length(X{k}(:,1))
        if(X{k}(j,i)==0) 
            you=i;break;
        end
    end
  end

X{k}=X{k}';
%寻找左边界
 for i=length(X{k}(:,1)):-1:1
    for j=length(X{k}(1,:)):-1:1
        if(X{k}(i,j)==0) 
           zuo=i;break;
        end
    end
 end
%寻找上边界
  for i=length(X{k}(1,:)):-1:1
    for j=length(X{k}(:,1)):-1:1
        if(X{k}(j,i)==0) 
           shang=i;break;
        end
    end
  end
%fprintf('手写文字上下左右边界:\n');
h=[shang xia zuo you] ;   
%fprintf('手写文字数据矩阵:\n');
X{k}=X{k}';
%截图
sb{k}=zeros(xia-shang,you-zuo);
 for i=shang:xia
    for j=zuo:you
    sb{k}(i-shang+1,j-zuo+1)=X{k}(i,j);
    end
 end
end 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%切近度
hang=65;
lie=45;
for t=1:n
    sb{t}=imresize(sb{t},[hang lie]);
    for k=1:36
       moban{k}=imresize(moban{k},[hang lie]);
       w1(k)=1-((sum(sum((sb{t}-moban{k}).*(sb{t}-moban{k}))))/(hang*lie))^0.5;
       w2(k)=1-sum(sum(abs(sb{t}-moban{k})))/(hang*lie);
    end
    r=0.5*(w1+w2);
    [MAX,K]=max(r);
    switch K
     case 1
        c(1)=char('0');
     case 2
        c(2)=char('1');
     case 3
        c(3)=char('2');
     case 4
        c(4)=char('3');
     case 5
        c(5)=char('4');
     case 6
        c(6)=char('5');
     case 7
        c(7)=char('6');
     case 8
        c(8)=char('7');
     case 9
        c(9)=char('8');
     case 10
        c(10)=char('9'); 
     case 11
       c(11)=char('A');
     case 12
        c(12)=char('B');
     case 13
       c(13)=char('C');
     case 14
        c(14)=char('D');
     case 15
       c(15)=char('E');
     case 16
        c(16)=char('F');
     case 17
       c(17)=char('G');
     case 18
        c(18)=char('H');
     case 19
        c(19)=char('I');
     case 20
        c(20)=char('J');
     case 21
       c(21)=char('K');
     case 22
        c(22)=char('L');
     case 23
      c(23)=char('M');
     case 24
        c(24)=char('N');
     case 25
       c(25)=char('O');
     case 26
        c(26)=char('P');
     case 27
       c(27)=char('Q');
     case 28
        c(28)=char('R');
     case 29
       c(29)=char('S');
     case 30
        c(30)=char('T');
     case 31
       c(31)=char('U');
     case 32
        c(32)=char('V');
     case 33
       c(33)=char('W');
     case 34
        c(34)=char('X');
     case 35
       c(35)=char('Y');
     case 36
       c(36)=char('Z');
    end
    
end
i=1;j=1;
syms p
while(i<=36)
    p=c(i);
    if(abs(p)~=32)
        b(j)=c(i);
        j=j+1;
    end
    i=i+1;   
end
set(handles.edit2,'string',b);
clear
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- 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)
set(handles.edit1,'string','');
set(handles.edit2,'string','');

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)





function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -