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(回调函数)
% --- 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) 回调函数
imshow(im) ; %------------------------------显示彩色图像
else
imformats
end
% Hint: get(hObject, ' Value' ) returns push state of togglebutton1
%-------------------------------------- pushbutton2_Callback(回调函数)
% --- 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 %----------------宣布为全域变量
if get (gcbo, 'Value' ) ==1;
imycbcr = rgb2ycbcr(im) ; %------------------------转换为YCbCr颜色空间
axes(handles. axes2)
set(handles. axes2, 'XMinorTick','on' ) %-------------嵌入Axes(2) 回调函数
imshow(imycbcr) ; %----------------------显示YCbCr颜色空间图像
else
imformats
end
% Hint: get(hObject, 'Value' ) returns push state of pushbutton2
%-------------------------------------- pushbutton3_Callback(回调函数)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (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;
37
dim = size(im) ; % ------------------------- 求出图像大小
lpf = 1/9 * ones(3); %----------------------------低通滤波器模板
cb = imycbcr(: ,: ,2) ;
cb = filter2(lpf, cb) ;
cb = reshape(cb, 1, prod(size(cb) ) ) ;
cr = imycbcr(: , : , 3) ;
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) ; %---------------------------------计算任意像素为皮肤的概率
end
end
skin1 = filter2(lpf, skin1) ;
skin1 = skin1./max(max(skin1) ) ;
axes(handles. axes3)
set(handles. axes3, 'XMinorTick' , 'on' ) %-------------嵌入Axes(3) 回调函数
imshow(skin1) ; %------------------------------显示皮肤概率图像
else
imformats
end
% Hint: get(hObject, ' Value' ) returns push state of pushbutton3
%-------------------------------------- pushbutton4_Callback(回调函数)
% --- 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 %----------------宣布为全域变量
if get (gcbo, 'Value' ) ==1;
38
%-----------------------------------------自适应门限值处理过程
previousSkin2 = zeros(size(skin1,1),size(skin1,2)) ;
changelist = [] ;
for threshold = 0.55: -0.1: 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) 回调函数
imshow(skin2); %------------------------------显示皮肤二值化图像
else
imformats
end
% Hint: get(hObject, ' Value' ) returns push state of pushbutton4
%-------------------------------------- pushbutton5_Callback(回调函数)
% --- 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) ; %-----------计算对象的总面积, 为利用面积进行判别做准备
[L,numobj] =bwlabel(skin2, 8) ;
avearea = sumarea/numobj; %-----------计算出对象的平均面积
A = zeros(4, numobj) ;%179
n = 1;
%-----------------------------------根据特征对每个标记进行判别, 找出人脸区域
for m=1: numobj
% ---------------------------------计算区域坐标
[x, y] = find(bwlabel(skin2) == m) ;
% ------------------------获得一副只包含该区域的图像, 让图像其他区域为黑色
bwsegment = bwselect(skin2, y, x, 8) ;
% --------------------- 计算出该区域内分割块的数目
[L, numobjs] = bwlabel(bwsegment, 4) ;
% --------------