function main
clc
close all
% 创建人脸检测对象
faceDetector = vision.CascadeObjectDetector;
% 人脸检测
FaceRecognition(faceDetector);
end
%% 选择图片
function I = SelectPicture()
[FileName,PathName] = uigetfile('*.jpg', '选择一张图片');
if isequal(FileName,0)
disp('没选择图片,请重新选择!')
I = [];
else
I = imread(fullfile(PathName,FileName));
end
end
%% 人脸检测
function [I_faces, bbox] = GetFaces(faceDetector, I)
% 检测人脸
bbox = step(faceDetector, I);
% 创建一个形状插入对象来绘制边框圈出的检测的结果
if size(I, 3) == 1 % 灰度图像,插入白色或黑色框
if mean(I(:)) > 128 % 图像较亮,使用黑框
shapeInserter = vision.ShapeInserter();
else % 图像较暗,使用白框
shapeInserter = vision.ShapeInserter('BorderColor','White');
end
else % 彩色图像,插入红色框
shapeInserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[255 0 0]);
end
% 绘制边框以圈出结果
I_faces = step(shapeInserter, I, int32(bbox));
end
%% 图片人脸检测
function FaceRecognition(faceDetector)
% 鼠标单击响应
function BtnDownFcn(h, evt)
FaceRecognition(faceDetector);
end
% 选择文件
I = SelectPicture();
if isempty(I)
return
end
% 人脸检测
[I_faces, bbox] = GetFaces(faceDetector, I);
close all
% 创建figure对象
fig1 = figure;
pos1 = get(fig1,'Position');
set(fig1,'Position',[10 pos1(2:4)]);
set(fig1,'WindowButtonDownFcn',@BtnDownFcn);
% 显示
figure(fig1)
imshow(I_faces)
title('单击此图片选择另一图片识别')
for i = 1:size(bbox, 1)
text(bbox(i, 1), bbox(i, 2), mat2str(i), 'color', 'r')
end
% 检测
intbbox = int32(bbox);
for i = 1:size(intbbox, 1)
xs = intbbox(i, 1);
xe = xs + intbbox(i,3);
ys = intbbox(i, 2);
ye = ys + intbbox(i,4);
% 创建figure
if rem(i, 16) == 1
fig2 = figure; %#ok
end
function varargout = FaceSystem(varargin)
% FACESYSTEM MATLAB code for FaceSystem.fig
% FACESYSTEM, by itself, creates a new FACESYSTEM or raises the existing
% singleton*.
%
% H = FACESYSTEM returns the handle to a new FACESYSTEM or the handle to
% the existing singleton*.
%
% FACESYSTEM('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FACESYSTEM.M with the given input arguments.
%
% FACESYSTEM('Property','Value',...) creates a new FACESYSTEM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before FaceSystem_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to FaceSystem_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 FaceSystem
% Last Modified by GUIDE v2.5 20-Apr-2018 19:18:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FaceSystem_OpeningFcn, ...
'gui_OutputFcn', @FaceSystem_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 FaceSystem is made visible.
function FaceSystem_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 FaceSystem (see VARARGIN)
% Choose default command line output for FaceSystem
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes FaceSystem wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = FaceSystem_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 str;
global a0;
[filename,pathname]=...
uigetfile({'*.jpg';'*.bmp';'*.gif'},'choose');
str=[pathname filename]
if str~=0;
a0=imread(str);
% 同学在这里写上进度条的代码 等待对话框
h=waitbar(0,'Pleast waiting, reading...');
%*********
axes(handles.axes1);
axis off
imshow(a0);
title('原图像')
waitbar(1,h,'finish');
pause(0.05);
delete(h);
end
% 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)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
global a0;
global dets
global i_face;
global im
i_face=0;
faceDetector = vision.CascadeObjectDetector;
[im, dets] = GetFaces(faceDetector, a0);
DisplayDetections(im, dets);
% --- 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 dets
global i_face;
global a0;
global A_face;
[M,N]=size(dets);
if (i_face>0)&(i_face<=M)
i_face=i_face-1;
i=i_face
A_face=a0(dets(i,2):(dets(i,2)+dets(i,4)),dets(i,1):(dets(i,1)+dets(i,3)),:);
axes(handles.axes2);
axis off
imshow(A_face);
title('待识别的人脸');
end
% --- 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 dets
global a0;
global i_face;
global A_face;
[M,N]=size(dets);
if (i_face>=0)&(i_face<M)
i_face=i_face+1;
i=i_face
A_face=a0(dets(i,2):(dets(i,2)+dets(i,4)),dets(i,1):(dets(i,1)+dets(i,3)),:);
axes(handles.axes2);
axis off
imshow(A_face);
title('待识别的人脸');
end
不知道该怎么运行