Eigenface Detection Matlab

行人检查中突然感觉可以尝试下Eigenface的方法。

意识参考了:

http://www.pages.drexel.edu/~sis26/Eigenface%20Tutorial.htm

研究了下Eigen的方法。

其实Eigen就是把一组相同的图片加起来求平均,作为一个模板

然后保留起来。之后对输入的图片与这个模板求差值

然后看这个产值也可说是距离的远近来判断输入的face是不是

训练数据的的某个face

担任具体的算法中不会这么简单,具体中需要对图像进行锐化,之后用训练中的图片与mean相减

然后依据一定规则计算出一个权重,然后用这个权重去表示一个模板。

具体代码如下:

clc;
clear;
close all;

load('Tlist1.mat');
N = 10;
S = [];
um = 100;
ustd = 80;

[c d] = size(Tlist(10).image);
count = 1;
for j = 1:1:d
    image = Tlist(10).image{j};
    if isempty(image)==0
        temp = image(:,:,1);
        temp = imresize(temp,[380,150]);
        img{count} = temp;
        count = count + 1;
    end
end
count = 1;

figure(1);
for i = 1:1:50
    image = img{i};
    
    subplot(ceil(sqrt(50)),ceil(sqrt(50)),i);
    imshow(image);
    drawnow;
    [irow icol] = size(image);
    temp = reshape(image', irow*icol, 1);
    S = [S temp];
end

for i = 1:size(S,2)
    temp = double(S(:,i));
    m = mean(temp);
    st = std(temp);
    S(:,i) = (temp-m)*ustd/st+um;
end

figure(2)
for i = 1:50
    img = reshape(S(:,i), icol, irow);
    img = img';
    subplot(ceil(sqrt(50)),ceil(sqrt(50)),i);
    imshow(img);
    drawnow;
end

m = mean(S,2);
tmimg = uint8(m);
img = reshape(tmimg, icol, irow);
img = img';
figure(3);
imshow(img);

dbx = [];
for i = 1:1:50
    temp = double(S(:,i));
    dbx = [dbx temp];
end

A = dbx';
L = A*A';
[vv dd] = eig(L);
v = [];
d = [];
for i = 1:size(vv,2);
    if dd(i,i)>1e-4
        v=[v,vv(:,i)];
        d = [d,dd(i,i)];
    end
end

[B index] = sort(d);
ind = zeros(size(index));
dtemp = zeros(size(index));
vtemp = zeros(size(v));
len = length(index);
for i = 1:len
    dtemp(i) = B(len+1-i);
    ind(i) = len+1-index(i);
    vtemp(:,ind(i)) = v(:,i);
end
d = dtemp;
v = vtemp;

for i = 1:size(v,2)
    kk = v(:,i);
    temp = sqrt(sum(kk.^2));
    v(:,i) = v(:,i)./temp;
end

u = [];
for i =1:size(v,2)
    temp = sqrt(d(i));
    u = [u (dbx*v(:,i))./temp];
end

for i = 1:size(u,2)
    kk=u(:,i);
    temp = sqrt(sum(kk.^2));
    u(:,i) = u(:,i)./temp;
end

figure(4);
for i = 1:size(u,2)
    img = reshape(u(:,i),icol,irow);
    img = img';
    img = histeq(img,255);
    subplot(ceil(sqrt(50)),ceil(sqrt(50)),i);
    imshow(img);
    drawnow;
end

omega = [];
for h = 1:size(dbx,2)
    WW=[];
    for i = 1:size(u,2)
        t = u(:,i)';
        WeightOfImage = dot(t,dbx(:,h)');
        WW=[WW; WeightOfImage];
    end
    omega=[omega WW];
end

%for i = 1:22   
inputimage = Tlist(i).image{300};
%if isempty(inputimage)==0
%    continue;
%end
inputimage = imresize(inputimage,[380,150]);
inputimage = inputimage(:,:,1);
figure(5)
subplot(1,2,1);
imshow(inputimage);
colormap('gray');
title('inputimage','fontsize',18);
inimage = reshape(double(inputimage)', irow*icol,1);
temp = inimage;
me = mean(temp);
st = std(temp);
temp = (temp-me)*ustd/st+um;
normimage = temp;
difference = temp-m;

p = [];
aa=size(u,2);
for i = 1:aa
    pare = dot(normimage, u(:,i));
    p = [p; pare];
end
reshapedimage = m+u(:,1:aa)*p;
reshapedimage = reshape(reshapedimage, icol, irow);
reshapedimage = reshapedimage';
subplot(1,2,2);
imagesc(reshapedimage);
colormap('gray');
title('reconstructed image','fontsize',18);

inimweight = [];
for i = 1:size(u,2)
    t = u(:,i)';
    weightofinputimage = dot(t,difference');
    inimweight = [inimweight; weightofinputimage];
end

ll=1:50;
figure(6);
subplot(1,2,1);
stem(ll,inimweight);
title('weight fo input face','fontsize',14);


e = [];
for i = 1:size(omega,2);
    q = omega(:,i);
    diffweight = inimweight-q;
    mag = norm(diffweight);
    e = [e mag];
end

kk=1:size(e,2);
subplot(1,2,2);
stem(kk,e);
title('eucledian distance of input image','fontsize',14);

maximumvalue = max(e);
minimumvalue = min(e)
end


具体大算法在上面给的网页中有详细的说明!大家可以查看。

你可能感兴趣的:(Eigenface Detection Matlab)