【圖像檢測】基于形態(tài)學(xué)實現(xiàn)人臉檢測定位matlab源碼含 GUI
一、簡介
人臉檢測是人臉識別、人機交互、智能視覺監(jiān)控等:工作的前提。近年來,在模式識別與計算機視覺領(lǐng)域,人臉檢測已經(jīng)成為一個受到普遍 重視、研究十分活躍的方向。本文針對復(fù)雜背景下的彩色正面人臉圖像,將膚色分割、模板匹配與候選人臉圖像塊篩選結(jié)合起來,構(gòu)建了人臉檢測實驗系統(tǒng),并用自制的人臉圖像數(shù)據(jù)庫在該系統(tǒng)下進行了一系列的實驗統(tǒng)計。本文首先介紹了人臉檢測技術(shù)研究的背景和現(xiàn)狀,闡明人臉檢測技術(shù)發(fā)展的重要意義,對目前常用的一一些檢測算法進行了總結(jié),然后著重闡述了基于膚色分割和模板匹配驗證的人臉檢測算法。膚色是人臉重要特征,在通過膚色采樣統(tǒng)計和聚類分析后,確立一種在YCbCr空間下的基于高斯模型的膚色分割方法。在YCbCr色彩空間中建立膚色分布的高斯模型,得到膚色概率似然圖像,在最佳動態(tài)閾值選取算法下完成膚色區(qū)域的分割。采用 數(shù)學(xué)形態(tài)學(xué)和一些先驗知識對所得到的膚色區(qū)域進行人臉特征篩選,進–步剔除非人臉膚色區(qū)域,減少候選人臉數(shù)量,簡化后續(xù)檢測過程的處理。本文使用平均模板匹配方法對候選人臉進行確認(rèn),并針對圖像中的人臉通常有一定角度旋轉(zhuǎn)和尺寸大小不確定的問題,通過計算候選人臉圖像塊的偏轉(zhuǎn)角度和面積,并以此調(diào)整模板,優(yōu)化模板配準(zhǔn),提高模板匹配的準(zhǔn)確性,同時避免使用多尺度模板進行多次匹配運算,提高算法效率。提出利用候選人臉圖像區(qū)域和模板質(zhì)心作為配準(zhǔn)的原點,抑制人臉圖像噪聲的干擾。最后構(gòu)建了基于膚色分割和模板驗證的人臉檢測試驗系統(tǒng),并對該系統(tǒng)采用自制人臉圖像數(shù)據(jù)庫進行測試。實驗結(jié)果表明,系統(tǒng)算法是有效的,具有較高的檢測性能和低的誤判率。
二、源代碼
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% ? ? ?UNTITLED, by itself, creates a new UNTITLED or raises the existing
% ? ? ?singleton*.
%
% ? ? ?H = UNTITLED returns the handle to a new UNTITLED or the handle to
% ? ? ?the existing singleton*.
%
% ? ? ?UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% ? ? ?function named CALLBACK in UNTITLED.M with the given input arguments.
%
% ? ? ?UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% ? ? ?existing singleton*. ?Starting from the left, property value pairs are
% ? ? ?applied to the GUI before untitled_OpeningFcn gets called. ?An
% ? ? ?unrecognized property name or invalid value makes property application
% ? ? ?stop. ?All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2.5 19-May-2020 15:25:06
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', ? ? ? mfilename, ...
? ?'gui_Singleton', ?gui_Singleton, ...
? ?'gui_OpeningFcn', @untitled_OutputFcn, ...
? ?'gui_OutputFcn', ?@untitled_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 untitled is made visible.
function untitled_OutputFcn(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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
%-------------------------------------- pushbutton1_Callback(回調(diào)函數(shù))
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
global im imycbcr skin1 skin2 lpf %----------------宣布為全域變量
% 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)
if get (gcbo, 'Value' ) ==1;
? ?im=imread('10.jpg' ) ; %------------------------------讀入彩色圖像
? ?axes(handles.axes1)
? ?set(handles.axes1, 'XMinorTick' , 'on' ) %-------------嵌入Axes(1) 回調(diào)函數(shù)
? ?imshow(im) ; %------------------------------顯示彩色圖像
else
? ?imformats
end
% Hint: get(hObject, ' Value' ) returns push state of togglebutton1
%-------------------------------------- pushbutton2_Callback(回調(diào)函數(shù))
% --- 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 im imycbcr skin1 skin2 lpf %------
? ?cr = filter2(lpf, cr) ;
? ?cr = reshape(cr, 1, prod(size(cr) ) ) ;
? ?bmean = mean(cb) ; %--------------------------------求平均值
? ?rmean = mean(cr) ;
? ?brcov = cov(cb, cr) ;
? ?skin1 = zeros(dim(1) , dim(2) ) ;
? ?for i = 1: dim(1)
? ? ? ?for j = 1: dim(2)
? ? ? ? ? ?cb = double(imycbcr(i, j, 2) ) ;
? ? ? ? ? ?cr = double(imycbcr(i, j, 3) ) ;
? ? ? ? ? ?x =[(cb-bmean);(cr-rmean)] ;
? ? ? ? ? ?skin1(i,j) = exp(-0.5*x'*inv(brcov)*x) ; %---------------------------------計算任意像素為皮膚的概率
? ? ?
? ?skin1 = skin1./max(max(skin1) ) ;
? ?axes(handles. axes3)
? ?set(handles. axes3, 'XMinorTick' , 'on' ) %-------------嵌入Axes(3) 回調(diào)函數(shù)
? ?imshow(skin1) ; %------------------------------顯示皮膚概率圖像
else
? ?imformats
end
% Hint: get(hObject, ' Value' ) returns push state of pushbutton3
%-------------------------------------- pushbutton4_Callback(回調(diào)函數(shù))
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im imycbcr skin1 skin2 lpf %----------------: 0.05
? ? ? ?skin2 = zeros(size(skin1,1),size(skin1,2)) ;
? ? ? ?skin2(find(skin1>threshold)) = 1;
? ? ? ?change = sum(sum(skin2 - previousSkin2) ) ;
? ? ? ?changelist = [changelist change] ;
? ? ? ?previousSkin2 = skin2;
? ?end
? ?%----------------------------------找出最佳門限值
? ?[C, I] = min(changelist) ;
? ?optimalThreshold = (7-I) *0.1;
? ?skin2 = ones(size(skin1,1),size(skin1,2)) ;
? ?skin2(find(skin1>optimalThreshold))= 0;
? ?skin2 = filter2(lpf, skin2) ; %------------------------中值濾波
? ?axes(handles.axes5)
? ?set(handles. axes5, 'XMinorTick','on' ) %-------------嵌入Axes(4) 回調(diào)函數(shù)
? ?imshow(skin2); %------------------------------顯示皮膚二值化圖像
else
? ?imformats
end
% Hint: get(hObject, ' Value' ) returns push state of pushbutton4
%-------------------------------------- pushbutton5_Callback(回調(diào)函數(shù))
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im imycbcr skin1 skin2 lpf %----------------宣布為全域變量
if get (gcbo, 'Value' ) ==1;
? ?sumarea = bwarea(skin2) ; %-----------計算對象的總面積, 為利用面積進行判別做準(zhǔn)備
? ?[L,numobj] =bwlabel(skin2, 8) ;
? ?avearea = sumarea/numobj; %-----------計算出對象的平均面積
? ?A = zeros(4, numobj) ;%179
? ?n = 1;
?
? ? ? ?% ------------------------獲得一副只包含該區(qū)域的圖像, 讓圖像其他區(qū)域為黑色
? ? ? ?bwsegment = bwselect(skin2, y, x, 8) ;
? ? ? ?% --------------------- 計算出該區(qū)域內(nèi)分割塊的數(shù)目
? ? ? ?[L, numobjs] = bwlabel(bwsegment, 4) ;
? ? ? ?% ---------------------------------得到區(qū)域孔的數(shù)目
? ? ? ?numfeatures = bweuler(bwsegment, 4) ;
? ? ? ?numholes = 1 - numfeatures;
? ? ? ?% --------------------------------掃描得出區(qū)域坐標(biāo)
? ? ? ?top = -1;
? ? ? ?buttom =-1;
? ? ? ?left = -1;
? ? ? ?right = -1;
? ? ? ?dim=size(bwsegment);
? ? ? ?for i=1:dim(1)
? ? ? ? ? ?for j=1:dim(2)
? ? ? ? ? ? ? ?if(bwsegment(i,j) ~= 0)
? ? ? ? ? ? ? ? ? ?top=i;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?end;
? ? ? ? ? ?end;
? ? ? ? ? ?if (top ~= -1) break; end;
? ? ? ?end;
三、運行結(jié)果


?