【圖像識別】基于模板匹配之手寫英文字母識別matlab源碼
簡介
在模式識別中一個最基本的方法,就是模板匹配法(template matching),它基本上是一種統(tǒng)計識別方法。? 為了在圖像中檢測出已知形狀的目標(biāo)物,我們使用這個目標(biāo)物的形狀模板(或窗口)與圖像匹配,在約定的某種準(zhǔn)則下檢測出目標(biāo)物圖像,通常稱其為模板匹配法。它能檢測出圖像中上線條、曲線、圖案等等。它的應(yīng)用包括:目標(biāo)模板與偵察圖像相匹配;文字識別和語音識別等。
原理
我們采用以下的算式來衡量模板T(m,n)與所覆蓋的子圖Sij(i,j)的關(guān)系,已知原始圖像S(W,H),如圖所示:

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


上述公式中第一項為子圖的能量,第三項為模板的能量,都和模板匹配無關(guān)。第二項是模板和子圖的互為相關(guān),隨(i,j)而改變。當(dāng)模板和子圖匹配時,該項由最大值。在將其歸一化后,得到模板匹配的相關(guān)系數(shù):

當(dāng)模板和子圖完全一樣時,相關(guān)系數(shù)R(i,j) = 1。在被搜索圖S中完成全部搜索后,找出R的最大值Rmax(im,jm),其對應(yīng)的子圖Simjm即位匹配目標(biāo)。顯然,用這種公式做圖像匹配計算量大、速度慢。我們可以使用另外一種算法來衡量T和Sij的誤差,其公式為:

計算兩個圖像的向量誤差,可以增加計算速度,根據(jù)不同的匹配方向選取一個誤差閥值E0,當(dāng)E(i,j)>E0時就停止該點的計算,繼續(xù)下一點的計算。
最終的實驗證明,被搜索的圖像越大,匹配的速度越慢;模板越小,匹配的速度越快;閥值的大小對匹配速度影響大;
改進的模板匹配算法
??? 將一次的模板匹配過程更改為兩次匹配;
??? 第一次匹配為粗略匹配。取模板的隔行隔列數(shù)據(jù),即1/4的模板數(shù)據(jù),在被搜索土上進行隔行隔列匹配,即在原圖的1/4范圍內(nèi)匹配。由于數(shù)據(jù)量大幅減少,匹配速度顯著提高。同時需要設(shè)計一個合理的誤差閥值E0:
E0 = e0 * (m + 1) / 2 * (n + 1) / 2
式中:e0為各點平均的最大誤差,一般取40~50即可;
????????? m,n為模板的長寬;
第二次匹配是精確匹配。在第一次誤差最小點(imin, jmin)的鄰域內(nèi),即在對角點為(imin -1, jmin -1), (Imin + 1, jmin + 1)的矩形內(nèi),進行搜索匹配,得到最后結(jié)果。
流程圖

??算法實現(xiàn)的關(guān)鍵問題是進行匹配,求最小距離,其解決方法是和訓(xùn)練集的樣品逐一進行距離的計算,最后找出最相鄰的樣品得到類別號。
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); ?%連接兩個字符串(把路徑和文件串聯(lián)起來)
px = getframe(handles.axes1);%使用 getframe 來將圖像捕獲為影片幀。
CurImg = frame2im(px);%然后,frame2im將捕獲的影片幀轉(zhuǎn)換為圖像數(shù)據(jù)。
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,'字母識別結(jié)果');



?