📄 runpcakernel.m
字号:
subplot(2,2,k);hold on;
plot(nT2Faces(2*k-1,:), nT2Faces(2*k,:), '.b');
plot(t2Faces(2*k-1,:), t2Faces(2*k,:), '+r');
str=sprintf('%s%d-%d', '\fontsize{12}',2*k-1, 2*k); xlabel(str);
end
title('\fontsize{12} Upper Face Distribution on PCA 3-5');
figure; hold on;
d1=4; d2=7;
plot(nT2Faces(d1,:), nT2Faces(d2,:), '.b');
plot(t2Faces(d1,:), t2Faces(d2,:), '+r');
xlabel('\fontsize{12} Upper (T2) Face Distribution on PCA 4-7');
% plot lower face on dim 1 and 2.
figure;
for k=1:4
subplot(2,2,k);hold on;
plot(nT3Faces(2*k-1,:), nT3Faces(2*k,:), '.b');
plot(t3Faces(2*k-1,:), t3Faces(2*k,:), '+r');
str=sprintf('%s%d-%d', '\fontsize{12}',2*k-1, 2*k); xlabel(str);
end
title('\fontsize{12} Lower (T3) Face Distribution on PCA 3-5');
figure;hold on;d1=1;d2=6;
plot(nT3Faces(d1,:), nT3Faces(d2,:), '.b');
plot(t3Faces(d1,:), t3Faces(d2,:), '+r');
title('\fontsize{12} Lower (T3) Face Distribution on PCA 1-6');
p=8;q=4;alpha=0.02; beta=0.1; thres=0.5;
[relevance] = relevanceRegression( p, q, alpha, beta, thres)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% non-linear mapping
% 12-11-2003.
%%%%%%%%%%%%%%%%%%%%%%%%%%%
CovT1Faces = cov(t1Faces');
CovT2Faces = cov(t2Faces');
CovT3Faces = cov(t3Faces');
StdT1Faces = sqrt(diag(CovT1Faces))
% [258.6135 99.7751 92.8625 79.4257 60.8324 54.7351 53.1098 46.3724 45.6027 41.6553 40.5613 38.7417];
StdT2Faces = sqrt(diag(CovT2Faces))
% [191.6423 92.2207 75.4775 58.2254 49.4999 41.9451 39.8454 37.1355];
StdT3Faces = sqrt(diag(CovT3Faces))
% [187.1485 67.5272 54.5260 49.3976 46.3355 40.8735 34.2275 30.2965]
for k=1:length(faces(1,:))
x = faces(:,0+k);
px(k)=faceProb(T1, T2, T3, mT1Face, mT2Face, mT3Face, x, '.b');
end
for k=1:length(nFaces(1,:))
y = nFaces(:,0+k);
py(k)=faceProb(T1, T2, T3, mT1Face, mT2Face, mT3Face, y, '.r');
end
for k=1:1000
x = faces(:,0+k);
y = nFaces(:,0+k);
figure(1);
title('\fontsize{12}nonfaces distrb');
subplot(2,1,2);px(k)=faceProb(T1, T2, T3, mT1Face, mT2Face, mT3Face, y, '.b');
title('\fontsize{12}faces distrb');
subplot(2,1,1);py(k)=faceProb(T1, T2, T3, mT1Face, mT2Face, mT3Face, x, '.r');
%figure(2); hold on;
%faceProb(T1, T2, T3, mT1Face, mT2Face, mT3Face, y, '.b');
%faceProb(T1, T2, T3, mT1Face, mT2Face, mT3Face, x, '.r');
end
title('\fontsize{12}faces vs nonfaces distrb');
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% face detection
% 12-11-2003.
%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [pos]=faceDetection(img, sWin, T1, T2, T3, mT1Face, mT2Face, mT3Face)
dbg = 'y';
if dbg == 'y'
sWin= [24, 32; 30, 45];
img = imread('k:/work/Face/matlab/seinfeld3.jpg', 'jpg');
imshow(img, [0 255]);
end
% const
W = 12; H=16;
% var
[h w] = size(img);
nScale = length(sWin(:,1));
cnt = 0;
hold on;
pos=[];
for s=1:nScale
% search window size
sW=sWin(1,s); sH=sWin(2,s);
fprintf('\n scale=%d, sW=%d sH=%d', s, sW, sH);
% search
for y=1:h-sH
for x=1:w-sW
face = img(y:y+sH-1,x:x+sW-1);
[icon, vec]=scaleImage(face, W, H);
prob = faceProb(T1, T2, T3, mT1Face, mT2Face, mT3Face, vec, '.r');
if prob == 1
cnt = cnt+1;
pos(cnt,1:4) = [x y sW sH];
fprintf('+');
%plotBox(x,y,sW,sH, '-r');
end
end
fprintf('\n >');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% face detection: SVM
% 02-09-2004.
%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
path(path, 'd:\work\SVMToolbox');
W=12; H=16;
% load PCA transforms
T1 = load('pcaFaceW12H16.dat');
T2 = load('pcaUpperFaceW12H16.dat');
T3 = load('pcaLowerFaceW12H16.dat');
% load face/nonface training data
faces = load('faFacesW12H16.dat');
[d n]=size(faces);
upFaces=faces(1:d/2,1:n);loFaces=faces(1+d/2:192,1:n);
mFace = mean(faces')'; mUpFace = mean(upFaces')'; mLoFace = mean(loFaces')';
nFaces = load('nonFacesW12H16.dat');
mT1Face = T1*mFace; mT2Face = T2*mUpFace; mT3Face = T3*mLoFace;
% compute transformed faces/nonfaces
t1Faces= T1*faces; nT1Faces=T1*nFaces;
t2Faces= T2*upFaces; nT2Faces=T2*nFaces(1:96,:);
t3Faces= T3*loFaces; nT3Faces=T3*nFaces(97:192,:);
% remove mean
for j=1:length(faces(1,:))
t1Faces(:,j)=t1Faces(:,j) - mT1Face;
t2Faces(:,j)=t2Faces(:,j) - mT2Face;
t3Faces(:,j)=t3Faces(:,j) - mT3Face;
end
% remove mean
for j=1:length(nFaces(1,:))
nT1Faces(:,j)= nT1Faces(:,j)- mT1Face;
nT2Faces(:,j)= nT2Faces(:,j)- mT2Face;
nT3Faces(:,j)= nT3Faces(:,j)- mT3Face;
end
saveModel = 'n';
if saveModel == 'y
% save models
save 't1Faces.dat' t1Faces -ascii;
save 't2Faces.dat' t2Faces -ascii;
save 't3Faces.dat' t3Faces -ascii;
save 'nT1Faces.dat' nT1Faces -ascii;
save 'nT2Faces.dat' nT2Faces -ascii;
save 'nT3Faces.dat' nT3Faces -ascii;
end
% train SVM 1
X1 = [t1Faces, nT1Faces]';
Y1 = [ones(length(t1Faces(1,:)), 1)', -1.*ones(length(nT1Faces(1,:)),1)']';
ker = 'poly', u=2, v=1;
C= 10;
[nsv, alpha, b0] = svc(X1,Y1, ker, C);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% face detect
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
img = imread('faceTest2.jpg', 'jpg');
sWin = [24 32;29 36];
styl = '-r';
svmFaceDetection(img, sWin, styl);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% face recog
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
% nfaceW16H24;
% save 'nfacew16h24.mat' nfacew16h24;
load 'nfacew16h24.mat';
truth = load('feretGndTruth.dat');
[n w]=size(truth);
offs = 400;
fw = 20; fh = 30; side = 0.5; top= 1.2; low = 1.8;
for k=1:60
subplot(6,10,k);
[icon]=getFeretFaceIcon(truth, 'y', offs+k, side, top, low, fw, fh);
axis off;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute face icons
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
path(path, 'k:/work/SVMToolbox/svm');
truth = load('feretGndTruth.dat');
[n w]=size(truth);
m=0; cnt=0; doHistEq='n';
%side = 0.5; top= 1.2; low = 1.8; fw=12; fh=18;
side = 0.4; top= 0.6; low = 0.6; fw=15; fh=20;
% test
view_face = 'y';
if view_face == 'y'
offs = 300;
for k=1:24
subplot(4,6,k); getFeretFaceIcon(truth, doHistEq, offs+k, side, top, low, fw, fh);
axis off;
end
end
for k=1:n
if k == 2438 % bad annotation
icon == -1;
else
[icon]=getFeretFaceIcon(truth, doHistEq, k, side, top, low,fw, fh);
end
if icon ~= -1
m=m+1; fprintf('.');
faceIcons(m,1:fw*fh)=icon;
else
fprintf('x');
end
cnt = cnt + 1;
if cnt >= 100
cnt =0; fprintf('\n');
end
end
size(faceIcons)
%save 'faceIconsW12H18HistEqed.mat' faceIcons;
save 'faceIconsW12H18NoHistEq.mat' faceIcons;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PCA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
load 'faceIconsW16H24NoHistEq.mat';
x = double(faceIcons');
[T, lat, mx]=pcaAnalysis(x, 24);
save 'meanfaceIconsW16H24NoHistEq.dat' mx -ascii;
save 'pcafaceIconsW16H24NoHistEq.dat' T -ascii;
% proj icons to PCA space
[d n]=size(x);
figure; hold on;
for k=1:n
y(:,k) = T*(x(:,k)-mx');
plot(y(1,k), y(2,k), '.');
end
save 'pctfaceIconsW16H24NoHistEq.dat' y -ascii;
clear;
load 'faceIconsW16H24HistEqed.mat';
x = double(faceIcons');
[T, lat, mx]=pcaAnalysis(x, 24);
save 'meanfaceIconsW16H24HistEqed.dat' mx -ascii;
save 'pcafaceIconsW16H24HistEqed.dat' T -ascii;
% proj icons to PCA space
[d n]=size(x);
hold on;
for k=1:n
y(:,k) = T*(x(:,k)-mx');
plot(y(1,k), y(2,k), '.r');
end
save 'pctfaceIconsW16H24HistEqed.dat' y -ascii;
clear;
x = load('pctfaceIconsW16H24NoHistEq.dat');
y = load('pctfaceIconsW16H24HistEqed.dat');
plot(x(1,:), x(2,:), '.'); hold on; plot(y(1,:), y(2,:), '.r');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SVM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% make data
clear;
path(path, 'k:/work/SVMToolbox');
n1 = 20; n2 = 30;
axis([0 40 0 40]); hold on;
fprintf('\n input samples for class +1: ');
for j=1:n1
[X(j,1) X(j,2)] = ginput(1);
plot(X(j,1), X(j,2), '+r');
Y(j) = 1;fprintf('+');
end
fprintf('\n input samples for class -1: ');
for j=n1+1:n1+n2
[X(j,1) X(j,2)] = ginput(1);
plot(X(j,1), X(j,2), '.b');
Y(j) = -1; fprintf('.');
end
Y = Y';
C= inf; % no slack, alpha >=0
[N, alpha, b0] = svm(X, Y, C);
face=load('faFacesW12H16.dat');
nface = load('nonFaceW12H16.dat');
% proj:
face = load('t2face.dat');
nface = load('t2nface.dat');
hold on; d1=4; d2=7;
plot(face(d1,:), face(d2,:), '+r');plot(nface(d1,:), nface(d2,:), '.b');
X=[face nface]';
Y=[ones(1, 1118) -ones(1,1800)]';
C=50;
[N, alpha, b0] = svm(X, Y, C);
save 'alphaC50.mat' alpha;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% sort samples by mean luma
% switch model in SVM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
load 'nfacew16h24.mat';
load 'faceIconsW16H24NoHistEq.mat';
face = faceIcons;
nface = nfacew16h24';
% sort face by mean luma
luma = mean(face');
%stem(luma, '.');
[v, sort_indx]=sort(luma);
max_v = max(v); min_v = min(v); std_v = std(v); mean_v=mean(v);
m=0; v0 = 80; v1 = 150;
for k=1:length(v)
if v(k) > v0 & v(k) < v1
m=m+1; sface(m,:)=face(sort_indx(k),:);
end
end
mf =load('meanfaceIconsW16H24NoHistEq.dat');
T =load('pcafaceIconsW16H24NoHistEq.dat');
% proj nonface
nface = double(nface);
for k=1:length(nface(:,1))
pctNFace(:,k) = T*(nface(k,:)-mf)';
end
% proj face
sface = double(sface);
for k=1:length(sface(:,1))
pctFace(:,k) = T*(sface(k,:)-mf)';
end
hold on; d1=5; d2=9;
plot(pctNFace(d1,:), pctNFace(d2,:), '.');
offs=1; n=400;
plot(pctFace(d1,offs:offs+n), pctFace(d2,offs:offs+n), '+r');
offs=401;
plot(pctFace(d1,offs:offs+n), pctFace(d2,offs:offs+n), '+m');
offs=801;
plot(pctFace(d1,offs:offs+n), pctFace(d2,offs:offs+n), '+g');
offs=1201;
plot(pctFace(d1,offs:offs+n), pctFace(d2,offs:offs+n), '+y');
offs=1601;
plot(pctFace(d1,offs:offs+n), pctFace(d2,offs:offs+n), '+k');
xstr = sprintf('%s comp no. %d', '\fontsize{12}',d1);
ystr = sprintf('%s comp no. %d', '\fontsize{12}',d2);
xlabel(xstr); ylabel(ystr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -