【图像识别】基于模板匹配之手写英文字母识别matlab源码

 

简介

在模式识别中一个最基本的方法,就是模板匹配法(template matching),它基本上是一种统计识别方法。  为了在图像中检测出已知形状的目标物,我们使用这个目标物的形状模板(或窗口)与图像匹配,在约定的某种准则下检测出目标物图像,通常称其为模板匹配法。它能检测出图像中上线条、曲线、图案等等。它的应用包括:目标模板与侦察图像相匹配;文字识别和语音识别等。

原理

我们采用以下的算式来衡量模板T(m,n)与所覆盖的子图Sij(i,j)的关系,已知原始图像S(W,H),如图所示:

【图像识别】基于模板匹配之手写英文字母识别matlab源码_第1张图片

利用以下公式衡量它们的相似性:

上述公式中第一项为子图的能量,第三项为模板的能量,都和模板匹配无关。第二项是模板和子图的互为相关,随(i,j)而改变。当模板和子图匹配时,该项由最大值。在将其归一化后,得到模板匹配的相关系数:

【图像识别】基于模板匹配之手写英文字母识别matlab源码_第2张图片

当模板和子图完全一样时,相关系数R(i,j) = 1。在被搜索图S中完成全部搜索后,找出R的最大值Rmax(im,jm),其对应的子图Simjm即位匹配目标。显然,用这种公式做图像匹配计算量大、速度慢。我们可以使用另外一种算法来衡量T和Sij的误差,其公式为:

计算两个图像的向量误差,可以增加计算速度,根据不同的匹配方向选取一个误差阀值E0,当E(i,j)>E0时就停止该点的计算,继续下一点的计算。

最终的实验证明,被搜索的图像越大,匹配的速度越慢;模板越小,匹配的速度越快;阀值的大小对匹配速度影响大;

 

改进的模板匹配算法

    将一次的模板匹配过程更改为两次匹配;

    第一次匹配为粗略匹配。取模板的隔行隔列数据,即1/4的模板数据,在被搜索土上进行隔行隔列匹配,即在原图的1/4范围内匹配。由于数据量大幅减少,匹配速度显著提高。同时需要设计一个合理的误差阀值E0:

E0 = e0 * (m + 1) / 2 * (n + 1) / 2

式中:e0为各点平均的最大误差,一般取40~50即可;

          m,n为模板的长宽;

第二次匹配是精确匹配。在第一次误差最小点(imin, jmin)的邻域内,即在对角点为(imin -1, jmin -1), (Imin + 1, jmin + 1)的矩形内,进行搜索匹配,得到最后结果。

流程图

【图像识别】基于模板匹配之手写英文字母识别matlab源码_第3张图片

  算法实现的关键问题是进行匹配,求最小距离,其解决方法是和训练集的样品逐一进行距离的计算,最后找出最相邻的样品得到类别号。

 

