【正文】
put from handles structurevarargout{1} = 。% Executes on button press in pushbuttontrain.function pushbuttontrain_Callback(hObject, eventdata, handles)% hObject handle to pushbuttontrain (see GCBO)% eventdata reserved to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)report = sprintf(39。正在訓(xùn)練樣本,請稍等。39。)。set(,39。String39。,report)。drawnowmytrain。%調(diào)用訓(xùn)練的m文件report = sprintf(39。PCA模型已經(jīng)生成,訓(xùn)練完畢,39。)。set(,39。String39。,report)。% Executes on button press in pushbuttonloadimg.function pushbuttonloadimg_Callback(hObject, eventdata, handles)% hObject handle to pushbuttonloadimg (see GCBO)% eventdata reserved to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%文件篩選框 選擇圖片[filename,pathname] = uigetfile({39。*.jpg。*.bmp。*.tif。*.png。*.gif39。,39。All Image Files39。},39。請選擇一張圖片39。)。if filename == 0%如果沒有選擇,直接返回即可 return。endstrfullname = strcat(pathname,filename)。%取得圖像文件全名 I = imread(strfullname)。%讀取圖片axes()imshow(I)。%顯示圖片title(39。測試圖像39。) = I。%保存在handles內(nèi),以便被其他函數(shù)調(diào)用report = sprintf(39。成功打開要測試的圖像:%s。39。,strfullname)。set(,39。String39。,report)。% Update handles structureguidata(hObject, handles)。% Executes on button press in pushbuttonrecg.function pushbuttonrecg_Callback(hObject, eventdata, handles)% hObject handle to pushbuttonrecg (see GCBO)% eventdata reserved to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)personnames = {39。 39。,39。 39。,39。 39。,39。 39。,39。 39。,39。楊洋 39。}。if ~isfield(handles,39。I39。)%判斷是否載入了圖片 report = sprintf(39。請先載入要測試的圖像!39。)。%提示信息 set(,39。String39。,report)。 msgbox(report)。 return。endif ~exist(39。39。)%是否訓(xùn)練了 report = 39。你還未訓(xùn)練樣本,請先訓(xùn)練!39。 msgbox(report,39。提示39。)。 set(,39。String39。,report)。 returnendI = 。load trainresult %裝載訓(xùn)練結(jié)果goalpixnum = goalsize(1)*goalsize(2)。%圖片中的像素個數(shù),goalpixnum[M,N,color] = size(I)。%獲取圖片的大小,以及色彩類型if color == 3%如果是彩色圖片 I1 = rgb2gray(I)。%轉(zhuǎn)為灰度圖片end% I1= imadjust(I1,stretchlim(I1))。%灰度變換,圖像增強if (isequal([M,N],goalsize) == 0)%如果不是64*64的 I1 = imresize(I1,goalsize)。%轉(zhuǎn)為固定的64*64的endI1 = reshape(I1, goalpixnum, 1)。%調(diào)整圖片矩陣樣式,轉(zhuǎn)為4096行1列的矩陣I1 = double(I1)。set(,39。string39。,39。測試數(shù)據(jù)降維......39。)testfeatures = UU39。*I1。%測試樣本的PCA特征矩陣TRAINNUM = size(samplesfeatures,2)。%訓(xùn)練樣本的個數(shù)dd = []。%存放距離for j =1:TRAINNUM tempfeature = samplesfeatures(:,j)。%取出某個樣本的PCA特征 d = dist(testfeatures,tempfeature)。%計算特征之間的距離 dd = [dd,d]。end[minvalue,minindex] = min(dd)。%minindex 距離的最小值所在的位置minvalueif minvalue =650 str = sprintf(39。未識別出此人。39。)。 set(,39。string39。,str)。 axes()。 claelse classlabel = ceil(minindex/trainnumpertype)。%測出的類別序號 picindex = mod(minindex,trainnumpertype)。%和這類中第幾個最接近 if picindex == 0 picindex = trainnumpertype。%圖片序列號 end str = sprintf(39。識別完成,識別此照片為第%d人:%s。39。,classlabel,personnames{classlabel})。 set(,39。string39。,str)。 drawnow axes()。 fn = sprintf(39。人臉庫\\%02d\\s%39。,classlabel,picindex)。%識別的那個人的 第一張照片拿出來當做識別的結(jié)果 imshow(imread(fn))。 strtitle = sprintf(39。識別結(jié)果第%d人:%s39。,classlabel,personnames{classlabel})。 title(strtitle)end% Executes on button press in pushbuttonquit.function pushbuttonquit_Callback(hObject, eventdata, handles)% hObject handle to pushbuttonquit (see GCBO)% eventdata reserved to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)close27