利用MATLAB实现人脸识别GUI程序设计

本文详细介绍了如何利用MATLAB设计一个用户界面友好的人脸识别程序,包括图像加载、预处理、特征提取(PCA)及人脸识别流程。通过GUI界面,用户可以上传测试图像并实时进行人脸识别,结果显示与最相似的训练图像对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用MATLAB实现人脸识别GUI程序设计

%% 人脸识别
clc;clear;close all;
%% 加载文件路径
addpath(genpath('F:\课程\模式识别\'));
%% --------- 训练集图像文件-------
Train_file_path = 'F:\课程\模式识别\实验四数据\TrainDatabase\ORL\';
Train_data_list = dir(strcat(Train_file_path));
%------训练集图像元胞分配内存-----%
Train_image=cell(size(Train_data_list,1),8);
Train_image_line=cell(8*size(Train_data_list,1),1);
%--------遍历得到训练集图像-------%
k=1;
for i=3:size(Train_data_list,1)
    img_file=Train_data_list(i).name;% 获取文件夹名称
    Train_img_list=dir(strcat(Train_file_path,img_file,'\*.jpg'));% 获取文件夹下图像
    img_num = length(Train_img_list);%获取图像总数量
    for j = 1:img_num %逐一读取图像
        Train_image_name = Train_img_list(j).name;% 图像名
        image = imread(strcat(Train_file_path,img_file,'\',Train_image_name));
        Train_image{i,j}=image; % 存入元胞数组
        Train_image_line{k,1}=image; % 存入元胞数组
        k=k+1;
        fprintf('%d %d %s\n',i,j,strcat(Train_file_path,img_file,'\',Train_image_name));% 显示正在处理的图像名
    end
end
%---------删除空余两行---------%
%---小括号删除元素,大括号置空--%
Train_image(1,:)=[];
Train_image(2,:)=[];
Train_image_line(321:336)=[];

%% 图像宽92,高112,8bit
All_Train_img=zeros(8*(size(Train_data_list,1)-2),112*92);
% 建立所有训练集图像矩阵
% for i=1:320
%     Temp=Train_image_line{i,1};
%     Temp=Temp';% 先转置
%     Temp=Temp(:);% 再行向化
%     All_Train_img(i,:)=Temp;
% end
for i=1:320
    Temp=Train_image_line{i,1};
    Temp=Temp(:);
    Temp=Temp';
    All_Train_img(i,:)=Temp;
end
% 求所有图片各像素平均值(平均脸)
image_mean=mean(All_Train_img,1);
% 求所有图片像素点位-平均值(中心化)
all_img_diff=zeros(320,92*112);
for i=1:320
    all_img_diff(i,:)=All_Train_img(i,:)-image_mean;
end
% 求协方差矩阵
sigma_mat=all_img_diff*all_img_diff';
% 求特征值和特征向量
[U , D]=eig(sigma_mat);%为什么这里是已经排序好的特征值
% 对特征值进行排序
D_diag=diag(D); %取对角线元素
[D_sort,index]=sort(D_diag,'descend'); %以降序排序 
Sum_D = sum(D_sort);%对所有特征值求和
temp = 0;
for i = 1:size(All_Train_img,1)
    temp = temp + D_sort(i,1);
    m = i;
    if (temp/Sum_D >0.95)%特征值占比95%
        break;
    end
end
U1 = U(:,index);%按照特征值的索引排序特征向量
New_U = U1(:,1:m);% 选取95%对应的特征向量组成矩阵
% 特征脸
Feature_face=all_img_diff'*New_U;
% 特征脸投影
Pn=All_Train_img*Feature_face;
save('F:\课程\模式识别\Feature_face.mat','Feature_face');
save('F:\课程\模式识别\Pn.mat','Pn')
save('F:\课程\模式识别\image_mean.mat','image_mean')
function varargout = exp_4_GUI(varargin)
% EXP_4_GUI MATLAB code for exp_4_GUI.fig
%      EXP_4_GUI, by itself, creates a new EXP_4_GUI or raises the existing
%      singleton*.
%
%      H = EXP_4_GUI returns the handle to a new EXP_4_GUI or the handle to
%      the existing singleton*.
%
%      EXP_4_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EXP_4_GUI.M with the given input arguments.
%
%      EXP_4_GUI('Property','Value',...) creates a new EXP_4_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before exp_4_GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to exp_4_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 exp_4_GUI

% Last Modified by GUIDE v2.5 10-Dec-2020 18:01:46

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @exp_4_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @exp_4_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 exp_4_GUI is made visible.
function exp_4_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 exp_4_GUI (see VARARGIN)

% Choose default command line output for exp_4_GUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes exp_4_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = exp_4_GUI_OutputFcn(hObject, eventdata, handles) 


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)
addpath(genpath('F:\课程\模式识别\实验四数据\TestDatabase\'));
[FileName,PathName] = uigetfile('F:\课程\模式识别\实验四数据\TestDatabase\*.jpg','请选择一幅图像');
[picture_in_one,map]=imread(FileName);%imread函数实现图像导入
axes(handles.axes1);  %用axes命令设定当前操作的坐标轴
imshow(picture_in_one);
title('测试人脸');
% handles.axes1=picture_in_one;
handles.picture_in_one=picture_in_one;
guidata(hObject,handles);%使用guidata函数保存该句柄结构

% --- 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)
% 通过句柄得到图片
%picture=handles.axes1;% uint8的图片
picture=handles.picture_in_one;% uint8的图片
picture_line = double(picture(:));
% New_U=importdata('F:\课程\模式识别\New_U.mat');
Feature_face=importdata('F:\课程\模式识别\Feature_face.mat');
Pn=importdata('F:\课程\模式识别\Pn.mat');
P=(picture_line)' * Feature_face;
% 最小距离法,寻找和待识别图片最为接近的训练图片
dist=zeros(size(Pn,1),1);
for k = 1:size(Pn,1)
    temp=(P - Pn(k,:)).^2;
    dist(k)= sqrt(sum(temp(:)));    %欧氏距离
end
[~,id]=sort(dist,'ascend');   %筛选出距离前三小的
% --------- 训练集图像文件-------
Train_file_path = 'F:\课程\模式识别\实验四数据\TrainDatabase\ORL\';
Train_data_list = dir(strcat(Train_file_path));
%------训练集图像元胞分配内存-----%
Train_image=cell(size(Train_data_list,1),8);
Train_image_line=cell(8*size(Train_data_list,1),1);
%--------遍历得到训练集图像-------%
k=1;
for i=3:size(Train_data_list,1)
    img_file=Train_data_list(i).name;% 获取文件夹名称
    Train_img_list=dir(strcat(Train_file_path,img_file,'\*.jpg'));% 获取文件夹下图像
    img_num = length(Train_img_list);%获取图像总数量
    for j = 1:img_num %逐一读取图像
        Train_image_name = Train_img_list(j).name;% 图像名
        image = imread(strcat(Train_file_path,img_file,'\',Train_image_name));
        Train_image{i,j}=image; % 存入元胞数组
        Train_image_line{k,1}=image; % 存入元胞数组
        k=k+1;
        fprintf('%d %d %s\n',i,j,strcat(Train_file_path,img_file,'\',Train_image_name));% 显示正在处理的图像名
    end
end
%---------删除空余两行---------%
%---小括号删除元素,大括号置空--%
Train_image_line(321:336)=[];
X1=Train_image_line{id(1,1),:};
X2=Train_image_line{id(2,1),:};
X3=Train_image_line{id(3,1),:};
axes( handles.axes2 )
imshow(X1);
axes( handles.axes3 )
imshow(X2);
axes( handles.axes4 )
imshow(X3);

% --- 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
set(gca,'XColor',get(gca,'Color')) ;% 这两行代码功能:将坐标轴和坐标刻度转为白色
set(gca,'YColor',get(gca,'Color'));
 
set(gca,'XTickLabel',[]); % 这两行代码功能:去除坐标刻度
set(gca,'YTickLabel',[]);
% Hint: place code in OpeningFcn to populate axes1

% 识别图片
% --- Executes during object creation, after setting all properties.
function axes2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
set(gca,'XColor',get(gca,'Color')) ;% 这两行代码功能:将坐标轴和坐标刻度转为白色
set(gca,'YColor',get(gca,'Color'));
 
set(gca,'XTickLabel',[]); % 这两行代码功能:去除坐标刻度
set(gca,'YTickLabel',[]);
% Hint: place code in OpeningFcn to populate axes2

% 识别图片
% --- Executes during object creation, after setting all properties.
function axes3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
set(gca,'XColor',get(gca,'Color')) ;% 这两行代码功能:将坐标轴和坐标刻度转为白色
set(gca,'YColor',get(gca,'Color'));
 
set(gca,'XTickLabel',[]); % 这两行代码功能:去除坐标刻度
set(gca,'YTickLabel',[]);
% Hint: place code in OpeningFcn to populate axes3

% 识别图片
% --- Executes during object creation, after setting all properties.
function axes4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
set(gca,'XColor',get(gca,'Color')) ;% 这两行代码功能:将坐标轴和坐标刻度转为白色
set(gca,'YColor',get(gca,'Color'));
 
set(gca,'XTickLabel',[]); % 这两行代码功能:去除坐标刻度
set(gca,'YTickLabel',[]);
% Hint: place code in OpeningFcn to populate axes4

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值