function varargout = IdentifyEnglish(varargin)
% IDENTIFYENGLISH MATLAB code for IdentifyEnglish.fig
%      IDENTIFYENGLISH, by itself, creates a new IDENTIFYENGLISH or raises the existing
%      singleton*.
%
%      H = IDENTIFYENGLISH returns the handle to a new IDENTIFYENGLISH or the handle to
%      the existing singleton*.
%
%      IDENTIFYENGLISH('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in IDENTIFYENGLISH.M with the given input arguments.
%
%      IDENTIFYENGLISH('Property','Value',...) creates a new IDENTIFYENGLISH or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before IdentifyEnglish_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to IdentifyEnglish_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 IdentifyEnglish

% Last Modified by GUIDE v2.5 05-May-2019 16:46:08

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

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes IdentifyEnglish wait for user response (see UIRESUME)
% uiwait(handles.figure1);
axis([0 240 0 240]);

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

% --- Executes on button press in pushbuttonSave.
function pushbuttonSave_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonSave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[f, p] = uiputfile({'*.bmp'},'save image file');%打开用于保存文件的对话框
str = strcat(p,f);  %连接两个字符串(把路径和文件串联起来)
px = getframe(handles.axes1);%使用 getframe 来将图像捕获为影片帧。
CurImg = frame2im(px);%然后,frame2im将捕获的影片帧转换为图像数据。
imwrite(CurImg,str,'bmp');


% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ButtonDown pos1
if strcmp(get(gcf,'SelectionType'),'normal')
    ButtonDown = 1;
    pos1 = get(handles.axes1,'CurrentPoint');
%     disp(pos1);
end




% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ButtonDown pos1
if(ButtonDown == 1)
    pos = get(handles.axes1,'CurrentPoint');
    line([pos1(1,1) pos(1,1)],[pos1(1,2) pos(1,2)],'LineStyle','-','LineWidth',8,'color','black','marker','.','markerSize',25);
pos1 = pos;
end


% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ButtonDown
ButtonDown = 0;


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

% --- Executes on button press in pushbuttonIdentify.
function pushbuttonIdentify_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonIdentify (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
strSample = 'pattern.mat';
px = getframe(handles.axes1);
CurImg = frame2im(px);
%figure; imshow(CurImg);
CurFea = GetFeature(CurImg);%把CurImg属性改成为5x5
load('pattern.mat');
label = Identify(pattern,CurFea);
% msgbox(['字母识别为: ' label],'msg');
str = ['字母识别为:',label];
f = warndlg(str,'字母识别结果');

【图像识别】基于模板匹配之手写英文字母识别matlab源码_第4张图片

【图像识别】基于模板匹配之手写英文字母识别matlab源码_第5张图片

完整代码或者代写添加QQ1575304183

往期回顾>>>>>>

【图像识别】国外车牌识别matlab源码

【图像识别】基于cnn卷积神经网络之验证码识别matlab源码

【图像识别】基于svm植物叶子疾病检测和分类matlab源码​​​​​​​

【图像识别】路面裂缝识别含GUI源码matlab源码​​​​​​​

【图像识别】基于RGB和BP神经网络的人民币识别系统含GUI界面matlab源码​​​​​​​

【图像识别】条形码识别系统matlab源码​​​​​​​

【图像识别】基于不变矩的数字验证码识别含GUI界面matlab源码​​​​​​​

【图像识别】基于模板匹配之手写数字识别系统GUI界面matlab源码​​​​​​​

【图像识别】基于贝叶斯分类器之目标识别matlab源码

【图像识别】身份证号码识别matlab源码

【图像识别】条形码识别系统matlab源码​​​​​​​

【模式识别】基于特征匹配的英文印刷字符识别matlab源码

【图像分类】基于极限学习分类器对遥感图像分类matlab源码​​​​​​​

【图像识别】基于BP神经网络之字母识别matlab源码

【图像特征处理】指纹图像细节特征提取matlab源码

【图像识别】基于反馈神经Hopfield的数字识别matlab源码

【图像识别】基于二值膨胀差分和椒盐滤波之教室内人数识别系统matlab源码

【图像识别】火灾检测matlab源码GUI

【模式识别】基于 Hough变换视频车道线检测matlab源码​​​​​​​

【模式识别】基于matlab Hough变换图片车道线检测

【模式识别】基于差影法之三维人体姿态行为识别matlab源码

【模式识别】指针式表盘识别matlab源码

【图像识别】表情检测matlab源码

【图像检测】基于LSD直线检测matlab源码

【图像识别】基于帧差法跌倒检测matlab源码

【图像识别】基于组合BCOSFIRE过滤器进行墙体裂缝识别matlab源码

【图像边缘检测】基于插值法亚像素边缘检测matlab源码

【模式识别】基于贝叶斯最小错误率手写数字识别matlab 源码

【模式识别】基于PCA手写数字识别matlab 源码

【模式识别】基于模板匹配的手写体数字识别matlab源码

【图像识别】基于模板匹配车牌识别matlab源码含GUI

【图像识别】基于模板匹配之人脸表情识别matlab源码含GUI

【图像识别】基于LBP+LPQ算法融合人脸表情识别matlab源码

【图像识别】基于HSV和RGB模型水果分类matlab源码含 GUI

【图像识别】基于模板匹配之数字识别matlab源码

【图像识别】基于BP神经网络的手写字体识别matlab源码含GUI界面

【图像识别】基于ksvd字典学习之人脸表情识别matlab源码

【图像识别】基于ORL数据库的PCA人脸识别系统matlab源码

【图像检测】基于 gabor滤波器布匹瑕疵检测matlab源码

你可能感兴趣的:(matlab,图像处理)