900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 【人脸识别】基于模板匹配算法实现人脸识别matlab源码

【人脸识别】基于模板匹配算法实现人脸识别matlab源码

时间:2020-08-15 22:31:26

相关推荐

【人脸识别】基于模板匹配算法实现人脸识别matlab源码

简介

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

原理

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

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

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

当模板和子图完全一样时,相关系数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)的矩形内,进行搜索匹配,得到最后结果。

流程图

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

function varargout = face(varargin)% FACE MATLAB code for face.fig%FACE, by itself, creates a new FACE or raises the existing%singleton*.%%H = FACE returns the handle to a new FACE or the handle to%the existing singleton*.%%FACE('CALLBACK',hObject,eventData,handles,...) calls the local%function named CALLBACK in FACE.M with the given input arguments.%%FACE('Property','Value',...) creates a new FACE or raises the%existing singleton*. Starting from the left, property value pairs are%applied to the GUI before face_OpeningFcn gets called. An%unrecognized property name or invalid value makes property application%stop. All inputs are passed to face_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 face% Last Modified by GUIDE v2.5 18-Dec- 12:02:18% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @face_OpeningFcn, ...'gui_OutputFcn', @face_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before face is made visible.function face_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 face (see VARARGIN)% Choose default command line output for facehandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes face wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = face_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 structurevarargout{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)% read image to be recognizeglobal im;[filename, pathname] = uigetfile({'*.bmp'},'choose photo');str = [pathname, filename];im = imread(str);axes( handles.axes1);imshow(im);% --- 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)global imglobal referenceglobal Wglobal imgmeanglobal col_of_dataglobal pathnameglobal img_path_list% 预处理新数据im = double(im(:));objectone = W'*(im - imgmean);distance = 100000000;% 最小距离法,寻找和待识别图片最为接近的训练图片for k = 1:col_of_datatemp = norm(objectone - reference(:,k));if(distance>temp)aimone = k;distance = temp;aimpath = strcat(pathname, '/', img_path_list(aimone).name);axes( handles.axes2 )imshow(aimpath)endend% 显示测试结果% aimpath = strcat(pathname, '/', img_path_list(aimone).name);% axes( handles.axes2 )% imshow(aimpath)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。