天天看點

【印刷字元識别】基于matlab模闆比對英文字母識别【含Matlab源碼 808期】

一、簡介

1 概述

模式識别就是通過計算機,用數學模型求解的方法研究模式的自動處理和判讀。在模式識别的各種方法中,模闆比對是最容易的一種,其數學模型易于建立,通過模闆比對對數字圖像模式識别有助于我們了解數學模型在數字圖像中的應用。

2 模闆比對算法

2.1 相似性測度求比對

模闆比對的實際操作思路很簡單:拿已知的模闆,和原圖像中同樣大小的一塊區域去對。最開始時,模闆的左上角點和圖像的左上角點是重合的,拿模闆和原圖像中同樣大小的一塊區域去對比,然後平移到下一個像素,仍然進行同樣的操作, ……所有的位置都對完後,差别最小的那塊就是我們要找的物體。

以上所描述的是相似性測度法求比對的求解思路,其在計算機中操作的如圖2所示。設模闆T疊放在搜尋圖上平移,被模闆覆寫搜尋圖下的那個圖像叫做子圖Si , j,i , j 為這塊子圖的左上角像素點在S圖的坐标,稱為參考點,從圖2可知,i , j 的取值範圍是:1<i ,j <N- M+1. 現在可以比較T和Si , j的内容。若兩者一緻,則T和S之差為零. 是以,可用下列公式(1) 和公式(2) 來衡量T和Si , j的相似程度。

【印刷字元識别】基于matlab模闆比對英文字母識别【含Matlab源碼 808期】
【印刷字元識别】基于matlab模闆比對英文字母識别【含Matlab源碼 808期】

在(2) 式中第3項表示模闆總能量,是一個與(i , j) 無關的常數;第1項是模闆覆寫下子圖的能量,它随着(i , j) 的位置緩慢地改變;第2項表示的子圖與模闆的互相關系,随着(i , j) 的改變而改變,當T和Si , j比對時這項取值最大。是以可用下列相關函數(3) 作相似性測度。

【印刷字元識别】基于matlab模闆比對英文字母識别【含Matlab源碼 808期】

當矢量t 和S1之間的夾角為0時,即當S1(i , j) =kt 時(k為常量) ,有R(i , j) =1,否則R(i , j) <1. 顯然R(i , j) 越大,模闆T和Si , j就越相似,點(i , j) 就是我們要尋找的比對點。

2.2 序貫相似性檢測的算法

用相關法求比對的計算量很大,因為模闆要在(N- M+1)2個參考位置上作相關計算,除了在比對點外,其它點作的都是無用功。是以,人們提出一種叫序貫相似性檢測的算法,簡稱SSDA(Sequential SimiliarityDetectionAlgorithm) 其要點是:

在數字圖像中,SSDA法用公式(6) 計算圖像f ( x, y) 在點(i , j) 的非相似度m(i , j) 作為比對尺度。式中(i , j) 表示的不是模闆中心坐标,而是它左上角坐标。模闆的大小為n ×m。

【印刷字元識别】基于matlab模闆比對英文字母識别【含Matlab源碼 808期】

如果在(i , j) 處圖像中有和模闆一緻的圖案時,則m(i , j) 值很小,反之則很大。特别是模闆和搜尋圖下的子圖部分,完全不一緻的場合下,如果在模闆内的各像素與圖像重合部分對應的像素灰階差的絕對值依次增加,其和會急劇增大。 是以,在作加法時,如果灰階差的絕對值部分和超過某一門檻值時,就認為這個位置不存在和模闆一緻的圖案,進而轉移到下一個位置上計算m(i , j)。并且在這模闆下的各像素點計算中止,是以能大幅度地縮短計算時間,提高比對速度。

根據上述思路,我們可以進一步改進SSDA算法。就是把在圖像上的模闆移動分為粗檢索和細檢索2個階段進行。首先進行粗檢索,它不是讓模闆每次移動1個像素,而是每隔若幹的像素把模闆和圖像重疊,并且計算比對的尺度,進而求出待尋找的圖案大緻存在的範圍。然後在這個範圍内,讓模闆每隔1個像素移動1次,根據比對的尺度确定尋找圖案的所在位置。這樣,整體上計算模闆比對的次數減少了,計算時間縮短,比對的速度提高了。但用這種方法具有漏掉圖像中最恰當位置的危險性。

2.3 相關算法

2個函數的相關性定義,可用公式(7)表示:

【印刷字元識别】基于matlab模闆比對英文字母識别【含Matlab源碼 808期】

f*表示f 的複共轭。我們知道相關理論類似卷積理論,F( u, v) 和 H( u, v) 分别表示f ( x, y) 和h( x, y)的傅立葉變換. 根據卷積理論有

