Demo entry 6742812

matlab

   

Submitted by anonymous on May 19, 2018 at 09:08
Language: Matlab. Code size: 3.1 kB.

clear;clc;close all;



orl_data=File_Read_ORL();
[train_face,test_face,indexs]=imgdata(orl_data,8);

%% ¶ÁÈ¡Êý¾Ý,Ñ¡Óà ORLÈËÁ³¿â  
function filedata = File_Read_ORL()  
%  Ó÷¨£º   filedata = File_Read();  
%  Ñù±¾Êý£º40¸öÈË£¬Ã¿¸öÑù±¾»¹ÓÐ10¸ö×ÓÑù±¾£¬Ã¿¸ö×ÓÑù±¾´óС£º119*92    
% clc; clear all;  
% ÔØÈëÎļþ¼Ð  
    for i = 1:40
%         pathname = uigetdir(cd, 'orl_faces');
        pathname=strcat('orl_faces/s',num2str(i));
        filesjpg = ls(strcat(pathname,'/*.pgm'));
        files = [cellstr(filesjpg)]; % µÃµ½Îļþ·¾¶
        len = length(files); % Îļþ¸öÊý
        for j = 1:len
            Filesname = strcat(pathname,'/',files(j));
            filedata{i,j} = imread(cell2mat(Filesname));
        end
    end
end

%% ÿ¸öÈËÈ¡num¸öÑù±¾Á³
function [train_face,test_face,indexs] = imgdata(filedata,num)  
%Ó÷¨£ºÊÊÓÃÓÚ ORLÈËÁ³¿â
[m,n] = size(filedata{1,1});  %ȡͼƬ´óС
indexs=cell(40,1);
for i = 1:40   %¹²ÓÐ40¸öÈË
    n_rand = randperm(10);
    indexs{i}=n_rand;
    for j = 1:num
        train_pic{(i-1)*num+j} = filedata{i,n_rand(j)};%j
    end
    for k = 1:(10-num)
        test_pic{(i-1)*(10-num)+k} = filedata{i,n_rand(k+num)};%k+num
    end
end
for i = 1:40*num
    train_face(i,:) = reshape(train_pic{i},1,m*n);
end
for i = 1:40*(10-num)
    test_face(i,:) = reshape(test_pic{i},1,m*n);
end

end


%%
%--------------º¯Êý˵Ã÷-------------    
%-----ÈËÁ³Æ¥Åä----ORL/ARÈËÁ³¿â  
%-----ÊäÈ룺ѵÁ·Ñù±¾µÄimg_new   img_mean   V  
%           k:ѵÁ·¼¯ÖÐÿ¸öÈËÁ³Ñ¡È¡µÄ¸öÊý£¨×î´óΪ10 or 26£©  
%-----------------------------------    
function accuracy = facefind(img_new,img_mean,V,test_face,k)  
%%  
num_train = size(img_new,1);  %ѵÁ·Á³×ÜÊý  
num_test = size(test_face,1); %²âÊÔÁ³×ÜÊý  
true_num = 0;  
for i = 1:num_test  
%-----Êä³ö£º×¼È·ÂÊaccuracy  
    pic = double(test_face(i,:));  
    pic1 = pic - img_mean;  
    pic2 = pic1*V;  
    for j = 1:num_train  
        error(j) = norm(img_new(j,:) - pic2);  %ÇóÈ¡·¶Êý¾àÀë  
    end  
    face_find = find(error<=min(error));   %È¡×îСÕß  
    face_find = ceil(face_find/k);  %ÏòÉÏÈ¡Õû  ±íʾʶ±ðµÄÊǵڼ¸¸öÈ˵ÄÁ³  
    true_face = ceil(i/(26-k));  %×¢ÒâÕâ¸öÊý×Ö£º10-ORLÈËÁ³¿â£»26-ARÈËÁ³¿â  
                                 %ÕæʵµÄÊǵڼ¸¸öÈ˵ÄÁ³  
     if face_find == true_face  
         true_num = true_num+1;  %ÕýÈ··ÖÀàµÄ¸öÊý  
     end  
end  
accuracy = true_num/num_test;   
end




function [img_new,img_mean,V] = PCA(img,k)  
    %Ó÷¨£º  [img_new,img_mean,V] = PCA(train_face,k)
    %reshapeº¯Êý£º¸Ä±ä¾ä¾ØÕóµÄ´óС£¬¾ØÕóµÄ×ÜÔªËظöÊý²»Äܱä
    img = double(img);
    [m,n] = size(img);  %È¡´óС
    img_mean = mean(img); %ÇóÿÁÐƽ¾ùÖµ
    img_mean_all = repmat(img_mean,m,1);%¸´ÖÆmÐÐƽ¾ùÖµÖÁÕû¸ö¾ØÕó
    Z = img - img_mean_all;
    T=Z*Z';  %Э·½²î¾ØÕ󣨷ÇԭʼËùÇó¾ØÕóµÄЭ·½²î)
    [V,D] = eigs(T,k);%¼ÆËãTÖÐ×î´óµÄÇ°k¸öÌØÕ÷ÖµÓëÌØÕ÷ÏòÁ¿
    V=Z'*V;         %Э·½²î¾ØÕóµÄÌØÕ÷ÏòÁ¿
    for i=1:k       %ÌØÕ÷ÏòÁ¿µ¥Î»»¯
        l=norm(V(:,i));
        V(:,i)=V(:,i)/l;
    end
    img_new = Z*V;  %µÍά¶Èϵĸ÷¸öÁ³µÄÊý¾Ý
end

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).