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

📄 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); %求矩阵duqu的行值及列值          
m=tmp(1);       %文件的个数
for k=1:m
    a=deblank(duqu(k,:));%取出矩阵duqu的第k行的所有元素并转化为字符串即为第k个文件的文件路径
    P{k}=imread(a);%读取模板图片数据
    r=double(P{k}(:,:,1));
    g=double(P{k}(:,:,2));
    b=double(P{k}(:,:,3));
   %图像边缘清晰化处理
   for i=1:length(r(:,1))
       for j=1:length(r(1,:))
           if(r(i,j)<=127.5)
               r(i,j)=0;
           else
               r(i,j)=255;
           end
        
           if(g(i,j)<=127.5)
               g(i,j)=0;
           else
               g(i,j)=255;
           end
        
          if(b(i,j)<=127.5)
               b(i,j)=0;
          else
               b(i,j)=255;
          end
       end
  end
  P{k}=(r+g+b)/3;
 for i=1:length(P{k}(:,1))
    for j=1:length(P{k}(1,:))
        if(P{k}(i,j)<=127.5)
            P{k}(i,j)=0;
        else
            P{k}(i,j)=1;
        end
    end
 end
    X{k}=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 
   %模板图片上下左右边界
   h=[shang xia zuo you]; 
   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(get(handles.edit1,'string'));%获取edit内的内容(待识别文件路径)并作为矩阵赋给duqu矩阵
tmp=size(duqu); %求矩阵duqu的行值及列值           
n=tmp(1);       %文件的个数
for k=1:n
   a=deblank(duqu(k,:));%取出矩阵duqu的第k行的所有元素并转化为字符串即为第k个文件的文件路径
   Pp{k}=imread(a);%读取待识别图片数据
   rr=double(Pp{k}(:,:,1));
   gg=double(Pp{k}(:,:,2));
   bb=double(Pp{k}(:,:,3));
     for i=1:length(rr(:,1))
       for j=1:length(rr(1,:))
           if(rr(i,j)<=127.5)
               rr(i,j)=0;
           else
               rr(i,j)=255;
           end
        
           if(gg(i,j)<=127.5)
               gg(i,j)=0;
           else
               gg(i,j)=255;
           end
        
          if(bb(i,j)<=127.5)
               bb(i,j)=0;
          else
               bb(i,j)=255;
          end
       end
  end
  Pp{k}=(rr+gg+bb)/3;
 for i=1:length(Pp{k}(:,1))
    for j=1:length(Pp{k}(1,:))
        if(Pp{k}(i,j)<=127.5)
            Pp{k}(i,j)=0;
        else
            Pp{k}(i,j)=1;
        end
    end
 end
   Xx{k}=Pp{k};    %将模板图片二值化  %将待识别图片二值化
   kongbai=0;%空白格初值
   %统计待识别图片空白格个数
   for i=1:length(Xx{k}(:,1))
       for j=1:length(Xx{k}(1,:))
           if(Xx{k}(i,j)==1)
             kongbai=kongbai+1;
           end
       end
   end
   %计算空白格所占比例
   bili(k)=kongbai/(length(Xx{k}(:,1))*length(Xx{k}(1,:)));
   if(bili(k)<=0.99) %空白格所占比例大于95%时认为是空白图片
      %寻找边界
      %寻找下边界
      shang=0;xia=0;zuo=0;you=0;%待识别图片边缘初值
      for i=1:length(Xx{k}(:,1))
          for j=1:length(Xx{k}(1,:))
              if(Xx{k}(i,j)==0) 
                  xia=i;break;
              end
          end
      end
      %寻找右边界
      for i=1:length(Xx{k}(1,:))
          for j=1:length(Xx{k}(:,1))
             if(Xx{k}(j,i)==0) 
                  you=i;break;
             end
          end
      end
      Xx{k}=Xx{k}';
      %寻找左边界
      for i=length(Xx{k}(:,1)):-1:1
          for j=length(Xx{k}(1,:)):-1:1
              if(Xx{k}(i,j)==0) 
                  zuo=i;break;
              end
          end
      end 
      %寻找上边界
      for i=length(Xx{k}(1,:)):-1:1
          for j=length(Xx{k}(:,1)):-1:1
              if(Xx{k}(j,i)==0) 
                 shang=i;break;
              end
          end 
      end 
      h=[shang xia zuo you];
      Xx{k}=Xx{k}';
      %截取待识别图片
      sb{k}=zeros(xia-shang,you-zuo);%定义截取后待识别图片初值
      for i=shang:xia
          for j=zuo:you
              sb{k}(i-shang+1,j-zuo+1)=Xx{k}(i,j);
          end 
      end 
   end  
end
%*******************************************************************************%
%模式匹配
c=num2str(zeros(37,1));%申请列向量用于存放模板对应的值
b=num2str(zeros(n,1)); %申请列向量用于存放识别对应的值
c(i)=char(' ');        %初值为空格
b(1)=char(' ');        %初值为空格
hang=65;lie=45;s=1;    %分别为矩阵缩放行列数及b向量下标的起使值
for t=1:n
   if(bili(t)<=0.99)   %如果空白格所占比例小于95%时
      sb{t}=imresize(sb{t},[hang lie]);%将待识别图片矩阵缩放为65行45列
       for k=1:36 
           moban{k}=imresize(moban{k},[hang lie]);;%将模板矩阵缩放为65行45列
           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 
   else                                      %空白格所占比例大于95%时认为是空白图片
       K=t;%在空白格所占比例大于95%时给K赋初值
       c(K)=char('#');%在空白格所占比例大于95%时对应#
   end
   syms p      %定义符号变量p
   p=c(K);
   if(abs(p)~=32)%如果变量p中的值的ASCII码,若其ASCII值不等于空格则赋给向量b
       b(s)=c(K);
       s=s+1;
   end 
   
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 + -