利用MATLAB实现人脸识别GUI程序设计
生活随笔
收集整理的這篇文章主要介紹了
利用MATLAB实现人脸识别GUI程序设计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
利用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:320Temp=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:320all_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}); endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_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
總結
以上是生活随笔為你收集整理的利用MATLAB实现人脸识别GUI程序设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于SSM的医院药品库存管理系统
- 下一篇: webuploader java版本