function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
% GUI, by itself, creates a new GUI or raises the existing
% singleton*.
%
% H = GUI returns the handle to a new GUI or the handle to
% the existing singleton*.
%
% GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI.M with the given input arguments.
%
% GUI('Property','Value',...) creates a new GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_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 GUI
% Last Modified by GUIDE v2.5 16-Apr-2019 13:58:33
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_OpeningFcn, ...
'gui_OutputFcn', @GUI_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 GUI is made visible.
function GUI_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 GUI (see VARARGIN)
% Choose default command line output for GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_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)
global I;
[filename,pathname]=uigetfile({'*.JPG'},'选择要检测的图片');
str=[pathname,filename];
I = imread(str);%从多媒体文件中读取数据。
axes(handles.axes1);imshow(I);title('原图')
axes(handles.axes2);cla reset;
% --- 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 I;
gray = rgb2gray(I);
% 将图像转化为YCbCr颜色空间
YCbCr = rgb2ycbcr(I);
% 获得图像宽度和高度
heigth = size(gray,1);
width = size(gray,2);
% 根据肤色模型将图像二值化
for i = 1:heigth
for j = 1:width
Y = YCbCr(i,j,1);
Cb = YCbCr(i,j,2);
Cr = YCbCr(i,j,3);
if(Y < 80)
gray(i,j) = 0;
else
if(skin(Y,Cb,Cr) == 1)
gray(i,j) = 255;
else
gray(i,j) = 0;
end
end
end
end
% 二值图像形态学处理
SE=strel('arbitrary',eye(5));
%gray = bwmorph(gray,'erode');
% imopen先腐蚀再膨胀
gray = imopen(gray,SE);
% imclose先膨胀再腐蚀
%gray = imclose(gray,SE);
axes(handles.axes2);
imshow(gray);title('分割');pause(1);
% 取出图片中所有包含白色区域的最小矩形
[L,num] = bwlabel(gray,8);
STATS = regionprops(L,'BoundingBox');
% 存放经过筛选以后得到的所有矩形块
n = 1;
result = zeros(n,4);
imshow(I);
hold on;
for i = 1:num
box = STATS(i).BoundingBox;
x = box(1); %矩形坐标x
y = box(2); %矩形坐标y
w = box(3); %矩形宽度w
h = box(4); %矩形高度h
% 宽度和高度的比例
ratio = h/w;
ux = uint8(x);
uy = uint8(y);
if ux > 1
ux = ux - 1;
end
if uy > 1
uy = uy - 1;
end
% 可能是人脸区域的矩形应满足以下条件:
% 1、高度和宽度必须都大于20,且矩形面积大于400
% 2、高度和宽度比率应该在范围(0.6,2)内
% 3、函数findeye返回值为1
if w < 20 || h < 20 || w*h < 800
continue
elseif ratio < 2 && ratio > 0.6 && findeye(gray,ux,uy,w,h) == 1
% 记录可能为人脸的矩形区域
result(n,:) = [ux uy w h];
n = n+1;
end
end
% 对可能是人脸的区域进行标记
if size(result,1) == 1 && result(1,1) > 0
rectangle('Position',[result(1,1),result(1,2),result(1,3),result(1,4)],'EdgeColor','r');
else
% 如果满足条件的矩形区域大于1则再根据其他信息进行筛选
for m = 1:size(result,1)
m1 = result(m,1);
m2 = result(m,2);
m3 = result(m,3);
m4 = result(m,4);
% 标记最终的人脸区域
if m1 + m3 < width && m2 + m4 < heigth
rectangle('Position',[m1,m2,m3,m4],'EdgeColor','r');
end
end
end
title('人脸定位');
% --- 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 I;
path = 'people\';
fileExt = '*.jpg';
files = dir(fullfile(path,fileExt));
len = size(files,1);
S = zeros(len,1,'double'); % 存储查询图像与库中图像的相似度---阈值是0-1的情况下
for i=1:len
Hu_f1 = zeros( 1,7) ;
fileName = strcat(path,files(i,1).name);
f=imread(fileName);
File_store{i} = f;
count=imghist1(rgb2gray(f));
res(:,i)=count(:,1)';
end
count1=imghist1(rgb2gray(I));
H0=count1(:,1)';
for i=1:len
H1=res(:,i)';
tmp=sum(abs(H0-H1)/sum(H0));
S(i)=tmp;%%
end
[y, idx] = sort(S, 'ascend');
% 查询前10个最相似的图像序号
if idx(1)==1
msgbox('识别是张三');
end
if idx(1)==2
msgbox('识别是阿英');
end
if idx(1)==3
msgbox('识别是李四');
end
if idx(1)==4
msgbox('识别是王菲');
end
if idx(1)==5
msgbox('识别是老于');
end
if idx(1)==6
msgbox('识别是宝宝');
end
if idx(1)==7
msgbox('识别是杰克');
end
if idx(1)==8
msgbox('识别是沃特');
end
if idx(1)==9
msgbox('识别是小张');
end
if idx(1)==10
msgbox('识别是马云');
end
%