【印刷字元識别】基于matlab模闆比對英文字母識别【含Matlab源碼 808期】

可知卷積是空間域過濾和頻率域過濾之間的紐帶。相關的重要用途在于比對。在比對中,f ( x, y) 是一幅包含物體或區域的圖像。如果想要确定f 是否包含有感興趣的物體或區域,讓h( x, y) 作為那個物體的區域(通常稱該圖像為模闆)。如果比對成功,2個函數的相關值會在h找到f 中相應點的位置上達到最大。從上面分析可知,相關算法可以有2種方法:可在空間域進行,也可在頻率域進行。

2.4 幅度排序相關算法

這種算法有2個步驟組成:

第1步,把實時圖中的各個灰階值按幅度的大小排成列的形式,然後在對它進行二進制(或三進制) 編碼,根據二進制排序的序列,把實時圖變換為二進制陣列的一個有序的集合{ Cn, n =1,2, …, N}。這一過程稱之為幅度排序預處理。

第2步,序貫地将這些二進制序列與基準圖進行由粗到細的相關,直到确定出比對點為止。由于篇幅的限制,這裡就不列出例子了。

2.5 分層搜尋的序慣判決算法

這種分層搜尋算法是直接基于人們先粗後細尋找事物的慣例而形成的,例如,在中國地圖上找肇慶的位置時,可以先找廣東省這個地域,這過程稱為粗相關。然後在這個地域中,再仔細确定肇慶的位置,這叫做細相關。很明顯,利用這種方法,可以很快找出肇慶的位置。因為在這過程中省略了尋找廣東省以外區域所需的時間,這種方法稱為分層搜尋的序貫判決法,利用這種思想形成的分層算法具有相當高的搜尋速度。限于篇幅,這裡隻給出這種操作的一般思路。

3 總結

模式比對本質就是應用數學。模闆比對過程如下:①将圖像數字化,按順序取出每個點的像素值;②代入事先建立的數學模型進行預處理;③選擇一種合适的算法進行模式比對;④将比對後圖像的坐标列出或直接在原圖顯示.。在模闆比對操作中最為關鍵的是如何建立數學模型,這是正确比對的核心。

二、源代碼

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

% Last Modified by GUIDE v2.5 06-Mar-2020 08:13:36

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = main_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)
al=['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z'];
[filename pathname] =uigetfile({'*.png';'*.*'},'打開圖檔');
str=[pathname filename];
if str(1)~=0
    I=imread(str);
    axes(handles.axes1);
    imshow(I);
    [alpha num]=Imagedeal(str);
    G=[];
    for kkkk=1:num
    % Solve an Input-Output Fitting problem with a Neural Network
% Script generated by Neural Fitting app
% Created 04-Dec-2018 12:32:56
%
% This script assumes these variables are defined:
%
%   Train_Data - input data.
%   Train_Result - target data.

x = Train_Data;
t = Train_Result;

% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainscg';  % Scaled conjugate gradient backpropagation.

% Create a Fitting Network
hiddenLayerSize = 113;
net = fitnet(hiddenLayerSize,trainFcn);

% Choose Input and Output Pre/Post-Processing Functions
% For a list of all processing functions type: help nnprocess
net.input.processFcns = {'removeconstantrows','mapminmax'};
net.output.processFcns = {'removeconstantrows','mapminmax'};

% Setup Division of Data for Training, Validation, Testing
% For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand';  % Divide data randomly
net.divideMode = 'sample';  % Divide up every sample
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse';  % Mean Squared Error

% Choose Plot Functions
% For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
    'plotregression', 'plotfit'};

% Train the Network
[net,tr] = train(net,x,t);

% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)

% Recalculate Training, Validation and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,y)
valPerformance = perform(net,valTargets,y)
testPerformance = perform(net,testTargets,y)

% View the Network
view(net)

% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotfit(net,x,t)

% Deployment
% Change the (false) values to (true) to enable the following code blocks.
% See the help for each generation function for more information.
if (false)
    % Generate MATLAB function for neural network for application
    % deployment in MATLAB scripts or with MATLAB Compiler and Builder
    % tools, or simply to examine the calculations your trained neural
    % network performs.
    genFunction(net,'myNeuralNetworkFunction');
    y = myNeuralNetworkFunction(x);
end
if (false)
    % Generate a matrix-only MATLAB function for neural network code
    % generation with MATLAB Coder tools.
    genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');
    y = myNeuralNetworkFunction(x);
end
if (false)
    % Generate a Simulink diagram for simulation or deployment with.
    % Simulink Coder tools.
    gensim(net);
end
           

三、運作結果

四、matlab版本及參考文獻