function varargout = gui1(varargin)
% GUI1 M-file for gui1.fig
% GUI1, by itself, creates a new GUI1 or raises the existing
% singleton*.
%
% H = GUI1 returns the handle to a new GUI1 or the handle to
% the existing singleton*.
%
% GUI1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI1.M with the given input arguments.
%
% GUI1('Property','Value',...) creates a new GUI1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gui1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gui1_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 gui1
% Last Modified by GUIDE v2.5 26-May-2013 12:36:00
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui1_OpeningFcn, ...
'gui_OutputFcn', @gui1_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 gui1 is made visible.
function gui1_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 gui1 (see VARARGIN)
% Choose default command line output for gui1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui1_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)
set(handles.axes1,'visible','off');
global numframes;
[filename,pathname]=...
uigetfile({'*.*';'*.mp4'},'请选择视频文件'); %选择视频路径
str=[pathname filename]; %合成路径+文件名
mybar=waitbar(0,'正在加载,请稍等....');%进度条程序
mov=VideoReader(str); %读入
waitbar(0.1);
vidframes = read(mov);
waitbar(0.2);
numframes = get(mov, 'numberOfFrames');
waitbar(0.4);
myRoot = 'D:\picture';%创建一个用于保存图片的路径
if ~isdir(myRoot) %判断路径是否存在
mkdir(myRoot);
end
for i=1:numframes
strtemp=strcat('D:\picture\',int2str(i),'.','jpg');%将每祯转成jpg的图片
imwrite(vidframes(:,:,:,i),strtemp);
end
waitbar(0.7);
I=cell(1,numframes);
waitbar(0.75);
waitbar(0.8);
waitbar(1);
close(mybar)
for b=1:numframes
axes(handles.axes1); %使用第一个axes(要在GUI中设计好一个axes1)
f1=imshow(['D:\picture\',int2str(b),'.jpg']);
I{b}=f1;
pause(0.025)
end
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
% --- 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 numframes;
I=cell(1,numframes);
for c=1:numframes
f1=imread(['D:\picture\',int2str(c),'.jpg']);
f2=rgb2ntsc(f1); %rgb颜色模型转ntsc颜色模型,可分离图像的亮度,色调,饱和度
f3=f2(:,:,2);%提出色调图像
[m n]=size(f3);
A=zeros(m,n);
for i=1:m
for j=1:n
if f3(i,j)>0.03&&f3(i,j)<0.16 %硬阈值0.03-0.16是人脸色调范围
A(i,j)=1;
end
end
end
sr=strel('disk',6);%用圆盘形结构进行闭操作去孔洞
f4=imclose(A,sr);
f5=bwlabel(f4);%标记8连通区域
B=regionprops(f5,'area');%测量标注矩阵f5中每一个标注区域的一系列属性,此处为面积
Se=[B.Area];Sm=max(Se); %找到面积最大的标记区域
if Sm>m*n/27
f6=bwareaopen(f4,Sm);%删除小面积标记区域
%%%%%计算识别出的人脸区域并标记
x=m;
k=m;
p=n;
for i=1:m
if any(f6(i,:))==1%找到人脸区域上边界
x=i;
break
end
end
for i=x:m
if f6(i,:)==0%找到人脸区域下边界
k=i;
break
end
end
for j=1:n
if any(f6(:,j))==1
y=j;
break
end
end
for j=y:n
if f6(:,j)==0
p=j;
break
end
end
k1=k-x;%识别区域高度
q=p-y;%识别区域宽度
if k1>0.5*q&&k1<3*q%根据人脸形态学知识再次判断识别区域是否为人脸
f7=imcrop(f6,[y,x,q,0.4*k1]);%返回指定剪裁区域
%再进行一次筛选
[n1 m1]=size(f7);
f8=bwlabel(f7);
C=regionprops(f8,'area');
Si=[C.Area];
Sm=max(Si);
if Sm/(n1*m1)>0.3
f9=bwareaopen(f7,floor(0.5*Sm));
x1=m1;
h1=m1;
for j=1:m1
if any(f9(:,j))==1
x1=j;
break
end
end
for j=x1:m1
if f9(:,j)==0;
h1=j;break
end
end
h=h1-x1;
axes(handles.axes1);
a=imshow(['D:\picture\',int2str(c),'.jpg']);
hold on
rectangle('Position',[y+x1,x,h,1.1*h],'LineWidth',2,'EdgeColor','r');%在原始图片上框出人脸范围
I{c}=a;
pause(0.001)
else
imshow(f1);
end
else
imshow(f1);
end
else
imshow(f1);
end
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)
close(gcf)
clear